当前位置: 动力学知识库 > 问答 > 编程问答 >

r - Automatic loading of data from sysdata.rda in package

问题描述:

I have spent a lot of time searching for an answer to what is probably a very basic question, but I just can't find the solution to my issue. The closest that I found was this exchange from a few years ago.

In that case, the issue was the location of the sysdata.rda file in the correct directory within the package. That is not my issue.

I have some variables that store things like color palettes that I amusing inside a package. These variables are only used inside my functions so I storing them in R/sysdata.rda. However, when I load the packages, the variables are not loading into the package environment. If I load the data manually from sysdata.rda then everything works fine.

My impression from reading everything that I could find on internal data in R packages was that the data in R/sysdata.rda would load automatically.

Here is the code that I am using to store my data.

devtools::use_data(tmpBrks, tmpColors, prcpBrks, prcpChgBrks,

prcpChgBrkLabels, prcpColors, prcpChgColors,

internal = TRUE, overwrite = TRUE)

That successfully creates the data file at R/sysdata.rda and the data is in the file when I load it manually.

What do I need to do to have the data load automatically so the functions in my package can use them?

网友答案:

As usual, this was a bad combination of user ignorance and poor R documentation. The data was being loaded and was available to the functions. Where I went wrong was in assuming that the data would be visible in the package environment. That is not the case.

As far as I can tell, internal data in the R\sysdata.rda file is available to the functions within the package, but not visible in any way. After I created the internal data file I was looking for the data in the package environment. When I didn't see it I assumed that it wasn't loaded. When I kept pushing forward with my package development I finally realized that the data was loading silently and accessible to the functions in the package.

As evidenced by the two up votes that my question got, I am not the only one who didn't understand the behavior of the R\sysdata.rda internal data. Hopefully this explanation will save someone else a bunch of time searching for an answer to this issue that doesn't really exist.

分享给朋友:
您可能感兴趣的文章:
随机阅读: