Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

r - Why does rm inside a function not delete objects?

rel.mem <- function(nm) {
  rm(nm)
}

I defined the above function rel.mem -- takes a single argument and passes it to rm

> ls()
[1] "rel.mem"
> x<-1:10
> ls()
[1] "rel.mem" "x"      
> rel.mem(x)
> ls()
[1] "rel.mem" "x"    

Now you can see what I call rel.mem x is not deleted -- I know this is due to the incorrect environment on which rm is being attempted.

What is a good fix for this?

Criteria for a good fix:

  1. The caller should not have to pass the environment
  2. The callee (rel.mem) should be able to determine the environment by using an R language facility (call stack inspection, aspects, etc.)
  3. The interface of the function rel.mem should be kept simple -- idiot proof: call rel.mem -- then rel.mem takes it from there -- no need to pass environments.

NOTES:

  1. As many commenters have pointed out that one easy fix is to pass the environment.
  2. What I meant by a good fix [and I should have clarified it] is that the callee function (in this case rel.mem) is able to calculate/find out the environment when the caller was referring to and then remove the object from the right environment.
  3. The type of reasoning in "2" can be done in other languages by inspecting the call stack -- for example in Java I would throw a dummy exception -- catch it and then parse the call stack. In other languages still I could use Aspect Oriented techniques. The question is can something like that be done in R?
  4. As one commenter has suggested that there may be multiple objects with the same name and thus the "right" environment is meaningless -- as I've stated above that in other languages it is possible (sometimes with some creative trickery) to interpret the call-stack -- this may not be possible in R
  5. As one commenter has suggested that rm(list=nm, envir = parent.frame()) will remove this from the parent environment. This is correct -- however I'm looking for something that will work for an arbitrary call depth.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The quick answer is that you're in a different environment - essentially picture the variables in a box: you have a box for the function and one for the Global Environment. You just need to tell rm where to find that box.

So

rel_mem <- function(nm) {
   # State the environment
   rm(list=nm, envir = .GlobalEnv )
}
x = 10
rel_mem("x")

Alternatively, you can use the pos argument, e.g.

rel_mem <- function(nm) {
   rm(list=nm, pos=1 )
}

If you type search() you will see a vector of environments, the global is number 1.

Another two options are

  • envir = parent.frame() if you want to go one level up the call stack
  • Use inherits = TRUE to go up the call stack until you find something

In the above code, notice that I'm passing the object as a character - I'm passing the "x" not x. We can be clever and avoid this using the substitute function

rel_mem <- function(nm) {
   rm(list = as.character(substitute(nm)), envir = .GlobalEnv )
}

To finish I'll just add that deleting things in the .GlobalEnv from a function is generally a bad idea.

Further resources:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...