在开发多个相关的R包时,使用:::或导出所有内容? [英] Use ::: or export everything when developing multiple, related R packages?
问题描述
我正在研究一系列的R包,所有这些都包含一个内置包装的大量通用代码,可以称之为 myPackageUtilities
。所以我有几个包
myPackage1
, myPackage2
等等...
所有这些软件包都依赖于 myPackageUtilities
中的每个方法。有关实际的示例,请参阅 CRAN上的statnet 。每个依赖包,当然有
取决于:myPackageUtilities
$ b $
我的问题是:在的R代码中,myPackage1
,以下两种技术从 myPackageUtilities
中访问方法中的哪一种比较可取:
- 使用
:::
访问myPackageUtilities
中的方法,或 - 从
myPackageUtilities
中导出所有内容(例如,通过包含exportPattern(^ [^ \\。])
在NAMESPACE中)?
选项2插入最终用户的搜索路径,但 R gurus 建议不要使用 :::
。
后续问题:如果(2)是更好的选择,是否有出口方式一切都使用roxygen2?
假设我们有一个名为 randomUtils
的包,这个包有一个名为 sd的函数
计算出Slytherin Defiance商。
现在我写了一个名为 spellbound
的包。如果 spellbound
取决于 randomUtils
,则 randomUtils :: sd
将在搜索路径中找到,并可能与计算标准偏差冲突。
如果 spellbound
导入 randomUtils
,然后R将安装 randomUtils
,但加载 spellbound
时不会加载。因此,新版本的 sd
在搜索路径上找不到,但仍可以通过 randomUtils :: sd
随着越来越多的CRAN贡献工作,越来越重要的是我们尽可能地使用Imports,这样我们就不会引入意想不到的具有冲突的功能定义的行为。
我使用Depends的一个例子:在编写HydeNet软件包时,我希望最终用户能够使用与海德网络一致的rjags软件包。所以我把rjags放在Depends中,这样 library(HydeNet)
将会包装包。 (换句话说,在搜索路径上放入 rjags
。
打算让用户直接访问这些功能,应该进入导入。
I am working on a family of R packages, all of which share substantial common code which is housed in an internal package, lets call it myPackageUtilities
. So I have several packages
myPackage1
, myPackage2
, etc...
All of these packages depend on every method in myPackageUtilities
. For a real-world example, please see statnet on CRAN. Each dependent package, of course, has
Depends: myPackageUtilities
in its DESCRIPTION file.
My question is: In the R code for myPackage1
, which of the following two techniques for accessing methods from myPackageUtilities
is preferable:
- Use
:::
to access the methods inmyPackageUtilities
, or - Export everything from
myPackageUtilities
(e.g. by includingexportPattern("^[^\\.]")
in the NAMESPACE)?
Option 2 clutters the end-user's search path, but the R gurus recommend against using :::
.
Follow-up question: If (2) is the better choice, is there a way to export everything using roxygen2?
Suppose we have a package called randomUtils
and this package has a function called sd
that calculates the Slytherin Defiance Quotient.
Now I write a package called spellbound
. If spellbound
Depends on randomUtils
, then randomUtils::sd
will be found in the search path and can conflict with calculating standard deviation.
If spellbound
Imports randomUtils
, however, then R will install randomUtils
but will not load it when spellbound
is loaded. Thus, the new version of sd
can't be found on the search path, but can still be accessed by randomUtils::sd
With an ever growing body of contributed work on CRAN, it is becoming very important that we use Imports as much as possible so that we don't introduce unexpected behaviors by having conflicting function definitions.
An example of when I have used Depends: when writing the HydeNet package, I wanted the end user to be able to use the rjags package in concert with HydeNet. So I put rjags in Depends so that library (HydeNet)
would loaf both packages. (In other words, put rjags
on the search path.
Moral of the story, if you don't intend for the user to directly access the functions, it should go in Imports.
这篇关于在开发多个相关的R包时,使用:::或导出所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!