[Rcpp-devel] Catching parse errors with RInside::parseEval()
Dirk Eddelbuettel
edd at debian.org
Wed Oct 10 13:15:33 CEST 2012
On 10 October 2012 at 12:30, Theodore Lytras wrote:
| 2012/10/10 Dirk Eddelbuettel <edd at debian.org>:
| [snip]
| > 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").
| [snip]
| > 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?
There is a boolean in RInside.cpp that you need to flip. Eventually, we
could (if it is useful) support a flag at instantiation of the RObject.
| > 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:
|
| case PARSE_ERROR:
| Rf_error("Parse Error: \"%s\"\n", line.c_str());
| UNPROTECT(2);
| return 1;
| break;
|
| 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?
In theory. In practice, I think, it just doesn't get there.
| 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).
I think it is pretty much this switch block we need to get to work properly.
Dirk
| 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
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the Rcpp-devel
mailing list