问题描述
我对此有点困惑.我有一个 R 包,它有一个小函数(不是包的主要部分),其中调用了 psych
包的 principal
函数.如何在DESCRIPTION和NAMESPACE中正确指定?
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. How do I correctly specify this in DESCRIPTION and NAMESPACE?
在DESCRIPTION中设置Depends: psych
可确保每次加载我的包时都会加载psych包.这可行,但对于我的包裹的这么小部分来说似乎是多余的.
Setting Depends: psych
in DESCRIPTION makes sure the psych package is loaded every time my package is loaded. This works, but it seems redundant for such a small part of my package.
设置 Suggests: psych
并在函数中输入 require("psych")
是我现在所做的,但是如果 psych
未安装,并且似乎是错误的做法(编写 R 扩展表示该建议主要用于示例).
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).
我想我需要导入这个函数.我尝试在DESCRIPTION中设置Imports: psych
,在NAMESPACE中设置importFrom(psych,"principal")
.这可行,但在没有安装 psych 的计算机上,加载我的包时会出错.
I think I need to import the function. I tried setting Imports: psych
in DESCRIPTION and importFrom(psych,"principal")
in NAMESPACE. This works, but on a computer that does not has psych installed it gives an error when loading my package.
推荐答案
您需要回答的基本问题是:您是否希望该功能对包的所有用户都可用,而无需进一步努力?".如果是,则使用导入 + 适当的命名空间声明,如果否,则使用建议并在 require("psych")
返回 FALSE
时打印信息性错误消息.
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
.
我不明白您与导入相关的投诉:但是在没有安装 psych 的计算机上,加载我的包时会出错".如果您的包在依赖中,这也是正确的!
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!
这篇关于如何在 R 包中正确使用其他包中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!