1

C++ 経由で R シェルをエミュレートしようとする私のコードでは、ユーザーが tcp 接続を介して R コマンドを送信し、実行時に RInside::parseEvalQ 関数を介して R インスタンスに渡すことができます。不適切な形式のコマンドを処理できる必要があります。parseEvalQ の引数として不正なコマンドが与えられるたびに、スローされた実行時エラーをキャッチします (RInside.cpp を見ると、parseEval(const string&, SEXP) 関数内で特定のエラーに「PARSE_ERROR」「status」というフラグが付けられています)、what() 「St9exception」例外を与えます。

私は 2 つの問題を抱えています。

1a. 最初の解析エラーの後、引き数が有効であっても parseEvalQ を呼び出すと、別の解析エラーが発生します。埋め込まれた R インスタンスは、解析エラーによって何らかの形で破損していますか?

1b. RInside のドキュメントでは、Rcpp::Evaluator::run を使用して C++ で R 例外を処理することを推奨しています (これは、エラー ステータス 'PARSE_ERROR を返す前に、parseEval(const string&, SEXP) の呼び出し中に R インスタンス内のどこかでスローされていると思われます)。 ')。私はこれを使用しようと実験しましたが、Rcpp::Evaluator::run を実際に使用する方法の例をウェブ上で見つけることができません。

2. 私のプログラムでは、stdout と stderr (C++ レベル) を tcp 接続のファイル記述子に再ルーティングします。RInside インスタンスからのエラー メッセージはコンソールに送信されますが、通常の出力は送信されません。stdout を stderr に再ルーティングするために、RInside コマンド 'sink(stderr(), type="output")' を送信します (stderr がコンソールに表示されているように見えるため) が、通常の出力はまだ表示されません。「print(command)」は機能しますが、通常の R シェルのように stdout をコンソールに直接渡すためのよりクリーンな方法が必要です。

どんな助けや考えも大歓迎です。私のコードの蒸留バージョンを以下に示します。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

using namespace std;

string request_cpp;

ostringstream oss;

int read(FILE* tcp_fd)  
{
  /* function to read input from FILE* into the 'request_cpp' string */
}  

int write(FILE* tcp_fd, const string& response)  
{
  /* function to write a string to FILE* */
}

int main(int argc, char* argv[])  
{
  // create RInside object
  RInside R(argc,argv);

  //socket  
  int sd = socket(PF_INET, SOCK_STREAM, 0);  
  addr.sin_family = AF_INET;  
  addr.sin_port = htons(40650);

  // set and accept connection on socket  
  inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);  
  bind(sd,(struct sockaddr*)&addr, sizeof(addr));  
  listen(sd,1);  
  int sd_i = accept(sd, 0, 0);  

  //re-route stdout and stderr to socket  
  close(1);  
  dup(sd_i);  
  close(2);  
  dup(sd_i);  

  // open read/write file descriptor to socket

  FILE* fp = fdopen(sd_i,"r+");  

  // emulate R prompt  

  write(fp,"> ");  

  // (attempt to) redirect R's stdout to stderr
  R.parseEvalQ("sink(stderr(),type=\"output\");");

  // read from socket and pass commands to RInside  
  while( read(fp) )  
  {
    try  
    {
      // skip empty input  
      if(request_cpp == "")  
      {  
        write(fp, "> ");  
        continue;  
      }
      else if(request_cpp == "q()")  
      {  
        break;
      }  
      else  
      {  
        // clear string stream  
        oss.str("");  

        // wrap command in try  
        oss << "try(" << request_cpp << ");" << endl;  

        // send command  
        R.parseEvalQ(oss.str());  
      }  
    }  
    catch(exception e)  
    {  
      // print exception to console  
      write(fp, e.what());  
    }    
 write(fp, "> ");
 }

fclose(fp);  
close(sd_i);  
exit(0);  
}
4

1 に答える 1

1

「r」タグを使用していないため、この数週間前に見逃しました。

Simon の信頼できるrserverを再実装しているようです。それを直接使用しないのはなぜですか?

それ以外の場合は、rcpp に関する質問については、rcpp-devel リストで質問することを検討してください。

于 2011-05-03T02:48:40.763 に答える