使用 roxygen2 导入两个同名函数
我是 CRAN 包的维护者,在加载时收到以下消息:
* checking whether package ‘qdap’ can be installed ... [10s/10s] WARNING
Found the following significant warnings:
Warning: replacing previous import ‘annotate’ when loading ‘NLP’
Warning: replacing previous import ‘rescale’ when loading ‘scales’
因为我使用了plotrix 和scales 包以及NLP 和ggplot 包。它们具有共同的函数rescale
和annotate
。这会导致最新的 CRAN 检查出现严重警告。所以我决定“修复”它。
我的描述如下:
Package: qdap
Type: Package
Title: Bridging the gap between qualitative data and quantitative analysis
Version: 1.0.0
Date: 2013-06-26
Author: Tyler Rinker
Maintainer: Tyler Rinker <[email protected]>
Depends:
R (>= 3.0.0),
ggplot2 (>= 0.9.3.1),
gdata,
grid,
Imports:
NLP,
openNLP,
plotrix,
scales,
LazyData: TRUE
Description: Stuff
License: GPL-2
并将其添加到一些 .R 文件中:
#' @import ggplot2 gridExtra RColorBrewer
#' @importFrom scales alpha
但这会导致另一个警告:
* installing *source* package 'qdap' ...
** R
** data
*** moving datasets to lazyload DB
** inst
** preparing package for lazy loading
Warning: replacing previous import 'rescale' when loading 'scales'
Warning: replacing previous import 'annotate' when loading 'NLP'
Warning: replacing previous import 'alpha' when loading 'scales'
如何正确使用 roxygen2
的 importFrom
标记?
我已阅读: https://github.com/hadley/devtools/wiki/Namespaces
但我从一个必须有人这样做的例子中学到了最好的东西。我不确定如何正确格式化描述文件以及避免使用 roxygen2
标签:
* checking whether package ‘qdap’ can be installed ... [10s/10s] WARNING
Found the following significant warnings:
Warning: replacing previous import ‘annotate’ when loading ‘NLP’
Warning: replacing previous import ‘rescale’ when loading ‘scales’
I'm a maintainer of a CRAN package and get the following messages when loading:
* checking whether package ‘qdap’ can be installed ... [10s/10s] WARNING
Found the following significant warnings:
Warning: replacing previous import ‘annotate’ when loading ‘NLP’
Warning: replacing previous import ‘rescale’ when loading ‘scales’
Because I use the plotrix and scales packages as well as the NLP and ggplot packages. They have the functions rescale
and annotate
in common. This results in a significant warning with the latest CRAN check. So I decide to "fix" it.
I made the description something like this:
Package: qdap
Type: Package
Title: Bridging the gap between qualitative data and quantitative analysis
Version: 1.0.0
Date: 2013-06-26
Author: Tyler Rinker
Maintainer: Tyler Rinker <[email protected]>
Depends:
R (>= 3.0.0),
ggplot2 (>= 0.9.3.1),
gdata,
grid,
Imports:
NLP,
openNLP,
plotrix,
scales,
LazyData: TRUE
Description: Stuff
License: GPL-2
And added this to some .R files:
#' @import ggplot2 gridExtra RColorBrewer
#' @importFrom scales alpha
But this results in another warning:
* installing *source* package 'qdap' ...
** R
** data
*** moving datasets to lazyload DB
** inst
** preparing package for lazy loading
Warning: replacing previous import 'rescale' when loading 'scales'
Warning: replacing previous import 'annotate' when loading 'NLP'
Warning: replacing previous import 'alpha' when loading 'scales'
How do I use roxygen2
's importFrom
tag correctly?
I have read: https://github.com/hadley/devtools/wiki/Namespaces
But I learn best from an example where someone had to do this. I'm unsure of how to format the DESCRIPTION file correctly as well as the use of roxygen2
tags to avoid:
* checking whether package ‘qdap’ can be installed ... [10s/10s] WARNING
Found the following significant warnings:
Warning: replacing previous import ‘annotate’ when loading ‘NLP’
Warning: replacing previous import ‘rescale’ when loading ‘scales’
Here is the qdap GitHub Repo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
需要记住的是,你不能拥有多个函数
与包的命名空间中的名称相同。
假设有两个包 pkgA 和 pkgB,它们都导出一个函数
称为 foo。如果您创建一个包 pkgC,它具有
import(pkgA)
和命名空间中的
import(pkgB)
。现在,当您调用library(pkgC)
时,您将得到警告:
现在,假设有人创建了另一个包 pkgD,它在 NAMESPACE 文件中包含以下内容:
那么,library(pkgD) 将给出 2 个警告:
如果每个人都采用导入整个命名空间的做法,那么30年
从现在开始,将会出现很多这样的警告。
相反,由于你的包中只能有一个“foo”,所以你应该
显式导入您想要的包的“foo”(和其他函数)
使用。在上面的示例中,pkgD 的命名空间应该是
如果您实际上需要使用两个不同包中具有相同名称的两个函数,您可以执行的一种技巧是从每个包中导入其他函数,以确保安装了这些包及其加载命名空间,然后使用
::
表示法将其放入您的 NAMESPACE: 来引用您需要的函数:然后调用函数
pkgA::abc()
和代码中的pkgB::abc()
。The thing to keep in mind is that you cannot have more than one function with
the same name in your package's namespace.
Suppose there are two packages, pkgA and pkgB, that both export a function
called foo. If you create a package, pkgC, that has
import(pkgA)
andimport(pkgB)
in the NAMESPACE. Now, when you calllibrary(pkgC)
you'll geta warning:
Now, suppose someone creates another package, pkgD, that has this in the NAMESPACE file:
Then,
library(pkgD
) will give 2 warnings:If everyone adopts the practice of importing entire namespaces, then 30 years
from now, there will be a lot of these warnings.
Instead, since you can only have a single "foo" in your package, you should
explicitly import the "foo" (and other functions) that you want your package
to use. In the above example, the NAMESPACE for pkgD should be
If you actually need to use the two functions with the same name from two different packages, one hack you can perform is to import other functions from each package to ensure the packages are installed and their namespaces are loaded, but then refer to the functions you need using
::
notation by placing this in your NAMESPACE:and then calling functions
pkgA::abc()
andpkgB::abc()
in your code.很可能不再对您有用,但对其他人可能不再有用:您问题的答案可以在您提到的网站中找到,特别是在这里(引用来源):
“无论你使用@importFrom foo bar多少次”。
所以roxygen2的标签@importFrom的正确使用是:
@importFrom package_name function_name
。没有逗号、括号,什么都没有,只是用空格分隔的两个名称(可能适用于多个函数,以明显的方式)。我刚刚在为我的一个软件包的新版本生成文档时亲自尝试过这一点,所以它应该可以工作。
我希望它有帮助。
Most likely no longer of use to you but maybe to others: the answer to your question can be found in the website you mention, in particular, here (quoting from source):
"No matter how many times you use @importFrom foo bar".
So the correct use of roxygen2's tag @importFrom is:
@importFrom package_name function_name
. No commas, parenthesis, nothing, just the two names separated by a space (possibly applicable to more than 1 function, in the obvious way).I have tried this myself just now when generating the documentation for the new version of one of my packages, so it should work.
I hope it helps.
最近我找到了解决这个问题的新方法。我想在开发中导入 dplyr 以及 data.table ,这会发出这些警告。为了删除重叠函数,我使用 importFrom 导入 data.table 中除重叠之外的每个函数。
setdiff 包含了所有冲突的函数名称。最后我 importFrom data.table 仅上面的功能。
Recently I've found a new way to tackle this problem. I want to import dplyr as well as data.table in development which gives these warnings. To remove the overlap functions, I used importFrom to import every function in data.table except for the overlaps.
the setdiff have included all the conflicted function names. Last I importFrom data.table only the functions above.