How do I manipulate the global environment inside a function in R?
r
Solution
You have to specify the environment to both `ls` and `rm`.
rm(list = setdiff(ls(globalenv()),
c("current_object_a", "current_object_b")),
pos = globalenv())
But, really, why do you want to do this? Deleting things out of the global environment from within a function seems like a Bad Thing.
Problem
I'd like to remove all the objects from my current environment except two of them, something like this ``` rm(list=setdiff(ls(),c("current_object_a","current_object_b"))) ``` but I'd like to call it within a function. If I do it now, nothing happens because I'm deleting the environment variables inside the function, not the global environment.