First of all, this question has a similar title, but there the environment only seemed to be unclean. Until now I thought that after
rm(list=ls(globalenv()))
we had a global environment as clean as it was when R was started for the first time. But by accident I realized that at least the class definitions survive:
rm(list=ls(globalenv()),envir=globalenv())
sapply(getClasses(globalenv()),function(x){removeClass(x,where=globalenv())})
ls(globalenv())
getClasses(globalenv())
#----------------------------------------------------------------
x <- 1:3
setClass("A", where=globalenv())
ls(globalenv())
getClasses(globalenv())
#----------------------------------------------------------------
rm(list=ls(globalenv()),envir=globalenv())
ls(globalenv())
getClasses(globalenv())
#----------------------------------------------------------------
sapply(getClasses(globalenv()),function(x){removeClass(x,where=globalenv())})
ls(globalenv())
getClasses(globalenv())
Warning: After running this reproducable example your global environment will be cleaner than after "rm(list=ls())".
> source('~/.active-rstudio-document', echo=TRUE)
> rm(list=ls(globalenv()),envir=globalenv())
> sapply(getClasses(globalenv()),function(x){removeClass(x,where=globalenv())})
named list()
> ls(globalenv())
character(0)
> getClasses(globalenv())
character(0)
> #----------------------------------------------------------------
> x <- 1:3
> setClass("A", where=globalenv())
> ls(globalenv())
[1] "x"
> getClasses(globalenv())
[1] "A"
> #----------------------------------------------------------------
> rm(list=ls(globalenv()),envir=globalenv())
> ls(globalenv())
character(0)
> getClasses(globalenv())
[1] "A"
> #----------------------------------------------------------------
> sapply(getClasses(globalenv()),function(x){removeClass(x,where=globalenv())})
A
TRUE
> ls(globalenv())
character(0)
> getClasses(globalenv())
character(0)
>
At least I understand now why in the documentation of "rm" it says that
rm(list = ls())
will remove (almost) everything in the working environment.
First I thought that only "ls" was the bad guy, since it doesn't tell "rm" the names of the classes. But "rm" discounts the class names:
rm(list=ls(globalenv()),envir=globalenv())
sapply(getClasses(globalenv()),function(x){removeClass(x,where=globalenv())})
ls(globalenv())
getClasses(globalenv())
#----------------------------------------------------------------
x <- 1:3
setClass ( "A", where=globalenv() )
ls(globalenv())
getClasses(globalenv())
#----------------------------------------------------------------
rm(list=ls(globalenv()),envir=globalenv())
rm(list=getClasses(globalenv()),envir=globalenv())
ls(globalenv())
getClasses(globalenv())
.
> source('~/.active-rstudio-document', echo=TRUE)
> rm(list=ls(globalenv()),envir=globalenv())
> sapply(getClasses(globalenv()),function(x){removeClass(x,where=globalenv())})
named list()
> ls(globalenv())
character(0)
> getClasses(globalenv())
character(0)
> #----------------------------------------------------------------
> x <- 1:3
> setClass ( "A", where=globalenv() )
> ls(globalenv())
[1] "x"
> getClasses(globalenv())
[1] "A"
> #----------------------------------------------------------------
> rm(list=ls(globalenv()),envir=globalenv())
> rm(list=getClasses(globalenv()),envir=globalenv())
> ls(globalenv())
character(0)
> getClasses(globalenv())
[1] "A"
Warning message:
In rm(list = getClasses(globalenv()), envir = globalenv()) :
object 'A' not found
>
Due to this warning I guess that
- R does not count class definitions among "Objects", and
- "rm" removes nothing but "Objects".
So it seems that "rm" is not able to remove everything. At least the deletion of class definitions requires some additional work. This scares me that there might be something else but objects and class definitions still hiding in the environment, even after "rm" and "removeClass" have done their damnedest.
Is there a command that clears out an environment completely, bar none?