[R] A question regarding R scoping

Ivo Shterev idc318 at yahoo.com
Fri Aug 7 18:36:02 CEST 2009


Dear All,

Sorry for the introduced confusion. My question is to have a function (in my case f1) that just takes an argument and modifies it (no copies, no returns). This can be done by:

f1 = function(i){i <<- 1}

Then this function is called by another function (in my case f2) that just initializes the above mentioned argument and calls f1, like this

f2 = function(n){
##whatever initialization on i
f1(i)
print(i)
}

Obviously in my code example f1 "loses" its ability to modify its argument, so the question is how to modify f2 so that it prints out 1. 

-ivo

--- On Fri, 8/7/09, markleeds at verizon.net <markleeds at verizon.net> wrote:

> From: markleeds at verizon.net <markleeds at verizon.net>
> Subject: Re: Re: [R] A question regarding R scoping
> To: murdoch at stats.uwo.ca
> Cc: idc318 at yahoo.com, r-help at r-project.org
> Date: Friday, August 7, 2009, 8:33 AM
> Hi Gabor, Steve, Eric and Duncan: I
> played around with below because I've always find scope
> in R difficult and I think the confusion with the question
> is arising because it's not clear whether the person who
> asked it wants i to be changed in f2 or in the global
> environment. 
> 
> I didn't know this before I
> started playing but the first f1 below is quite different
> from the second and the third ( which are identical ) which
> I'm sure all of you are well aware of. But that's
> why there's confusion with the question I think.  I
> apologize if this email ends up having a lot of control
> A's in it. I still haven't cracked that problem
> yet.
> 
> 
> f1 <- function(i)
> assign('i', 1, envir=parent.frame())
> f1 <-
> function(i) assign('i', 1, envir=.GlobalEnv)
> f1
> <- function(i) { i <<- 1 }
> 
> 
> f2
> <- function(n) {
> i <- length(n)
> f1(i)
> print(i)
> }
> 
> f2(1:20)
> print(i)
> rm(i)
> 
> 
> 
> 
> 
> On Aug 6,
> 2009, Duncan Murdoch
> <murdoch at stats.uwo.ca> wrote: Ivo
> Shterev 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).
> >   
> 
> That is what you get. 
> What is the question?
> 
> Duncan Murdoch
> >
> >
> >
> > --- 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.
> >
> 
> ______________________________________________
> 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