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
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?
Cleanliness is essential for every healthcare setting, as they are tasked with protecting the health of millions of people every day. Part of maintaining patients' well-being is protecting them from bacteria that may be present in a facility that provides medical treatment.
The best option is to restart r. I've seen it recommended by experienced r programmers who also recommend avoiding rm(list = ls()) because "it makes your script vulnerable to hidden dependencies on things you ran" prior to it but in that same process. https://www.tidyverse.org/articles/2017/12/workflow-vs-script/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With