[Rcpp-devel] Catching parse errors with RInside::parseEval()

Theodore Lytras thlytras at gmail.com
Wed Oct 10 11:30:30 CEST 2012

2012/10/10 Dirk Eddelbuettel <edd at debian.org>:
> On 10 October 2012 at 02:30, Theodore Lytras wrote:
> | However, if the R code passed to parseEval() contains syntax errors, my
> | application crashes with an error ("Error: Parse Error: <expression>.
> | Execution halted").
> I looked at that recently. The main reason it dies is that we are NOT in
> interactive mode -- so the parse error leads to immediate death by design.

Uh, bummer...

> So as a start you can turn a toggle on and force interactive mode.

Great! But how do I force interactive mode to my RInside instance?

> But in
> that case you end up in code branch that is underdeveloped:
>     switch (status){
>     case PARSE_OK:
>         // Loop is needed here as EXPSEXP might be of length > 1
>         for(i = 0; i < Rf_length(cmdexpr); i++){
>             ans = R_tryEval(VECTOR_ELT(cmdexpr, i), global_env_m, &errorOccurred);
>             // NB: we never actually get here if interactice is set to FALSE as it is above
>             if (errorOccurred) {
>                 Rf_error("%s: Error in evaluating R code (%d)\n", programName, status);
>                 UNPROTECT(2);
>                 return 1;
>             }
>             if (verbose_m) {
>                 Rf_PrintValue(ans);
>             }
>         }
>         mb_m.rewind();
>         break;
> and the following statements.  This needs work/extensions.

I browsed the code, and a few lines down I see:

   Rf_error("Parse Error: \"%s\"\n", line.c_str());
   return 1;

thus it appears that parse errors are already handled. Even better:
handled with a return code (I don't like exceptions that much). So I
guess the only problem is that we're not in interactive mode, correct?

What kind of work/extensions do you suggest this code needs? (I could
try to work a little on that, and learn a little about the insides of
R in the process).

Anyway, on a practical side, crashing the application everytime
parseEval() encounters an error would limit usage of RInside to
applications doing prespecified analyses, or running R code that is
specified at compile-time (and is thus probably free of errors).

If one's application needs to execute "arbitrary" R code, as mine
does, this is a real showstopper. If it's only a matter of turning
interactive mode on, that would be great. Otherwise, I would like to
(try to) help bring this capability to RInside or improve it.

> Alternatively, you could tru to wrap each R expression that could fail in its
> own try/catch block. I have not tried that.

Sounds a little cumbersome, but I'll try that too as an alternative...

> Dirk
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com

More information about the Rcpp-devel mailing list