[Rd] Question on parsing R code from C
KR
krunal.rao78 at gmail.com
Tue Nov 15 23:39:09 CET 2011
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?
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?
Thanks a lot for all the suggestions so far, I have made a lot of progress.
More information about the R-devel
mailing list