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

Dirk Eddelbuettel edd at debian.org
Wed Oct 10 01:55:25 CEST 2012


Hi Theodore,

On 10 October 2012 at 02:30, Theodore Lytras wrote:
| Hello all,
| 
| I am using RInside in my application to execute some R code with parseEval() 
| and parseEvalQ().
| 
| However, if the R code passed to parseEval() contains syntax errors, my 
| application crashes with an error ("Error: Parse Error: <expression>. 
| Execution halted").
| 
| I tried putting the parseEval() call inside a try...catch block (as per the 
| RInside examples) but no exception seems to be thrown.
| 
| Isn't there any way to detect these parse errors without crashing the 
| application?

A classic yes/no/maybe.

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.

So as a start you can turn a toggle on and force interactive mode. 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.

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

Dirk

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


More information about the Rcpp-devel mailing list