R: How to lazyload variables from inst/extdata in R package

谁说胖子不能爱 提交于 2020-08-22 06:00:20

问题


I have a file helper.RData file in my inst/extdata that contains variables and datasets to be used by the functions in my package, but not meant to be accessed by the user.

I load it at the beginning of the package using:

load(system.file("extdata","helper.RData", package = "mypackage"))

As the file is big this takes quite a bit of time and it is especially annoying during development (I use quite a loot the function load_all() from the devtools package).

I would rather prefer to have it lazy loaded so that the file is loaded only when actually needed.

How can I do that?


回答1:


Before being able to lazy-load your data you have to save your variables in a database that supports lazy load.

You can do this using the function tools:::makeLazyLoadDB and later the function lazyLoad.

To create the lazy load database. Say you have the variables X and Y, the you have to create an environment that contains them:

e=new.env(parent=emptyenv())
e$X = X
e$Y = Y

next you create the database:

tools:::makeLazyLoadDB(e,"DBNAME")

of course you can change DBNAME.

You can the import it in R using lazyLoad("DBNAME").



来源:https://stackoverflow.com/questions/21583382/r-how-to-lazyload-variables-from-inst-extdata-in-r-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!