Site icon R-bloggers

eponge: Keep Your Environment Clean

[This article was first published on krzjoa, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

eponge is a small package, which facilitates selective object removal. It was released on CRAN at 23th March 2020. Initially, the package was named sponge, but during first submission trial I found out, that currently there exists the SPONGE package, availbale on BioConductor. Because of that, I decided to rename my package, changing only one letter. The package was given a new name: eponge, which simply means sponge in French. Let me present, what the package was created for.

Removing objects by name

Typically, when we want to remove all objects from the global environment, we can use click the broom icon in RStudio (supposing we use this IDE – but in most cases, we do). Alternatively, we can obtain the same results combining rm with ls.

# Remove all the objects
rm(list = ls())
# Remove object with 'iris' in its name
rm(list = ls(pattern = "iris"))

eponge offers a an equivalent shortcut: erase function. It’s particularly handy when we want to select some set of objects using regex pattern.

# Remove all the objects
eponge::erase() 
# Remove object with 'iris' in its name
eponge::erase("iris")

Removing objects by type

epnoge becomes even more useful, when we want to precisely remove a tiny subset of objects. Normally, we would use a combination of ls, get and rm functions. If we don’t want to recreate such code from scratch, eponge do it for us:

# Erasing by type
eponge::erase_if(is.character)
# We can use a regex pattern to identify the objects we want
eponge::erase_functions("prepare_")
# We can clean whole section in RStudio Envitonment tab
# Remove all the objects named in RStudio as "Data"
eponge::erase_data()
# Remove all the "Values" in RStidio
eponge::erase_values()

Removing masking objects

As we know, homonime objects mask each other. If we want to get rid of such objects from our environment, the most convenient way to do that is eponge’s erase_masking_* function family. At the moment, it embraces two functions:

log <- function(x) print(paste0("Logging:", x))
cars <- data.frame(idx   = 1:10,
                   speed = runif(10, 30, 50))
eponge::erase_masking()

eponge allows you to keep your R environments clean in easy way. Try it yourself!

To leave a comment for the author, please follow the link and comment on their blog: krzjoa.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.