[Rcpp-devel] Checking for a promise

Dirk Eddelbuettel edd at debian.org
Sun Dec 26 18:07:02 CET 2010


On 25 December 2010 at 17:22, Gabor Grothendieck wrote:
| It seems that there are no facilities for this in Rcpp -- at least I
| haven't found them.  It seems one has to go one level lower to do

Patches are always welcome, particularly if they come with use cases
motivating them ("Does it make things easier? Or faster? Or at least make
them possible?")  as well as unit tests and documentation.

| this.  The code below works.  It passes an environment and name and
| returns the type code.  I think this sort of functionality was
| intentionally left out of R itself and if one were just trying to
| emulate R then I can understand it not being there but since Rcpp is
| intended for performance it might be a good idea to give this sort of
| access to promises without forcing the program to use the lower level
| facilities.

Well, why?  I do not use delayedAssign() all that much in R; I haven't needed
it in Rcpp yet at all.  When I look up "Promise" in, say, Chambers (2008) I
mostly see references to the R Evaluator, something we do not muck with a lot
in Rcpp.
 
| The main functions that would be nice would be:
| - to determine if an object is a promise
| - to extract, set and replace its components (i.e. the expression and
| the environment).
| - also the ability to copy a promise from one environment to another
| without forcing it
| 
| library(Rcpp)
| library(inline)
| f <- cxxfunction(signature(env="environment", nm = "character"),
|        body=' Environment e(env);
|           std::string s = as<std::string>(nm);
|           SEXP res = Rf_findVarInFrame( e, Rf_install(s.c_str())  ) ;
|           if( res == R_UnboundValue ) return R_NilValue ;
|           return wrap(TYPEOF (res)); ',
|        plugin="Rcpp")
| 
| # create promise
| delayedAssign("prom", 3)
| 
| # returns 5 which is the code for a promise
| f(.GlobalEnv, "prom")

It's a nice start. Maybe you want to build on that. Keep us posted.

Dirk

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


More information about the Rcpp-devel mailing list