[Rcpp-devel] calling and R function from rcpp and evaluation

Dirk Eddelbuettel edd at debian.org
Thu Feb 6 03:16:57 CET 2014


On 5 February 2014 at 17:07, Antonio Piccolboni wrote:
| Hi,
| I was wondering why I can serialize a call such as in
| 
| serialize(call("ripley"), NULL) 
| [1] 58 0a 00 00 00 02 00 03 00 02 00 02 03 00 00 00 00 06 00 00 00 01 00 04 00
| 09 00 00 00 06 72 69 70 6c 65 79 00 00 00 fe
| 
| but if I try from C++ the call gets evaluated
| 
|  library("Rcpp")
| 
|  rcpp.serialize = cppFunction(code="RObject my_serialize(RObject x){Function
| r_serialize(\"serialize\"); return r_serialize(x, R_NilValue);}")
| 
|  rcpp.serialize(10)
|  [1] 58 0a 00 00 00 02 00 03 00 02 00 02 03 00 00 00 00 0e 00 00 00 01 40 24
| 00 00 00 00 00 00
| # easy things work
| 
|  rcpp.serialize(call("ripley"))
| Error: could not find function "ripley"
| # wants to evaluate it
| 
|  unserialize(rcpp.serialize(call("sqrt", 2)))
| [1] 1.414214
| # How is one to serialize the call itself
| 
|  rcpp.serialize(list(call("ripley")))
|  [1] 58 0a 00 00 00 02 00 03 00 02 00 02 03 00 00 00 00 13 00 00 00 01 00 00
| 00 06 00 00 00 01 00 04 00 09 00 00 00 06 72 69 70 6c 65 79 00 00 00 fe
| #wrapping in a list is enough to stop evaluation
| 
|  eval(call("ripley"))
| Error in eval(expr, envir, enclos) : could not find function "ripley"
|  eval(list(call("ripley")))
| [[1]]
| ripley()
| 
| #OK that's consistent
| 
| 
| Could anyone enlighten me on the evaluation mechanism? Not saying it's broken,
| but certainly my expectations were off. Thanks

Did you look at eg "Section 4: Function Calls" in the Rcpp-introduction vignette?
Besides Rcpp::Function you could also try the Rcpp::Language class, which is
shown there.  We now also have an Rcpp::Promise class, but I have not yet used it. 

And in all those years with Rcpp I don't think I have used, or thought I
needed, delayed evaluation at the C++ level.  But I suspect that for example the
recent dplyr package does something along those lines, so you could look there.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list