Cormac Long
2009-Dec-17 10:33 UTC
[R] issue with using rm: cannot generate on-the-fly list
Hello, I have the following problem when trying to use rm: In a top level script file I have a loop iterating over some index. The loop is not contained within a function, so the scope of variables declared in the loop is global. Within this loop I generate several variables which should be removed at the end of each iteration. To do this, I wrote a function to clean up the workspace. An example is included here: cleanUpWorkspace<-function() { #Remove key data sructures, if they have been declared: delList<-list() for(varname in c("rv1","rv2", "rv3", "rv4")) { if(exists(varname)) { delList<-append(delList, varname) } } if(length(delList)>0) { rm(list=delList, pos=-1) #rm(list=delList, envir=parent.env(environment())) #rm(list=delList, envir=globalenv()) } } Unfortunately, this fails to work - it aborts with the following error: Error in rm(list = delList, pos = -1) : invalid first argument if I use rm(list=delList, pos=-1) Or with the following error: Error in rm(list = delList, evir = parent.env(environment())) : ... must contain names or character strings if I use rm(list=delList, envir=parent.env(environment())) or rm(list=delList, envir=globalenv()) I get the same errors if I bypass rm entirely and use .Internal(remove(delList, globalenv(),FALSE)). I am using R version 2.10.0 running on a windows 7 box. I want to declare the clean-up within a function for design purposes. The full project is spread over several files and I am trying to keep the main loop as simple as possible. Sincerly, Cormac Long. [[alternative HTML version deleted]]
Romain Francois
2009-Dec-17 14:05 UTC
[R] issue with using rm: cannot generate on-the-fly list
The list argument of rm is supposed to be a character vector, as indicated in ?rm. not a list. You can do something like this: rm( list = Filter( exists, c("rv1","rv2", "rv3", "rv4") ) ) or, considering rm only warns when you attempt to remove objects, you could: suppressWarnings( rm( list = c("rv1","rv2", "rv3", "rv4") ) ) Romain On 12/17/2009 11:33 AM, Cormac Long wrote:> > Hello, > > I have the following problem when trying to use rm: > > In a top level script file I have a loop iterating over some index. The loop > is not contained within a function, so the scope of variables declared in > the loop is global. Within this loop I generate several variables which > should be removed at the end of each iteration. To do this, I wrote a > function to clean up the workspace. An example is included here: > > cleanUpWorkspace<-function() > { > #Remove key data sructures, if they have been declared: > delList<-list() > for(varname in c("rv1","rv2", "rv3", "rv4")) { > if(exists(varname)) { > delList<-append(delList, varname) > } > } > if(length(delList)>0) { > rm(list=delList, pos=-1) > #rm(list=delList, envir=parent.env(environment())) > #rm(list=delList, envir=globalenv()) > } > } > > Unfortunately, this fails to work - it aborts with the following error: > Error in rm(list = delList, pos = -1) : invalid first argument > if I use rm(list=delList, pos=-1) > > Or with the following error: > Error in rm(list = delList, evir = parent.env(environment())) : > ... must contain names or character strings > if I use rm(list=delList, envir=parent.env(environment())) or > rm(list=delList, envir=globalenv()) > > I get the same errors if I bypass rm entirely and use > .Internal(remove(delList, globalenv(),FALSE)). > > I am using R version 2.10.0 running on a windows 7 box. > > I want to declare the clean-up within a function for design purposes. The > full project is spread over several files and I am trying to keep the main > loop as simple as possible. > > Sincerly, > Cormac Long.-- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/HlX9 : new package : bibtex |- http://tr.im/Gq7i : ohloh `- http://tr.im/FtUu : new package : highlight
Duncan Murdoch
2009-Dec-17 14:08 UTC
[R] issue with using rm: cannot generate on-the-fly list
On 17/12/2009 5:33 AM, Cormac Long wrote:> Hello, > > I have the following problem when trying to use rm: > > In a top level script file I have a loop iterating over some index. The loop > is not contained within a function, so the scope of variables declared in > the loop is global. Within this loop I generate several variables which > should be removed at the end of each iteration. To do this, I wrote a > function to clean up the workspace. An example is included here: > > cleanUpWorkspace<-function() > { > #Remove key data sructures, if they have been declared: > delList<-list() > for(varname in c("rv1","rv2", "rv3", "rv4")) { > if(exists(varname)) { > delList<-append(delList, varname) > } > } > if(length(delList)>0) { > rm(list=delList, pos=-1) > #rm(list=delList, envir=parent.env(environment())) > #rm(list=delList, envir=globalenv()) > } > } > > Unfortunately, this fails to work - it aborts with the following error: > Error in rm(list = delList, pos = -1) : invalid first argument > if I use rm(list=delList, pos=-1)Your delList is a list. Despite the name of the argument, it doesn't want a list, it wants a character vector. So create it as character(0), and append elements using delList <- c(delList, varname). BTW, I'd recommend using the envir=globalenv() version of your rm() call, as it says most clearly that your function is messing with the global environment. But first I'd recommend not messing with the global environment; think of some other way to share data between functions, e.g. defining them all in the same environment, etc. Duncan Murdoch> > Or with the following error: > Error in rm(list = delList, evir = parent.env(environment())) : > ... must contain names or character strings > if I use rm(list=delList, envir=parent.env(environment())) or > rm(list=delList, envir=globalenv()) > > I get the same errors if I bypass rm entirely and use > .Internal(remove(delList, globalenv(),FALSE)). > > I am using R version 2.10.0 running on a windows 7 box. > > I want to declare the clean-up within a function for design purposes. The > full project is spread over several files and I am trying to keep the main > loop as simple as possible. > > Sincerly, > Cormac Long. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Maybe Matching Threads
- [LLVMdev] Error with instruction selection
- [03/15][PATCH] kvm/ia64: Add header files for kvm/ia64. V8
- [03/15][PATCH] kvm/ia64: Add header files for kvm/ia64. V8
- [syslinux:firmware] load_linux: dynamically calculate the cmdline region
- [LLVMdev] Patch - Allow calls that return i8 or i16. On SPU.