Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing object from parent environment using rm()

Tags:

r

I am trying to remove an object from the parent environment.

rm_obj <- function(obj){
  a <-deparse(substitute(obj))
  print (a)
  print(ls(envir=sys.frame(-1)))  
  rm(a,envir=sys.frame(-1))
}
> x<-c(1,2,3)
> rm_obj(x)
[1] "x"

[1] "rm_obj" "x"    
Warning message:
In rm(a, envir = sys.frame(-1)) : object 'a' not found

This will help clarify my misunderstanding regarding frames.

like image 213
user151410 Avatar asked Mar 21 '10 02:03

user151410


2 Answers

Your frames are right I think, it's just that rm is trying to remove a itself instead of evaluating a to get the quoted name of the variable to remove. Use the list parameter instead:

rm(list=a,envir=sys.frame(-1))
like image 129
Jonathan Chang Avatar answered Sep 28 '22 14:09

Jonathan Chang


The following code works for me.

myEnv = new.env()
assign('xx', 5, envir=myEnv)
get('xx', envir=myEnv)
rm('xx', envir=myEnv)
like image 43
Pabitra Dash Avatar answered Sep 28 '22 16:09

Pabitra Dash