[Rd] expressions as active bindings?

Vitalie S. spinuvit.list at gmail.com
Thu Oct 28 11:49:52 CEST 2010


Hello everyone!

Would it be possible some day to use expressions as active bindings?
Something like

  makeActiveExpression("foo", Expr0,  env)

where Expr0 is executed in _env_ whenever "foo" is refereed.

The motivation for this question is fourfold:
 - need to use <<- if the active binding is a function,
 - need to create objects in the environments beforehand, to make sure <<- does
 not assign them in a wrong place.
 - need to set the environment for function
 - active expressions would be faster (would not require the function invocation)

 
So instead of :

te <- new.env()
te$tempa <- 0
myfun <- function(x){tempa <<- 33}
environment(myfun) <- te
makeActiveBinding("a", myfun, te)

one would just use:

makeActiveExpression("a", tempa <- 33) ? 



I noticed that active bindings are very fast:

te$b <- expression(tempb <- 55)
system.time(evalq(for(i in 1:1e5) a, te))
   user  system elapsed 
   0.14    0.00    0.14 
system.time(evalq(for(i in 1:1e5) eval(b), te))
   user  system elapsed 
   0.57    0.02    0.57 

So, I would assume the active expressions would be even faster.

Thanks,
Vitalie.



More information about the R-devel mailing list