[R] lazy evaluation question

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jan 19 04:10:55 CET 2009


Note that

rm(i)
for(j in 1:4) F(j)

raises an error due to scoping issues.

On Sun, Jan 18, 2009 at 10:02 PM,  <markleeds at verizon.net> wrote:
> I've been going back to old difficult R-list "evaluation" emails that I save
>  in order to understand evaluation better and below still confuses me. Could
> someone explain why A) works and B) doesn't. A variant of below is in the
>  Pat's Inferno book also but I'm still not clear on what is happening.
> Thanks.
>
> f <- function() {
>  # FORCING i here doesn't help
>  i*i
> }
>
> F <- function(i) {
>  force(i)
>  print(f())
>  }
>
> A) THIS WORKS
> for ( i in 1:4 ) {
>  F(i)
> }
>
> B) THIS DOESN'T
> lapply(1:4,function(i) F(i))
>
> ______________________________________________
> 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