简体   繁体   English

如何正确使用R包中其他包的功能

[英]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.

相关问题 使用其他包中的功能 - 何时使用 package::function? - Using functions from other packages - when to use package::function? 在未从其他软件包导入的软件包中寻找功能 - Seeking Functions in a Package not imported from other packages R - 如何对来自其他软件包的函数使用rollapply,例如来自tseries的maxdrawdown? - R - How to use rollapply on functions from other package like maxdrawdown from tseries? 如何使用R包MACD功能? - How to use R packages MACD functions? 不要在 R 中暴露自己的 package 中导入包的功能 - Don't expose functions from imported packages in own package in R 在R上制作玩具包装,无法使用R文件中的其他功能 - Making a toy package on R, unable to use the other functions in the R file 如何删除依赖于其他软件包的R软件包? - How to remove R package that has dependencies on other packages? 使R-package只加载其他软件包并导出所有功能 - Make R-package that just loads other packages and exports all functions R包:当导出的函数没有显式调用其他包中的函数时,“导入”如何工作,但子例程却如此 - R Package: how “import” works when my exported function does not call explicitly a function from other packages, but a subroutine does 从`parallel`包中的非基本R包调用函数,而不在函数中对它们进行编码 - Calling functions from non-base R packages in `parallel` package without librarying them within the function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM