Hi Mark, I think your suggestion to call rm(i) before or after calling f1 so that the local variable is removed, is the most straighforward solution to my problem. I was trying to implement some efficient C-style techniques in R, but the scoping principle in R is quite different than C.
Thank you very much for the help! Best Regards Ivo --- On Fri, 8/7/09, markle...@verizon.net <markle...@verizon.net> wrote: > From: markle...@verizon.net <markle...@verizon.net> > Subject: Re: Re: Re: [R] A question regarding R scoping > To: idc...@yahoo.com > Date: Friday, August 7, 2009, 6:57 PM > Hi Ivo: There's something wrong > with my mailer so I'm not including the R-list on this > email. I found your question > interesting because > scope in R has always been befuddling to me. But, I'm > still not following you because > if you run below , i > does get changed ? I set it to 100 in the .GlobalEnv > initially. Then I delete it. Then I > run f2 and it's > equal to 1 inside f2 and equal to 1 in .GlobalEnv. Is that > not what you want ? > > > 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) > } > > i <- 100 > rm(i) > f2(1:20) > print(i) > > > > On Aug 7, 2009, Ivo Shterev > <idc...@yahoo.com> wrote: > 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, markle...@verizon.net <markle...@verizon.net> wrote: > > > From: markle...@verizon.net <markle...@verizon.net> > > > Subject: Re: Re: [R] A question regarding R scoping > > To: murd...@stats.uwo.ca > > Cc: > idc...@yahoo.com, r-help@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 > > <murd...@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.honey...@gmail.com> > > wrote: > > > > > > > > > >> From: Steve > > Lianoglou <mailinglist.honey...@gmail.com> > > >> Subject: Re: [R] A question regarding R > > scoping > > >> To: "Ivo > Shterev" <idc...@yahoo.com> > > > >> > > Cc: r-help@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@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@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@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.