Example #1
0
 /**
  * Evaluate a string as an R code and return result
  * @param string $string
  * @param int $parser 
  * @param REXP_List $attr
  */
 public function evalString($string, $parser = self::PARSER_NATIVE, $attr = NULL)
 {
     $r = $this->command(self::CMD_eval, $string);
     $i = 20;
     if (!$r['is_error']) {
         $buf = $r['contents'];
         $r = NULL;
         switch ($parser) {
             case self::PARSER_NATIVE:
                 $r = Rserve_Parser::parse($buf, $i, $attr);
                 break;
             case self::PARSER_REXP:
                 $r = Rserve_Parser::parseREXP($buf, $i, $attr);
                 break;
             case self::PARSER_DEBUG:
                 $r = Rserve_Parser::parseDebug($buf, $i, $attr);
                 break;
             case self::PARSER_NATIVE_WRAPPED:
                 $old = Rserve_Parser::$use_array_object;
                 Rserve_Parser::$use_array_object = TRUE;
                 $r = Rserve_Parser::parse($buf, $i, $attr);
                 Rserve_Parser::$use_array_object = $old;
                 break;
             default:
                 throw new Exception('Unknown parser');
         }
         return $r;
     }
     // TODO: contents and code in exception
     throw new Rserve_Exception('unable to evaluate');
 }
Example #2
0
 /**
  * 
  * Parse a response from Rserve
  * @param string $r
  * @param int $parser
  * @return parsed results 
  */
 private function parseResponse($buf, $parser)
 {
     $type = int8($buf, 0);
     if ($type != self::DT_SEXP) {
         // Check Data type of the packet
         throw new Rserve_Exception('Unexpected packet Data type (expect DT_SEXP)', $buf);
     }
     $i = 4;
     // + 4 bytes (Data part HEADER)
     $r = NULL;
     switch ($parser) {
         case self::PARSER_NATIVE:
             $r = Rserve_Parser::parse($buf, $i);
             break;
         case self::PARSER_REXP:
             $r = Rserve_Parser::parseREXP($buf, $i);
             break;
         case self::PARSER_DEBUG:
             $r = Rserve_Parser::parseDebug($buf, $i);
             break;
         case self::PARSER_NATIVE_WRAPPED:
             $old = Rserve_Parser::$use_array_object;
             Rserve_Parser::$use_array_object = TRUE;
             $r = Rserve_Parser::parse($buf, $i);
             Rserve_Parser::$use_array_object = $old;
             break;
         default:
             throw new Rserve_Exception('Unknown parser');
     }
     return $r;
 }