How do you handle R Data internal to a package?

后端 未结 2 1587
生来不讨喜
生来不讨喜 2021-01-31 08:54

The R package I am developing requires several R data objects, such as pre-computed models and parameters.

Currently I have each object in the \'data\' directory of the

2条回答
  •  别跟我提以往
    2021-01-31 09:30

    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.

提交回复
热议问题