How do you handle R Data internal to a package?

后端 未结 2 1550
生来不讨喜
生来不讨喜 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:35

    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 function just 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)
    > sysdata
    Error: object 'sysdata' not found
    
    > anRpackage:::sysdata
      A  B C
    1 1  6 a
    2 2  7 b
    3 3  8 c
    4 4  9 d
    5 5 10 e
    
    > anRpackage:::foo()
      A  B C
    4 4  9 d
    5 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).

提交回复
热议问题