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

Antonio Piccolboni antonio at piccolboni.info
Thu Feb 6 02:07:10 CET 2014


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


Antonio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140205/833e181a/attachment.html>


More information about the Rcpp-devel mailing list