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

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri, 18 Apr 2008 09:19:41 -0400

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?



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. Received on Fri 18 Apr 2008 - 13:22:42 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 18 Apr 2008 - 15:30:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive