[R] delayedAssign list members

Philippe Grosjean phgrosjean at sciviews.org
Tue Mar 4 09:47:20 CET 2014


> delayedAssign("foo$bar", 2)
> 
> foo$bar  # 1
> 
> `foo$bar`  # 2

Yes, you assign to the new variable `foo$bar`, not to bar component of the foo variable!
You can use an environment for doing what you want:
e <- new.env()
e$bar <- 1
delayedAssign("bar", 2, assign.env = e)
e$bar

But notice also the eval.env= argument of delayedAssign()… Depending on what you want do, you have to provide this also. Note also that the use of environments, as well as playing with lazy evaluation are advanced techniques you should really well understand, or you will encounter many other surprises. Take care, for instance, about how you save and reload, or dump(), etc. environment objects, if that happens to be something you want do too.

So, unless you really find an advantage with this, you would be better to continue using plain list() and try to forget about delayedAssign().

Best,

Philippe Grosjean


On 04 Mar 2014, at 09:23, "Shan Huang" <openhuangs at gmail.com> wrote:

> I am fascinated by lazy evaluation mechanism provided by delayedAssign. Like
> 
> 
> 
> delayedAssign("foo", { 
> 
>  Sys.sleep(1)  # or any other time consuming operations
> 
>  1
> 
> }
> 
> 
> 
> Time consuming operations will be evaluated only if object "foo" is used.
> 
> But when I try: 
> 
> 
> 
> foo <- list()
> 
> foo$bar <- 1
> 
> delayedAssign("foo$bar", 2)
> 
> foo$bar  # 1
> 
> `foo$bar`  # 2
> 
> 
> 
> Shows that delayedAssign can't be applied to list members, instead symbol
> with "$" will be assigned.
> 
> What I actually want is a lazy evaluation wrapper for large complex
> hierarchical datasets. Like:
> 
> 
> 
> MyDatasets <- list(
> 
>  Dataset1 = complexData1
> 
>  Dataset2 = complexData2
> 
> )
> 
> 
> 
> There is no need for loading whole datasets when I only use Dataset1. And I
> want to implement some 
> 
> incremental updating mechanism fetching data from remote host.
> 
> Can any one give me some tips to implement this?
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list