如何在 R 包中正确使用其他包中的函数

编程入门 行业动态 更新时间:2024-10-27 17:20:45
本文介绍了如何在 R 包中正确使用其他包中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对此有点困惑.我有一个 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 包中正确使用其他包中的函数

本文发布于:2023-11-17 03:55:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608742.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:包中   函数   正确   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!