[R] A question regarding R scoping

Vitalie S. vitosmail at rambler.ru
Sat Aug 8 12:07:01 CEST 2009


You have to understand the difference between the lexical scope of a  
function (which gives the search path for variables) and call stack (which  
is a path of function calls). This gives an enormous flexibility in  
programming, perhaps at the cost of confusing some people.

See http://cran.r-project.org/doc/manuals/R-intro.html#Scope
http://cran.r-project.org/doc/manuals/R-lang.html#Scope
and
http://cran.r-project.org/doc/manuals/R-lang.html#Scope-of-variables

Here is a slightly corrected Steve's example:

f1 <- function(name, envir=parent.frame()){
#`name` is a string here, objects are referenced by their names in R and  
not pointers
     assign(name, 1, envir=envir)
     #assignment is made in the parent.environment which is an environment  
of calling function
}

f2 <- function(n){
     i <- n
     f1("i") #pass the name of an object and not an object as you did in  
your original example
     print(i)
}

Now,


> i <- 10
> f1("i")
> i
[1] 1
> f2(2323)
[1] 1  #not 2323
>

On Fri, 07 Aug 2009 00:02:35 +0200, Ivo Shterev <idc318 at yahoo.com> wrote:

>
> Hi,
>
> Perhaps I have to rephrase a bit my question. If we have the following:
>
> i = 10
> f1 = function(i){
> i <<- 1
> }
>
> after calling f1, the value of i becomes 1. Now, suppose that f1 is  
> called in another function f2, and i is initialized in f2 as well, i.e:
>
> f2 = function(n){
> i = n
> f1(i)
> }
>
> The intention is, after executing f2, i=1 (not i=n).
>
>
>
>
> --- On Thu, 8/6/09, Steve Lianoglou <mailinglist.honeypot at gmail.com>  
> wrote:
>
>> From: Steve Lianoglou <mailinglist.honeypot at gmail.com>
>> Subject: Re: [R] A question regarding R scoping
>> To: "Ivo Shterev" <idc318 at yahoo.com>
>> Cc: r-help at r-project.org
>> Date: Thursday, August 6, 2009, 10:23 PM
>> Howdy,
>>
>> On Aug 6, 2009, at 4:11 PM, Ivo Shterev wrote:
>>
>> > Hi,
>> >
>> > The intention is that after executing f2, the value of
>> i to become 1.
>> >
>> > f1 = function(i){i = 1}
>> >
>> > f2 = function(n){  i = length(n)
>> > f1(i)
>> > print(i)}
>> >
>> > i.e. f2 should print 1, not length(n).
>>
>> Yeah, you can using parent.frame()'s and such:
>>
>> f1 <- function(i) assign('i', 10, envir=parent.frame())
>> f2 <- function(n) {
>>   i <- length(n)
>>   f1(i)
>>   print(i)
>> }
>>
>> R> f2(1:20)
>> [1] 10
>>
>> Honestly, this just smells like a *really* bad idea, though
>> ... just have f1() return a value that you use in f2.
>>
>> -steve
>>
>> --
>> Steve Lianoglou
>> Graduate Student: Computational Systems Biology
>>   |  Memorial Sloan-Kettering Cancer Center
>>   |  Weill Medical College of Cornell University
>> Contact Info: http://cbio.mskcc.org/~lianos/contact
>>
>>
>
>
>
>
> ______________________________________________
> 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.


--




More information about the R-help mailing list