2.14.0 即将到来的命名空间、依赖、导入更改(请一些定义/使用)

发布于 2024-12-12 05:20:18 字数 753 浏览 0 评论 0原文

如果您是软件包作者,那么当我们在大约一周后转向 2.14 时,您希望能够清楚地了解软件包结构即将发生的变化。其中一项更改是所有软件包都需要命名空间,如果您不创建命名空间,系统会为您生成一个命名空间(R 相当于您在美国的 Miranda 权利)。所以作为一个好公民,我试图解决这个问题。这是 R-exts 的部分:

1.6.5 总结 – 转换现有包

总而言之,将现有包转换为使用命名空间 涉及几个简单的步骤:

确定公共定义并将其放入导出指令中。 识别S3风格的方法定义并编写相应的S3方法 声明。识别依赖关系并替换任何 require 调用 导入指令(并在 Depends 和 导入描述文件的字段)。替换.First.lib函数 使用 .onLoad 函数或 useDynLib 指令。

为了确保我在这里做正确的事情,有人可以给出一个简短明确的定义/答案(我是否因为将几个小但相关的问题放在一起而违反了规则?)。所有答案都应考虑 2.14,请:

  1. R 使用的 NAMESPACE 的定义
  2. 有没有办法在构建和检查之前生成 NAMESPACE,或者我们 b/c 一次,然后编辑自动创建的 NAMESPACE?
  3. 描述文件中“取决于:”和“导入:”之间的区别。特别是,为什么我要把包放在“Depends:”而不是“Imports:”中,反之亦然?
  4. 听起来“require”不再被使用,尽管它没有这么说。这是正确的解释吗?

谢谢!

If you are a package author, you are hopefully well aware of upcoming changes in package structure when we move to 2.14 in about a week. One of the changes is that all packages will require a NAMESPACE, and one will be generated for you in the event you do not make one (the R equivalent of your Miranda rights in the US). So being good citizen I was trying to figure this out. Here is the section from R-exts:

1.6.5 Summary – converting an existing package

To summarize, converting an existing package to use a namespace
involves several simple steps:

Identify the public definitions and place them in export directives.
Identify S3-style method definitions and write corresponding S3method
declarations. Identify dependencies and replace any require calls by
import directives (and make appropriate changes in the Depends and
Imports fields of the DESCRIPTION file). Replace .First.lib functions
with .onLoad functions or useDynLib directives.

To ensure I do the right thing here, can someone give a short clear definition/answer (am I breaking a rule by having several small but related questions together?). All answers should take 2.14 into account, please:

  1. A definition of NAMESPACE as used by R
  2. Is there a way to generate a NAMESPACE prior to build and check, or do we b/c once and then edit the NAMESPACE created automatically?
  3. The difference between "Depends:" and "Imports:" in the DESCRIPTION file. In particular, why would a I put a package in "Depends:" instead of the "Imports:" or vice versa?
  4. It sounds like "require" is no longer to be used, though it doesn't say that. Is this the correct interpretation?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

dawn曙光 2024-12-19 05:20:18

我在 https://github.com/hadley/devtools/wiki 上写了一些关于这个主题的文章/命名空间

回答您的问题:

  1. 请参阅德克的回答。
  2. 使用 roxygen2
  3. 既然每个包都有一个命名空间,那么就没有理由使用 Depends 了。
  4. require 只能用于加载建议的包

I've written a little on this topic at https://github.com/hadley/devtools/wiki/Namespaces.

To answer your questions:

  1. See Dirk's answer.
  2. Use roxygen2
  3. Now that every package has a namespace there is little reason to use Depends.
  4. require should only be used to load suggested packages
笔落惊风雨 2024-12-19 05:20:18

CRAN 包几乎自古就有命名空间。只需选择一些您最喜欢的 CRAN 软件包并查看它们的 NAMESPACE 文件即可。

它可以像摘自 snow 的一行字(加上注释)一样简单

# Export all names unless they start with a dot
exportPattern("^[^.]")

:像往常一样运行 R CMD check ,在大多数情况下应该没问题。

CRAN packages have had NAMESPACEs since almost time immortal. Just pick a few of your favorite CRAN packages and look at their NAMESPACE files.

It can be as easy as this one-liner (plus comment) taken from snow:

# Export all names unless they start with a dot
exportPattern("^[^.]")

The run R CMD check as usual, and you should be fine in most circumstances.

§普罗旺斯的薰衣草 2024-12-19 05:20:18

我将用我在将几个软件包切换到 R 2.14 后学到的一些细节来回答我自己的问题。

手册中的上述描述给人的印象是,R 2.13 的 Depends: 中的任何内容都应移至 R 2.14 中的 Imports: 中。您应该这样做,但它们在功能上并不是一对一的相同,我希望从下面的注释中可以清楚地看出这一点。

我们开始吧:

Depends:可能应该仅用于版本限制,例如“R >= 2.10”或“MASS >” 0.1',R 2.14 下没有其他内容。

拥有命名空间在一定程度上是一种通知用户可能存在名称冲突和“替换”的机制——换句话说,覆盖正在使用的名称。命名空间文件必须与描述中的项目和导入:字段匹配。导入的函数名称等将列在 sessionInfo() 中的“通过命名空间加载(且未附加)”下。这些包已安装但未加载(即没有库(某些导入的包))。

命名空间的另一个作用是让函数在你的包“内部”可用。我的意思是,如果您的包使用导入包中的函数,就会找到它。

但是,当您在检查期间要运行 .Rd 文件中的示例时,以前在 R 2.13 中的 Depends: 下但现在在 R 2.14 下的 Imports: 下的包不可用。这是因为检查环境非常类似于在干净的环境中获取脚本(假设您使用的是 R --vanilla,因此 .Rprofiles 等尚未运行)。除非您在示例中添加库(需要的包)语句,否则即使在 R 2.13 下工作,它也不会在 R 2.14 下工作。因此,即使您的包 Imports: 是所需的包,旧示例也不一定会运行,因为 Imports: 与 Depends: 并不完全相同(严格来说,它们是附加的但未加载)。

如果其中有任何错误,请纠正我。非常感谢 Hadley Wickham 和其他帮助我的人!

I'm going to answer my own question with a few details I've learned after switching several packages over to R 2.14.

The description above from the manual sort of gives the impression that whatever you had in Depends: for R 2.13 should be moved over to Imports: in R 2.14. You should do that, but they are not 1-for-1 functionally the same, as I hope will be clear from the notes below.

Here we go:

Depends: should probably be used only for restrictions on versions, like 'R >= 2.10' or 'MASS > 0.1' and nothing else under R 2.14.

Having a namespace is partly a mechanism of notifying users that there may be name conflicts and "replacements" -- in other words overwriting of names in use. The NAMESPACE file must match in items and in order the Imports: field in DESCRIPTION. Function names etc imported will be listed under "loaded via namespace (and not attached)" in sessionInfo(). Those packages are installed but not loaded (i.e. no library(some imported package)).

The other role of a namespace is to make functions available to your package "internally". By that, I mean that if your package uses a function in an imported package, it will be found.

However, when you have an example in an .Rd file to be run during checking, packages that you used to have under Depends: in R 2.13 but are now in Imports: under R 2.14 are not available. This is because the checking environment is pretty much like sourcing a script in a clean environment (assuming you are using R --vanilla so .Rprofiles etc have not been run). Unless you put a library(needed package) statement in your example, it won't work under R 2.14 even if it did under R 2.13. So the old examples do not necessarily run even though your package Imports: the needed packages, because again Imports: is not quite the same as Depends: (strictly, they are attached but not loaded).

Please do correct me if any of this is wrong. Many thanks to Hadley Wickham and others who helped me along!

(り薆情海 2024-12-19 05:20:18

我最近为我的一个包做了这个工作。这是我的新 Depends、Imports 和 Suggests 行

Depends: R (>= 2.15.0)
Imports: nlme, mvtnorm, KFAS (>= 0.9.11), stats, utils, graphics
Suggests: Hmisc, maps, xtable, stringr

stats、utils 和图形是基本 R 的一部分,但用户可以分离它们,然后我的包将无法工作。如果您从命令行使用 R,您可能会想“为什么有人要分离这些?”。但是,如果用户正在使用 RStudio,我可以看到他们浏览并“取消单击”所有包。然而,奇怪的是,如果他们这样做,我不希望我的包裹停止工作。或者他们可能会重新定义绘图函数(或其他一些函数),然后我的包就会失败。

然后,我的命名空间包含以下几行,

import(KFAS)
import(stats)
import(utils)
import(graphics)

我不想浏览并跟踪我使用的统计信息、实用程序和图形函数,因此我导入了它们的整个命名空间。对于 KFAS,我只需要 2 个函数,但导出的函数名称在版本之间发生了变化,因此我导入整个命名空间,然后在我的代码中测试用户拥有哪个版本。

对于 mvtnorm 和 nlme,我只使用一个函数,因此我只导入这些函数。我可以导入整个命名空间,但尝试只导入我真正使用的内容。

importFrom(mvtnorm, rmvnorm)
importFrom(nlme, fdHess)

建议包出现的小插图

require(package)

中有线条。

对于我的命名空间中的导出函数,我有点困惑。 CRAN 已经变得严格,不允许 ::: 出现在你的包代码中。这意味着如果我不导出函数,我就会限制创意的重复使用。另一方面,我理解需要仅导出您打算使用稳定的参数列表和输出来维护的函数,否则我们会通过更改函数接口来破坏彼此的包。

I recently worked on this for one of my packages. Here are my new Depends, Imports, and Suggests lines

Depends: R (>= 2.15.0)
Imports: nlme, mvtnorm, KFAS (>= 0.9.11), stats, utils, graphics
Suggests: Hmisc, maps, xtable, stringr

stats, utils and graphics are part of base R, but the user could detach them and then my package wouldn't work. If you use R from the command line, you might think 'why would anyone detach those?'. But if a user is using RStudio, say, I could see them going through and 'unclicking' all the packages. Strange thing to do, nonetheless, I don't want my package to stop working if they do that. Or they might redefine, say, the plot function (or some other function) , and then my package would fail.

My NAMESPACE then has the following lines

import(KFAS)
import(stats)
import(utils)
import(graphics)

I don't want to go through and keep track of which stats, utils and graphics functions I use, so I import their whole namespaces. For KFAS, I only need 2 functions, but the exported function names changed between versions so I import the whole namespace and then in my code test which version the user has.

For mvtnorm and nlme, I only use one function so I import just those. I could import the whole namespace, but try to only import what I really use.

importFrom(mvtnorm, rmvnorm)
importFrom(nlme, fdHess)

The vignettes where the Suggests packages appear have

require(package)

lines in them.

For the exported functions in my NAMESPACE, I'm a little torn. CRAN has become strict in not allowing ::: in your package code. That means that if I don't export a function, I am restricting creative re-use. On the other hand, I understand the need to only export functions that you intend to maintain with a stable arg list and output otherwise we break each other's packages by changing the function interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文