[R] Help with using 'get' function and variable scope

Gabor Grothendieck ggrothendieck at gmail.com
Fri Apr 18 15:19:41 CEST 2008


On Fri, Apr 18, 2008 at 7:49 AM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
> On 18/04/2008 7:27 AM, Gabor Grothendieck wrote:
> > If you define your functions in the loop you can it directly
> > since then the scoping rules work in your favor:
> >
> > for(i in 1:4) {
> >  f <- function() i*i
> >  print(f())
> > }
> >
>
> f doesn't need to be in the loop, it just needs to be defined in the same
> environment as i was defined in.  Loops in R don't create new local frames.
>

That's a good point. Do you know if R will optimize out constant expressions
from a loop?



More information about the R-help mailing list