RInside:parseEvalQ“解析错误”导致每次后续调用 parseEvalQ 都会给出“解析错误”即使异常已处理

发布于 2024-11-01 14:51:58 字数 2779 浏览 2 评论 0原文

我的代码尝试通过 C++ 模拟 R shell,允许用户通过 tcp 连接发送 R 命令,然后在运行时通过 RInside::parseEvalQ 函数传递到 R 实例。我必须能够处理格式错误的命令。每当一个错误的命令作为 parseEvalQ 的参数给出时,我都会捕获抛出的运行时错误(查看 RInside.cpp 我的特定错误在 parseEval(const string&, SEXP) 函数中用“PARSE_ERROR”“status”标记),什么( ) 给出“St9exception”异常。

我有两个问题,第一个比第二个更紧迫:

1a 。在初始解析错误之后,即使参数有效,对 parseEvalQ 的任何后续调用都会导致另一个解析错误。嵌入式 R 实例是否因解析错误而以某种方式损坏?

1b. RInside 文档建议使用 Rcpp::Evaluator::run 来处理 C++ 中的 R 异常(我怀疑在调用 parseEval(const string&, SEXP) 期间,在返回错误状态之前,会在 R 实例中的某个地方抛出异常)解析错误')。我尝试过使用它,但在网上找不到如何实际使用 Rcpp::Evaluator::run 的示例。

2.在我的程序中,我将 stdout 和 stderr (在 C++ 级别)重新路由到我的 tcp 连接的文件描述符,来自 RInside 实例的任何错误消息都会发送到控制台,但常规输出不会。我发送 RInside 命令 'sink(stderr(), type="output")' ,试图将 stdout 重新路由到 stderr (因为 stderr 似乎显示在我的控制台中),但常规输出仍然没有显示。 'print(command)' 有效,但我想要一种更干净的方法,将 stdout 直接传递到控制台,就像在普通 R shell 中一样。

任何帮助和/或想法将不胜感激。我的代码的精炼版本如下所示:

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

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);  
}

My code, which tries to emulate an R shell via C++, allows a user to send R commands over a tcp connection which are then passed to the R instance through the RInside::parseEvalQ function, during runtime. I have to be able to handle badly formatted commands. Whenever a bad command is given as an argument to parseEvalQ I catch the runtime error thrown (looking at RInside.cpp my specific error is flagged with 'PARSE_ERROR' 'status' within the parseEval(const string&, SEXP) function), what() gives a "St9exception" exception.

I have two problems, the first more pressing than the second:

1a . After an initial Parse Error any subsequent call to parseEvalQ results in another Parse Error even if the argument is valid. Is the embedded R instance being corrupted in some way by the parse error?

1b . The RInside documentation recommends using Rcpp::Evaluator::run to handle R exceptions in C++ (which I suspect are being thrown somewhere within the R instance during the call to parseEval(const string&, SEXP), before it returns the error status 'PARSE_ERROR'). I have experimented trying to use this but can find no examples on the web of how to practically use Rcpp::Evaluator::run.

2 . In my program I re-route stdout and stderr (at C++ level) to the file descriptor of my tcp connection, any error messages from the RInside instance get sent to the console, however regular output does not. I send RInside the command 'sink(stderr(), type="output")' in an effort to re-route stdout to stderr (as stderr appears to be showing up in my console) but regular output is still not shown. 'print(command)' works but i'd like a cleaner way of passing stdout straight to the console as in a normal R shell.

Any help and/or thoughts would be much appreciated. A distilled version of my code is shown below:

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

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);  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶茶白久 2024-11-08 14:51:58

我几周前错过了这个,因为你没有使用“r”标签。

看来您正在重新实现 Simon 可信的 rserver。为什么不直接使用它呢?

否则,对于 Rcpp 问题,请考虑在我们的 rcpp-devel 列表中询问。

I missed this weeks ago as you didn't use the 'r' tag.

Seems like you are re-implementing Simon's trusted rserver. Why not use that directly?

Otherwise, for Rcpp question, consider asking on our rcpp-devel list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文