From krunal.rao78 at gmail.com  Tue Nov 15 23:39:09 2011
From: krunal.rao78 at gmail.com (KR)
Date: Tue, 15 Nov 2011 22:39:09 +0000
Subject: [Rd] Question on parsing R code from C
References: <loom.20111108T203022-872@post.gmane.org>
	<9465BE63-EA88-4BE2-8D1C-9E33BE8AA45D@r-project.org>
	<loom.20111108T224700-655@post.gmane.org>
	<3D4F776D-4D5D-4C39-90B3-12DF09C5C6BA@r-project.org>
	<loom.20111109T001139-68@post.gmane.org>
	<AF0744B1-C119-4BF1-99D0-9DBA31E734F9@r-project.org>
	<loom.20111111T000844-348@post.gmane.org>
	<84A85DE6-2914-4C8D-834A-07DE5820B5AD@r-project.org>
Message-ID: <loom.20111115T225950-180@post.gmane.org>

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.



[Rd] Question on parsing R code from C
KR