【发布时间】:2010-05-27 22:36:21
【问题描述】:
如何解决函数中包的依赖关系?我在函数中添加了require(package),但我想知道是否有完成这项任务的首选方式。
【问题讨论】:
标签: r dependencies
如何解决函数中包的依赖关系?我在函数中添加了require(package),但我想知道是否有完成这项任务的首选方式。
【问题讨论】:
标签: r dependencies
根据函数的帮助,require is designed for use inside other functions; it returns FALSE and gives a warning (rather than an error as library() does by default) if the package does not exist
以后……
The source code for a package that requires one or more other packages should have a call to require, preferably near the beginning of the source, and of course before any code that uses functions, classes or methods from the other package
【讨论】:
通过使用包的DESCRIPTION 文件的Depends: 字段。
这也是为什么你更喜欢使用包而不是仅仅使用你是source()-ing 的文件的另一个原因。
编辑:在DESCRIPTION 中也有Imports:。但总体而言,R 具有 依赖机制,如果使用它会更好。
【讨论】:
标准格式好像是
if(!require(the_package))
{
#Maybe try an alternative or do some cleanup here
stop("You must install the package 'the_package'.")
}
如果你的包广泛使用另一个包,那么你应该在你的包初始化时加载那个包,即在将它添加到你的包描述文件的Depends字段中。如果包只被你的一个或两个函数使用,那么这段代码就在这些函数的开头。.First.lib函数中
【讨论】: