1

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);  
}
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
C Noble
  • 11
  • 2

1 Answers1

1

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725