Where are objects stored in R packages?

0

Issue

I am refactoring an R package of another author (previous employee of my company). The author uses multiple variables in the roxygen2 examples that apparently are stored somewhere in the package. They are accessible with pkgname:::bar. I renamed some of these variables across the entire package to improve naming consistency, let’s say the variable is called foo instead of bar now, but the examples are not running any more upon devtools::check(). The error: Error in get(name, envir = asNamespace(pkg), inherits = FALSE) : object 'foo' not found. However, I cannot find where they are defined. There are no objects in the data folder that would be named bar and searching for the variable name within all files (in RStudio and in Windows Explorer) does not reveal anything. Yet, pkgname:::bar still works. Any ideas where I should look for bar?

Solution

Variables can also be created in a normal R script of the package with foo <- new.env() and then populated with content, e.g.

foo <- new.env()

foo <- data.frame(
  x=1:10,
  y <- rnorm(10)
)

It is also possible to make the internal data hidden, e.g. .foo. As an example CRAN package marelac uses this mechanism in files aaa.R, gas_schmidt.R and some others.

Another possibility is a file sysdata.rda that can be placed in the /Rfolder, see: https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Package-subdirectories

Answered By – tpetzoldt

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More