R:如何从R包中的inst / extdata延迟加载变量 [英] R: How to lazyload variables from inst/extdata in R package

查看:85
本文介绍了R:如何从R包中的inst / extdata延迟加载变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的inst / extdata文件中有一个文件helper.RData文件,其中包含
变量和数据包,供我的包中的函数使用,
但不代表用户访问。 / p>

我使用以下方法在包的开头加载它:

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

由于文件很大,因此需要花费很多时间,并且在开发过程中尤其令人烦恼
(我从中使用了很多功能 load_all() devtools 包)。



我宁愿延迟加载它,以便仅在实际需要时才加载文件。



我该怎么办?

解决方案

在能够延迟加载数据之前,必须将变量保存在数据库中,支持延迟加载。



您可以使用功能 tools ::: makeLazyLoadDB 和更高的功能 lazyLoad



创建延迟加载数据库。假设您有变量X和Y,则必须创建一个包含它们的环境:

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

接下来,您将创建数据库:

  tools ::: makeLazyLoadDB(e, DBNAME)

当然,您可以更改 DBNAME



您可以使用 lazyLoad( DBNAME)将其导入R。


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?

解决方案

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").

这篇关于R:如何从R包中的inst / extdata延迟加载变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆