Storing Package Data in Custom Environments

R packages

How do you keep track of stuff for your own package without cluttering the users global space or setting a bunch of options?

Robert M Flight
2013-05-30

If you do R package development, sometimes you want to be able to store variables specific to your package, without cluttering up the users workspace. One way to do this is by modifying the global options. This is done by packages grDevices and parallel. Sometimes this doesn’t seem to work quite right (see this issue for example.

Another way to do this is to create an environment within your package, that only package functions will be able to see, and therefore read from and modify. You get a space to put package specific stuff, the user can’t see it or modify it directly, and you just need to write functions that do the appropriate things to that environment (adding variables, reading them, etc). This sounds great in practice, but I wasn’t clear on how to do this, even after reading the help page on environments, the R documentation, or even Hadley’s excellent writeup. From all these sources, I could glean that one can create environments, name them, modify them, etc, but wasn’t sure how to work with this within a package.

I checked out the knitcitations package to see how it was done. When I looked, I realized that it was pretty obvious in retrospect. In zzz.R, initialize the environment, assigning it to a variable. When you need to work with the variables inside, this variable will be accessible to your package, and you simply use the get and assign functions like you would if you were doing anything on the command line.

To make sure I had it figured out, I created a very tiny package to create a custom environment and functions for modifying it. Please feel free to examine, download, install (using devtools]) and see for yourself.

I have at least two projects where I know I will use this, and I’m sure others might find it useful as well.

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/rmflight/researchBlog_distill, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Flight (2013, May 30). Deciphering Life: One Bit at a Time: Storing Package Data in Custom Environments. Retrieved from https://rmflight.github.io/posts/2013-05-30-storing-package-data-in-custom-environments/

BibTeX citation

@misc{flight2013storing,
  author = {Flight, Robert M},
  title = {Deciphering Life: One Bit at a Time: Storing Package Data in Custom Environments},
  url = {https://rmflight.github.io/posts/2013-05-30-storing-package-data-in-custom-environments/},
  year = {2013}
}