简体   繁体   English

R中的延迟包依赖

[英]Lazy package dependency in R

I'd like to write a R package. 我想写一个R包。 A small part of its functionality would be to save data into xlsx file. 它的一小部分功能是将数据保存到xlsx文件中。 But this functionality would require a big and heavy dependency: library(xlsx) . 但是这个功能需要大而重的依赖: library(xlsx) So I'd like to make this dependency somehow optional and lazy-loaded. 所以我想以某种方式使这种依赖可选和延迟加载。

What is the Best Practice for it? 什么是最佳实践?

I guess I could simply library(xlsx) in the code of the function that need it, and handle possible failures of this command. 我想我可以在需要它的函数的代码中简单地使用library(xlsx) ,并处理此命令可能的失败。

I believe the most robust way to do this is the add the following line to the NAMESPACE of your package: 我相信最有效的方法是NAMESPACE下行添加到包的NAMESPACE中:

importFrom(xlsx, the_function_you_need)

along with 随着

Depends: xlsx

in the DESCRIPTION file. DESCRIPTION文件中。 As far as I understand, this will give your package access to the function you want without loading the entire library. 据我所知,这将使您的包访问您想要的功能,而无需加载整个库。 There is some discussion of importFrom here: What is the benefit of import in a namespace in R? 这里有一些关于importFrom讨论: R中命名空间中导入的好处是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM