使用 .onLoad 加载依赖包
我的包需要 ggplot2 包,但我无法修复运行 R CMD 检查时得到的以下注释。
no visible global function definition for qplot
'library' or 'require' call not declared from: ggplot2
我还有一个 .onLoad 函数,
.onLoad <- function(libname, pkgname){
.libPaths("~/RLibrary")
require(ggplot2)
}
关于如何解决错误有什么建议吗? onLoad 函数应该放在哪里?
谢谢
桑
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不应该这样做。最好让你的包依赖于 ggplot2 或导入 ggplot2 的命名空间。通过添加
Depends: ggplot2
在DESCRIPTION文件中执行此操作,第二个通过在DESCRIPTION中添加Imports: ggplot2
并在NAMESPACE中添加import(ggplot2)
(或者更准确地使用importfrom(ggplot2,"somefunction")
。或者,您可以在描述中设置
Suggests: ggplot2
并添加require("ggplot2")<。 /code> 在任何使用它的函数中,但我不太喜欢这个。
另请参阅:
http://cran.r-project.org/doc/manuals/R-exts.html#The-DESCRIPTION-file
编辑:为了更清楚一点。使用
取决于
每次加载包时都会加载该包,并且其功能都可供用户使用。使用
Imports
您可以使用该包的功能,但在以下情况下不会加载该包。您的包未加载(功能对用户不可用)。使用
Suggests
,当您加载包时,包不会被加载,并且您无法使用其功能。您需要在某处声明require
才能使用它们。基本上,这可以用来明确您在某个地方使用了这个包(在示例左右)。这完全取决于您希望用户如何使用依赖的包以及它对您的包有多重要。例如,如果您的包是 ggplot2 的前端,则
Depends
是最好的,如果它进行一些分析并具有绘图功能,则最好是Imports
。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 addingImports: ggplot2
in DESCRIPTION andimport(ggplot2)
in NAMESPACE (or be more exact withimportfrom(ggplot2,"somefunction")
.Alternatively you could set
Suggests: ggplot2
in DESCRIPTION and put arequire("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 arequire
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 functionImports
is best.