使用.onLoad加载依赖包 [英] Loading depending packages using .onLoad

查看:121
本文介绍了使用.onLoad加载依赖包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的软件包需要ggplot2软件包,但是我无法修复以下在运行R CMD检查时得到的NOTES。

 没有可见的全局函数定义为qplot 
'library'或'require'调用未声明:ggplot2

我也有一个.onLoad函数,

  .onLoad<  -  function(libname,pkgname){ 
.libPaths(〜/ RLibrary)
require(ggplot2)
}

有关如何解决错误的建议?我应该在哪里放置onLoad函数?



谢谢

San

解决方案

我认为你不应该那样做。最好是让包依赖于ggplot2或导入ggplot2的名称空间。在描述文件中添加取决于:ggplot2 ,然后在描述文件中添加 Imports:ggplot2 ,并在 import(ggplot2)在NAMESPACE中(或者更准确地说 importfrom(ggplot2,somefunction)



或者你可以在描述中设置建议:ggplot2 并且放置一个 require(ggplot2)在任何使用它的函数中,但我不喜欢这样。



另见:

http://cran.r-project.org/doc /manuals/R-exts.html#The-DESCRIPTION-file

编辑:更清楚一点,取决于在每次加载包并且它的函数都可供用户使用时,程序包会被加载。



通过 Imports 你可以使用这个包的功能,但是当你的包没有被加载时,这个包不会被加载(功能是对用户不可用)。

使用建议当你加载你的包并且你不能使用它的函数时,包没有被加载。您需要在某处声明 require 来使用它们。基本上这可以用来清楚地表明你在某个地方使用这个软件包(在一个例子中)。



这一切都取决于你希望用户如何能够使用依赖包,以及它对你的包的重要性。例如,如果你的包是前端 ggplot2 取决于是最好的,如果它做了一些分析并且有一个图函数 Imports 是最好的。


My package requires ggplot2 package, but I am having trouble to fix the following NOTES that I get when I run R CMD check.

no visible global function definition for qplot
'library' or 'require' call not declared from: ggplot2

I also have a .onLoad function,

.onLoad <- function(libname, pkgname){
.libPaths("~/RLibrary")
require(ggplot2)
}

Any suggestions on how to solve the errors? Where should I place the onLoad function?

Thank you
San

解决方案

I don't think you should do it like that. It is best to either make your package depend on ggplot2 or import the namespace of ggplot2. Do the in the DESCRIPTION file by adding Depends: ggplot2 and the second by adding Imports: ggplot2 in DESCRIPTION and import(ggplot2) in NAMESPACE (or be more exact with importfrom(ggplot2,"somefunction").

Alternatively you could set Suggests: ggplot2 in DESCRIPTION and put a require("ggplot2") in any functions that uses it, but I don't like this alot.

See also:

http://cran.r-project.org/doc/manuals/R-exts.html#The-DESCRIPTION-file

EDIT: To be a bit more clear. With Depends the package is loaded every time your package is loaded and its functions are all available for the user.

With Imports you can use the functions of the package, but the package is not loaded when your package is not loaded (functions are not available to the user).

With Suggests the package is not loaded when you load your package and you can't use its functions. You need to declare a require somewhere to use them. Basically this can be used to make clear that you use this package somewhere (in an example or so).

It all depends on how you want your users to be able to use the depended package and how important it is for your package. For example, if your package is a frontend to ggplot2 Depends is best, if it does some analysis and has an plot function Imports is best.

这篇关于使用.onLoad加载依赖包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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