I have an environment of objects that are either xts or NULL
. I would like to remove all the NULL
from the environment. Is there a function I can use with eapply
to achieve this?
Asked
Active
Viewed 550 times
5

Joshua Ulrich
- 173,410
- 32
- 338
- 418

lab_notes
- 407
- 5
- 11
1 Answers
8
rm(
list=names(
which(
sapply(globalenv(),is.null) # or .GlobalEnv
)
)
)
If it's not the global environment, you can use the envir
switch in rm
and wrap the environment name in getenv()
in the sapply
call

Brandon Bertelsen
- 43,807
- 34
- 160
- 255
-
1`globalenv()` would be better (less obscure). – Gavin Simpson Oct 26 '12 at 21:46
-
1@GavinSimpson Hrmmm, why would you say using .GlobalEnv is obscure? – Brandon Bertelsen Oct 26 '12 at 21:48
-
It is the **actual** global envioronmnent and the `.` in the name means it is hidden or intended to be hidden from normal view. `globalenv()` is the accessor function. – Gavin Simpson Oct 26 '12 at 21:52
-
Interesting. Strange that it doesn't work that way in practice. – Brandon Bertelsen Oct 26 '12 at 21:54
-
I mean that it seems to act in a manner that's almost interchangeable with `globalenv()`. – Brandon Bertelsen Oct 26 '12 at 22:21
-
2Yes, that is my point. `.GlobalEnv` is the object that *is* the global environment, the actual thing. `globalenv()` returns this. It's a bit like `mod$coefficients` and coef(mod)` both give you what you want but it is better to access via the accessor function than directly. – Gavin Simpson Oct 26 '12 at 22:23
-
1You're going to laugh but I also commonly use `$coefficients`. Hopeless. lol – Brandon Bertelsen Oct 26 '12 at 23:17