At the end of a for loop its variables are still present: for (i in 1:10) { x <- vector(length=100000000) } ls() will print "i" and "x". this means that at the end of the for loop body I have to write rm(x) gc() is there a more elegant way to handle this? Thanks. -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://camera.org http://palestinefacts.org http://iris.org.il http://www.PetitionOnline.com/tap12009/ http://truepeace.org Computers are like air conditioners: they don't work with open windows!
On Tue, Aug 28, 2012 at 1:29 PM, Sam Steingold <sds at gnu.org> wrote:> At the end of a for loop its variables are still present: > > for (i in 1:10) { > x <- vector(length=100000000) > } > ls() > > will print "i" and "x". > this means that at the end of the for loop body I have to write > > rm(x) > gc() > > is there a more elegant way to handle this?Wrap the loop in local() scope perhaps? This might get tricky if you need to save some results from the loop, but I think you're ok if they are initialized outside the loop and you use super-assignment. Almost always you shouldn't need manual garbage collection. Something like: # Terribly impractical, but gets the point across y <- numeric(100) local({ for(i in 1:10){ x <- rnorm(10) y[10*(i-1) + 1:10] <<- x } }) print(x) # Error print(y) Doubt that works out to be significantly more elegant however. Michael> > Thanks. > > -- > Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 > http://www.childpsy.net/ http://camera.org http://palestinefacts.org > http://iris.org.il http://www.PetitionOnline.com/tap12009/ http://truepeace.org > Computers are like air conditioners: they don't work with open windows! > > ______________________________________________ > 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.
Hello, Maybe local(). Continue your example with #?local local(for (i in 1:10) { x <- vector(length=100000000) }) ls() # not 'i' nor 'x' Hope this helps, Rui Barradas Em 28-08-2012 19:29, Sam Steingold escreveu:> At the end of a for loop its variables are still present: > > for (i in 1:10) { > x <- vector(length=100000000) > } > ls() > > will print "i" and "x". > this means that at the end of the for loop body I have to write > > rm(x) > gc() > > is there a more elegant way to handle this? > > Thanks. >
Perhaps I'm dense, but huh*? -- Bert *e.g. What are you trying to do? R does it's own garbage collection -- why do you think you need it? And, as a general comment which may or may not be applicable, if you create variables in a function they are local only to the function -- they disappear once the function returns. But I'm not sure this is relevant to your query. On Tue, Aug 28, 2012 at 11:29 AM, Sam Steingold <sds at gnu.org> wrote:> At the end of a for loop its variables are still present: > > for (i in 1:10) { > x <- vector(length=100000000) > } > ls() > > will print "i" and "x". > this means that at the end of the for loop body I have to write > > rm(x) > gc() > > is there a more elegant way to handle this? > > Thanks. > > -- > Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 > http://www.childpsy.net/ http://camera.org http://palestinefacts.org > http://iris.org.il http://www.PetitionOnline.com/tap12009/ http://truepeace.org > Computers are like air conditioners: they don't work with open windows! > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
On Aug 28, 2012, at 1:29 PM, Sam Steingold <sds at gnu.org> wrote:> At the end of a for loop its variables are still present: > > for (i in 1:10) { > x <- vector(length=100000000) > } > ls() > > will print "i" and "x". > this means that at the end of the for loop body I have to write > > rm(x) > gc() > > is there a more elegant way to handle this? > > Thanks.It is not clear why you want 'x' to be created and overwritten 10 times, but perhaps I am missing something. You end up with 1 'x' after the loop, not 10 objects. More generally, I can think of a few options, all of which use functions to create your desired object, so that there are no other objects created during execution. Use sapply() rather than a for() loop: NewObject <- sapply(seq(10), DoSomethingHere...) Use replicate(), which will return an array by default: NewObject <- replicate(10, DoSomethingHere...) Or...just create a function that takes requisite arguments and runs the for() loop within the function body and returns the object you actually need. That way, any variables created within the scope of the function are gone when the function exits. Regards, Marc Schwartz
On 28/08/2012 2:29 PM, Sam Steingold wrote:> At the end of a for loop its variables are still present: > > for (i in 1:10) { > x <- vector(length=100000000) > } > ls() > > will print "i" and "x". > this means that at the end of the for loop body I have to write > > rm(x) > gc() > > is there a more elegant way to handle this? > >You should put most code in functions, so i and x will be locals and will be automatically collected when your function returns. You rarely need to call gc() explicitly; R will do automatic collections. There are exceptions to the automatic collection. For example, if your return value is a function, its environment will include all the locals, and they will persist as long as the returned function does. If you have big local values like your x and you don't need them as part of the environment of your function, then you might want to remove them explicitly. The only reason I ever call gc() is for debugging, but others may have good reasons for asking space to be freed sooner rather than later. Duncan Murdoch