From simon.urbanek at r-project.org  Wed Nov 16 00:45:03 2011
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Tue, 15 Nov 2011 18:45:03 -0500
Subject: [Rd] Question on parsing R code from C
In-Reply-To: <loom.20111115T225950-180@post.gmane.org>
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>
	<loom.20111115T225950-180@post.gmane.org>
Message-ID: <F4E5DACB-DBAC-46AE-B647-6A93CAA8FAB2@r-project.org>


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
> 
> 



[Rd] Question on parsing R code from C
Simon Urbanek