[Rd] Question on parsing R code from C

Simon Urbanek simon.urbanek at r-project.org
Fri Nov 11 16:08:53 CET 2011


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



More information about the R-devel mailing list