[英]How to properly use functions from other packages in a R package
I am a bit confused about this. 我对此有点困惑。 I have an R package that has a small function (not a mayor part of the package) in which the
principal
function of the psych
package is called. 我有一个R包,它有一个小函数(不是包的mayor部分),其中调用了
psych
包的principal
功能。 How do I correctly specify this in DESCRIPTION and NAMESPACE? 如何在DESCRIPTION和NAMESPACE中正确指定?
Setting Depends: psych
in DESCRIPTION makes sure the psych package is loaded every time my package is loaded. 设置
Depends: psych
DESCRIPTION中的psych确保每次加载包时都加载psych包。 This works, but it seems redundant for such a small part of my package. 这有效,但对于我的包的这么小部分来说似乎是多余的。
Setting Suggests: psych
and entering a require("psych")
in the function is what I do now, however this does not work if psych
is not installed, and seems to be the wrong way of doing this (writing R extensions says that suggest is meant mainly for examples). 设置
Suggests: psych
和输入require("psych")
是我现在所做的,但是如果未安装psych
,这不起作用,并且似乎是这样做的错误方法(写R扩展说建议主要是为了举例)。
I think I need to import the function. 我想我需要导入这个功能。 I tried setting
Imports: psych
in DESCRIPTION and importFrom(psych,"principal")
in NAMESPACE. 我尝试在NAMESPACE中设置
Imports: psych
in DESCRIPTION和importFrom(psych,"principal")
。 This works, but on a computer that does not has psych installed it gives an error when loading my package. 这是有效的,但在没有安装精神的计算机上,它在加载我的包时会出错。
The basic question you need to answer is: "do you want the function to be available to all users of the package without further effort?". 您需要回答的基本问题是:“您是否希望该功能无需进一步努力即可供所有用户使用?”。 If yes, then use imports + the appropriate namespace declarations, if no, then use suggests and print an informative error message if
require("psych")
returns FALSE
. 如果是,则使用imports +相应的命名空间声明,如果不是,则使用建议并在
require("psych")
返回FALSE
打印信息性错误消息。
I don't understand your import related complaint that: "but on a computer that does not has psych installed it gives an error when loading my package". 我不明白你的进口相关投诉:“但是在没有安装精神的计算机上,它会在加载我的包装时出错”。 This is also true if your package is in depends!
如果你的包装依赖,这也是如此!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.