[R] Help with using 'get' function and variable scope
Duncan Murdoch
murdoch at stats.uwo.ca
Fri Apr 18 15:25:54 CEST 2008
On 4/18/2008 9:19 AM, Gabor Grothendieck wrote:
> 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?
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
More information about the R-help
mailing list