Peng Yu
2010-Feb-01 01:56 UTC
[R] Is there a way to make blocks of code independent from each other?
I'm wondering if there is a way to make blocks of code independent from each other. Please see the following example for what I mean. x=1 ## is there a way to make the following assignment not affect the above x? ## in C++, I can use {} to make the effect local. Is there an equivalent construct in R? x=3 <do something with the new value of x> ### print(x) # I don't want the value of x change to 3.
Jeff Laake
2010-Feb-01 02:04 UTC
[R] Is there a way to make blocks of code independent from each other?
Put in a function what you would have put in the {} and execute the function x=1 myf=function() { x=3 do something with new value of x ... } myf() print(x) # it will be 1 On 1/31/2010 5:56 PM, Peng Yu wrote:> I'm wondering if there is a way to make blocks of code independent > from each other. Please see the following example for what I mean. > > x=1 > > ## is there a way to make the following assignment not affect the above x? > ## in C++, I can use {} to make the effect local. Is there an > equivalent construct in R? > x=3 > <do something with the new value of x> > ### > > print(x) # I don't want the value of x change to 3. > > ______________________________________________ > 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. >
jim holtman
2010-Feb-01 02:11 UTC
[R] Is there a way to make blocks of code independent from each other?
Use 'local'> x <- 1 > local({+ x <- 3 + print(x) + }) [1] 3> print(x)[1] 1> >On Sun, Jan 31, 2010 at 8:56 PM, Peng Yu <pengyu.ut at gmail.com> wrote:> I'm wondering if there is a way to make blocks of code independent > from each other. Please see the following example for what I mean. > > x=1 > > ## is there a way to make the following assignment not affect the above x? > ## in C++, I can use {} to make the effect local. Is there an > equivalent construct in R? > x=3 > <do something with the new value of x> > ### > > print(x) # I don't want the value of x change to 3. > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?