Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an environment clinically clean?

Tags:

r

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?

like image 469
mra68 Avatar asked Aug 17 '15 12:08

mra68


People also ask

Why it is important to maintain a clean environment in the clinical setting?

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.


1 Answers

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/

like image 170
n.soto Avatar answered Oct 06 '22 00:10

n.soto