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

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Fri, 18 Apr 2008 09:25:54 -0400

On 4/18/2008 9:19 AM, Gabor Grothendieck wrote:
> On Fri, Apr 18, 2008 at 7:49 AM, Duncan Murdoch <murdoch@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?

It doesn't. Luke Tierney's experimental byte code compiler might do optimizations like that, but the regular evaluator does very little inspection of what it is evaluating.

Duncan Murdoch



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 - 14:23:32 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 - 14:30:30 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