How to make the clinic clean?

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()) #---------------------------------------------------------------- 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()) 

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) > #---------------------------------------------------------------- > 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 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()) #---------------------------------------------------------------- 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 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?

+7
r
source share

No one has answered this question yet.

See similar questions:

29th
rm (list = ls ()) does not completely clear the workspace

or similar:

2474
How to make a great R reproducible example
1251
How to sort a data frame by multiple columns
674
How can we create xkcd style graphics?
291
How to properly document S4 class slots using Roxygen2?
4
R warning: "Package: statistics" may not be available when downloading "
one
R: media confusion
one
Each command gives several warnings in R, although the code compiles
one
Rename datasets in an environment
0
how to provide user function output as data.frame in R in. GlobalEnv
0
Overwriting an object in a global environment after using deparse (substitute ()) when calling a function

All Articles