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.

<span class="c1"># Remove all the objects</span><span class="w">
</span><span class="n">rm</span><span class="p">(</span><span class="n">list</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ls</span><span class="p">())</span><span class="w">
</span><span class="c1"># Remove object with 'iris' in its name</span><span class="w">
</span><span class="n">rm</span><span class="p">(</span><span class="n">list</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ls</span><span class="p">(</span><span class="n">pattern</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"iris"</span><span class="p">))</span><span class="w">
</span>

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

<span class="c1"># Remove all the objects</span><span class="w">
</span><span class="n">eponge</span><span class="o">::</span><span class="n">erase</span><span class="p">()</span><span class="w"> 
</span><span class="c1"># Remove object with 'iris' in its name</span><span class="w">
</span><span class="n">eponge</span><span class="o">::</span><span class="n">erase</span><span class="p">(</span><span class="s2">"iris"</span><span class="p">)</span><span class="w">
</span>

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:

<span class="c1"># Erasing by type</span><span class="w">
</span><span class="n">eponge</span><span class="o">::</span><span class="n">erase_if</span><span class="p">(</span><span class="n">is.character</span><span class="p">)</span><span class="w">
</span><span class="c1"># We can use a regex pattern to identify the objects we want</span><span class="w">
</span><span class="n">eponge</span><span class="o">::</span><span class="n">erase_functions</span><span class="p">(</span><span class="s2">"prepare_"</span><span class="p">)</span><span class="w">
</span><span class="c1"># We can clean whole section in RStudio Envitonment tab</span><span class="w">
</span><span class="c1"># Remove all the objects named in RStudio as "Data"</span><span class="w">
</span><span class="n">eponge</span><span class="o">::</span><span class="n">erase_data</span><span class="p">()</span><span class="w">
</span><span class="c1"># Remove all the "Values" in RStidio</span><span class="w">
</span><span class="n">eponge</span><span class="o">::</span><span class="n">erase_values</span><span class="p">()</span><span class="w">
</span>

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:

  • erase_masking
  • erase_masking_functions
<span class="n">log</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="k">function</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="n">paste0</span><span class="p">(</span><span class="s2">"Logging:"</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">))</span><span class="w">
</span><span class="n">cars</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">data.frame</span><span class="p">(</span><span class="n">idx</span><span class="w">   </span><span class="o">=</span><span class="w"> </span><span class="m">1</span><span class="o">:</span><span class="m">10</span><span class="p">,</span><span class="w">
                   </span><span class="n">speed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">runif</span><span class="p">(</span><span class="m">10</span><span class="p">,</span><span class="w"> </span><span class="m">30</span><span class="p">,</span><span class="w"> </span><span class="m">50</span><span class="p">))</span><span class="w">
</span><span class="n">eponge</span><span class="o">::</span><span class="n">erase_masking</span><span class="p">()</span><span class="w">
</span>

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)