From simon.urbanek at r-project.org  Fri Nov 11 16:08:53 2011
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Fri, 11 Nov 2011 10:08:53 -0500
Subject: [Rd] Question on parsing R code from C
In-Reply-To: <loom.20111111T000844-348@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>
Message-ID: <84A85DE6-2914-4C8D-834A-07DE5820B5AD@r-project.org>


On Nov 10, 2011, at 6:24 PM, KR wrote:

> First of all thanks a lot to you both for all the replies, they have been of 
> great help to me!
> 
> I got the basic embedding running, however I still have some issues to solve in 
> order to complete the interface to R I am working on:
> 
> 1. I initialize with Rf_initEmbeddR().
> - Is there a way to have more than one R "state" (per single thread)? Or is 
> everything necessarily global?

There are no threads in R. You have only one, global instance of R.


> - I also read somewhere that the fpu settings may be touched. Is there a way to 
> make sure that this is not the case? Is this related to fpu_setup()?
> 

All following answers assume that you're not running REPL. If you did, you would get all the errors on the console callback, so I'm assuming that's not what you want.


> 2. I create a string and parse it via Rf_mkString() and R_parseVector() (yes I 
> protect/un-protect the SEXPs).
> - Is it possible to obtain the precise error message, like "unexpected symbol 
> in..." (as would be reported by R.exe) in case of error as a const char* string?

AFAIR you have to evaluate parse(text=...) for that, there is no C-level access to parser errors.


> - If I pass a wrongly escaped string (for instance 'ggsave("C:\gtest.png")', 
> please notice the missing \) I get on stderr: Error '\g' is an unrecognized 
> escape and I get a crash. This does not happen if for instance i try to parse 
> 'rnorm(10a10)' in which case I get the error flag and the NULL ptr return. I 
> suspect I need to initialize something / set a callback to avoid this but I am 
> not sure...
> 

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.


> 3. I eval with R_tryEvalSilent().
> - Again, is it possible to obtain the precise error message in case of 
> evaluation error as would be reported from R.exe?
> 

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.


> 4. Regarding the returned result of evaluation.
> - What is the correct way to obtain a const char* string representing the result 
> as would be "printed" in the R shell by executing a command (for instance 
> "summary(c(1,2,3))") ?

See ?capture.output


> - Why is the return type (as reported from typeof in R and TYPEOF in C) of 
> "summary(c(1,2,3))" a double?
> 

Because the constants 1, 2 and 3 are all doubles. For example 1L is an integer and "1" is a string.

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

Cheers,
Simon



> Any help / feedback on these issues would be greatly appreciated.
> 
> Thanks again.
> 
> ______________________________________________
> 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