How do you handle R Data internal to a package? How do you handle R Data internal to a package? r r

How do you handle R Data internal to a package?


Put your sysdata.rda file in the data directory of your package.

Do not use Lazy Data -- your DESCRIPTION file should either not have a line for LazyData, or, if it does, it should be LazyData: no

In any .R file in the R directory of your package add a line like this

data(sysdata, envir=environment())

I created a data.frame named sysdata and saved it to a file called sysdata.rda in the data directory of a package called anRpackage

I added the above line to an .R file, and also added this unexported functionjust to show that functions in the package have access to the data.

foo <- function() tail(sysdata, 2)

Then I see the following an R session

> library(anRpackage)> sysdataError: object 'sysdata' not found> anRpackage:::sysdata  A  B C1 1  6 a2 2  7 b3 3  8 c4 4  9 d5 5 10 e> anRpackage:::foo()  A  B C4 4  9 d5 5 10 e

So, users still have access to the data, but as you requested, they do not have direct access. The user still has the option to run data(sysdata).


You can use the .onLoad() hook to call data() when your package is being loaded, and specify the package namespace as the environment where to load the data objects into.

Assuming you have files model1.R and mydata.RData in the data/ directory of your package called foopkg, define the function

.onLoad <- function(libname, pkgname) {  data("model1", "mydata", package=pkgname, envir=parent.env(environment()))}

somewhere in your package (e.g. in foopkg-package.R).

After building and installing the package,

> library(foopkg)> ls(loadNamespace("foopkg")) 

should demonstrate that the various data objects were successfully loaded into the package namespace, i.e. visible to functions in your package but not polluting the global environment.