Removing an object from the parent using rm ()

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.

+4
source share
2 answers

Your frames are correct, I think it’s just that rm trying to delete a itself, and not evaluate a to get the specified variable name to delete. Use the list parameter instead:

 rm(list=a,envir=sys.frame(-1)) 
+10
source

The following code works for me.

 myEnv = new.env() assign('xx', 5, envir=myEnv) get('xx', envir=myEnv) rm('xx', envir=myEnv) 
0
source

Source: https://habr.com/ru/post/1304678/


All Articles