I would like to delete all global variables except a defined environment with variables in it.
Say I have the following:
my.env = new.env()
assign("a", 123, envir = my.env)
my.env$somestring = "Variable mat as vector string"
And also some global variables:
Age <- as.numeric(42)
HbA <- as.numeric(2)
WHR <- as.numeric(1.3)
I know I can delete all global variables but one with setdiff
setdiff(ls(),'Age')
#[1] "HbA" "my.env" "WHR"
but when I call the following I get an error. This is expected because my.env
is actually a pointer, if I am not mistaken
setdiff(ls(), my.env)
#Error in as.vector(x, mode) :
# cannot coerce type 'environment' to vector of type 'any'
How can I delete all variables but keep my.env
? (with the variables in it)
Try
to.del <- setdiff(ls(), "my.env")
rm(list = to.del)
# not relevant anymore
eval(parse(text = paste0("rm(", to.del, ")")))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.