[Rd] Question on parsing R code from C

Simon Urbanek simon.urbanek at r-project.org
Wed Nov 16 00:45:03 CET 2011


On Nov 15, 2011, at 5:39 PM, KR wrote:

> Simon Urbanek <simon.urbanek <at> r-project.org> writes:
>> AFAIR you have to evaluate parse(text=...) for that, there is no C-level 
> access to parser errors.
> 
> Yes that did it, thanks!
> 
>> If you get a crash, you're not setting up you R correctly. If your R quits 
> then you are in non-interactive mode
>> and you didn't setup an error handler.
> 
> I am really getting a crash for strings with incorrect escapes like when 
> evaluating:
> parse(text='ggsave("C:\test.png")')
> Using R.exe does not result in a crash.
> It's really puzzling that all the other parse errors (like rnorm(10a10)) or 
> evaluation errors (like idontexists(10)) correctly results in an error string an 
> no crash. Code that executes correctly works fine as well.
> Do you have any suggestion on what may be causing the issue? 
> 

Not without seeing the actual code. (And details such as which platform you're on).
Note that setup_Rmainloop() is the last to set the SETJMP context target so you should make sure the stack is still present after it finished (i.e. you can't call it from a sub-function - which Rf_initEmbeddedR does) otherwise error without an enclosing context will crash. Or to put it other way round, create a context around your parse/eval code (probably the easiest way to do that is to use R_ToplevelExec).


> I am initializing R with:
> Rf_initEmbeddedR passing progsname, --vanilla and --slave
> 
> And I am parsing/evaluating with:
> Rf_mkString (protected) passing capture.output(eval(parse(text="<code>")))
> (I also tried with allocVector + Rf_mkChar with no difference)
> Followed by:
> R_ParseVector(r_cmd, -1, parse_err, R_NilValue) (protected) with r_cmd given by 
> the SEXP returned above
> And by:
> R_tryEvalSilent(VECTOR_ELT(r_expr,i), R_GlobalEnv, eval_err) (protected) 
> iterating along r_expr which is given by the SEXP returned above.
> 
> I don't perform any other operation at all. R_Interactive is true.
> 
>> I prefer using Rf_eval() and try(..., silent=TRUE) - you can check on the 
> class of the result to see if there
>> was an error.
> 
> I actually find it easier (less code) to use R_tryEvalSilent followed by 
> R_curErrorBuf in case of error. Are they going to be deprecated?
> 
>> BTW: you are asking a lot of questions the are answered in the Rserve FAQ 
> (since what you do is exactly what
>> Rserve provides) so you may want to have a quick look:
>> http://rforge.net/Rserve/faq.html
> 
> Thanks a lot, that helped as well!
> 
> Plotting works as well but after the window is created it "freezes", cannot be 
> closed without passing "dev.off()" command. I understand this is expected 
> behavior and that it cannot be changed using Rf_initEmbeddedR and 
> Rf_endEmbeddedR.
> 
> The only problem I am having (aside from the wrongly escaped string crashes) is 
> that if I call:
> Rf_initEmbeddedR (to open the global R state) followed by Rf_endEmbeddedR (to 
> close the global R state) and then I try to open the global state again with 
> Rf_initEmbeddedR I get:
> "Lost warning messages" on stderr and calling other R functions result in hang.
> What am I doing wrong?
> 

Nothing. You can initialize R only once.

Cheers,
Simon


> Thanks a lot for all the suggestions so far, I have made a lot of progress.
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 



More information about the R-devel mailing list