First of all, this question has a similar name, but there the environment only seemed unclean. Until now, I thought that after
rm(list=ls(globalenv()))
we had a global environment, clean as it was when R was launched for the first time. But by chance, I realized that at least there are class definitions:
rm(list=ls(globalenv()),envir=globalenv()) sapply(getClasses(globalenv()),function(x){removeClass(x,where=globalenv())}) ls(globalenv()) getClasses(globalenv())
A warning. After running this reproducible example, your global environment will be less 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) >
At least now I understand why the rm documentation says that
rm(list = ls())
will remove (almost) everything in the production environment.
At first, I thought that only “ls” was a bad guy, since he did not say “rm” to class names. But "rm" gives discounts to class names:
rm(list=ls(globalenv()),envir=globalenv()) sapply(getClasses(globalenv()),function(x){removeClass(x,where=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) >
Due to this warning, I assume that
- R does not account for class definitions among "Objects" and
- "rm" does not delete anything except "objects".
So, it seems that "rm" cannot delete everything. At the very least, deleting class definitions requires some extra work. It scares me that there could be something else besides objects and class definitions that are still hiding in the environment, even after "rm" and "removeClass" made their damn ones.
Is there a team that completely cleans the environment, a ban?