Interactive data analysis in R can result in a messy environment with many temporary environment. We may want to remove most of the variables except a few, e.g., the main data table. This package provides convenient functions to keep some variables persisting in a different environment and remove others.
devtools::install_github("chatchavan/cleanslate")
Load the library.
library(cleanslate)
Suppose you have the following analysis environment.
To keep a variable, use persist()
:
x <- "I want to keep"
persist(x)
x
is now moved from the interactive environment (.GlobalEnv
) to a special environment created by this package. You can still access x
, but rm(x)
won't work.
If you have many other variables, there's also a function rm_all()
to remove everything.
y <- 12
z <- 13
rm_all()
y # Error: object 'y' not found
x # [1] "I want to keep"
Note that if you define a new variable with the same name as the persist
ed variable, the new variable will mask the persisted one until it is removed.
x <- 12
x # 12
rm(x)
x # [1] "I want to keep"
This pattern is intended for temporary modification of global variables until it is not used.
Restarting R will also clear persisted variables.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.