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

2014-02-06 Thread Romain Francois
Hi Antonio, 

This is about how R evaluation works. Might not just be what you think it is. 

When you use Rcpp and therefore .Call things are evaluated fairly early, at 
least earlier than what would happen with R, etc … you can reproduce what R 
does by using promises. For example this work in Rcpp11: 

// [[Rcpp::export]]
RawVector rcpp_serialize(Dots dots){
  Function serialize(serialize) ;
  return serialize( dots.promise(0), R_NilValue ) ;
}

Dots has not been implemented in Rcpp, but would not be too hard to port: 
https://github.com/romainfrancois/Rcpp11/blob/master/inst/include/Rcpp/Dots.h

But it would also need to fix Rcpp’s handling of recursive promises (i.e. 
promise of a promise of a promise). 



Alternatively, for a solution based on Rcpp, you can look in dplyr. The 
.data_dots function has most of the R-level logic
https://github.com/hadley/dplyr/blob/master/R/manip-df.r

And the C++ logic is mostly in DataDots.h
https://github.com/hadley/dplyr/blob/master/inst/include/tools/DataDots.h

Romain

Le 6 févr. 2014 à 02:07, Antonio Piccolboni anto...@piccolboni.info a écrit :

 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
 ___
 Rcpp-devel mailing list
 Rcpp-devel@lists.r-forge.r-project.org
 https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

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

2014-02-05 Thread Antonio Piccolboni
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
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

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

2014-02-05 Thread Dirk Eddelbuettel

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 | e...@debian.org | http://dirk.eddelbuettel.com
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel