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

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Fri, 18 Apr 2008 07:49:17 -0400

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.

Duncan Murdoch

> or via lapply:
> 
> F <- function(i) { f <- function() i*i; print(f()) }
> lapply(1:4, F)
> 
> Often the sort of situation you discuss is really an attempt
> to use object oriented programming without realizing it.
> 
> The body of the loop is an object whose methods are the
> functions.  The proto package
> http://r-proto.googlecode.com
> can deal with such situations as can direct manipulation of
> R environments or the use of function bodies as wrappers,
> e.g.
> demo(scoping)
> 
> 
> 
> On Fri, Apr 18, 2008 at 2:00 AM, Peter Waltman <peter.waltman_at_gmail.com> wrote:

>> Hi Duncan -
>>
>> Thanks for the reply. Yeah, I understand what I'm doing is a bit weird, but
>> I'm actually calling a few functions w/in the for-loop that need the value
>> of the "i" var, and because I was a bit confused by the concept of
>> environments, I was hoping to avoid having to pass it in as an arg to each
>> function.
>>
>> Thanks,
>>
>> Peter
>>
>> On Thu, Apr 17, 2008 at 7:25 PM, Duncan Murdoch <murdoch_at_stats.uwo.ca>
>> wrote:
>>
>>
>>> On 17/04/2008 5:37 PM, Peter Waltman wrote:
>>>
>>>> Hi -
>>>>
>>>> I'm having a really hard time w/understanding R's get function, and
>>>> would
>>>> appreciate any help with this.
>>>>
>>>> Specifically, I'm using a for loop to call a function. I'd like the
>>>> function to have access to the variable being incremented in the
>>>> for-loop,
>>>> i.e.
>>>>
>>>> t.fn <- function() return( get( "i" ) )
>>>>
>>>> t.fn2 <- function() {
>>>> for ( i in 1:5 )
>>>> cat( t.fn(), "\n" )
>>>>
>>>> }
>>>>
>>>> However, I keep getting err msg's from the 'get' function about how it
>>>> can't
>>>> find the 'i' variable.
>>>>
>>>> I've tried various combinations w/in the get fn, i.e. passing inherits=T
>>>> (should be the default val according to R's help) and envir=sys.frame().
>>>>
>>>> As I understand it, 'get' should search the enclosing environments,
>>>> which I
>>>> assume would be the call-stack of the functions. If not, could someone
>>>> clarify?
>>>>
>>> The R Language manual describes this; R uses lexical scope. get() will
>>> search the calling environment, and its parent -- which in your case is
>>> where t.fn was defined -- and the parent of that environment, etc. The call
>>> stack is not searched.
>>>
>>> There are ways to look up the stack; passing envir=parent.frame() to get
>>> will work for your needs. But it's not a natural thing to do in R; it means
>>> your t.fn wouldn't work if it was called from anywhere but t.fn2. So why
>>> not define it there, and then i would be visible to it without this
>>> trickery? I.e.
>>>
>>> t.fn2 <- function() {
>>> t.fn <- function() return( i )
>>> for ( i in 1:5 )
>>> cat( t.fn(), "\n" )
>>> }
>>>
>>> Duncan Murdoch
>>>
>>>
>>>> Thanks,
>>>>
>>>> Peter
>>>>
>>>> p.s. when I define t.fn to be:
>>>>
>>>> t.fn<- function() {
>>>> for ( j in sys.nframe():0 ) cat( j,":",ls( sys.frame( j ) ), "\n" )
>>>> }
>>>> and call that in t.fn2(), I do eventually see the 'i' variable, i.e.
>>>>
>>>>> t.fn2()
>>>>>
>>>> 2 : j
>>>> 1 : i
>>>> 0 : test t.fn t.fn2 t.fn3
>>>>
>>>> [[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> 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.
>>>>
>>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>


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 - 11:51:59 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 - 13: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