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
______________________________________________
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.