使用 roxygen2 导入两个同名函数

发布于 2024-12-15 21:06:24 字数 2167 浏览 0 评论 0原文

我是 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 包。它们具有共同的函数rescaleannotate。这会导致最新的 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'

如何正确使用 roxygen2importFrom 标记?

我已阅读: 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’

这是 qdap GitHub 存储库

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 技术交流群。

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

发布评论

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

评论(3

≈。彩虹 2024-12-22 21:06:24

需要记住的是,你不能拥有多个函数
与包的命名空间中的名称相同。

假设有两个包 pkgA 和 pkgB,它们都导出一个函数
称为 foo。如果您创建一个包 pkgC,它具有 import(pkgA)
命名空间中的import(pkgB)。现在,当您调用 library(pkgC) 时,您将得到
警告:

replacing previous import 'foo' when loading 'pkgB'. 

现在,假设有人创建了另一个包 pkgD,它在 NAMESPACE 文件中包含以下内容:

import(pkgA)
import(pkgB)
import(pkgC)

那么,library(pkgD) 将给出 2 个警告:

1: replacing previous import ‘foo’ when loading ‘pkgB’ 
2: replacing previous import ‘foo’ when loading ‘pkgB’ 

如果每个人都采用导入整个命名空间的做法,那么30年
从现在开始,将会出现很多这样的警告。

相反,由于你的包中只能有一个“foo”,所以你应该
显式导入您想要的包的“foo”(和其他函数)
使用。在上面的示例中,pkgD 的命名空间应该是

importFrom(pkgB,foo)

如果您实际上需要使用两个不同包中具有相同名称的两个函数,您可以执行的一种技巧是从每个包中导入其他函数,以确保安装了这些包及其加载命名空间,然后使用 :: 表示法将其放入您的 NAMESPACE: 来引用您需要的函数:

importFrom(pkgA,foo)
importFrom(pkgB,bar)

然后调用函数 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) and
import(pkgB) in the NAMESPACE. Now, when you call library(pkgC) you'll get
a warning:

replacing previous import 'foo' when loading 'pkgB'. 

Now, suppose someone creates another package, pkgD, that has this in the NAMESPACE file:

import(pkgA)
import(pkgB)
import(pkgC)

Then, library(pkgD) will give 2 warnings:

1: replacing previous import ‘foo’ when loading ‘pkgB’ 
2: replacing previous import ‘foo’ when loading ‘pkgB’ 

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

importFrom(pkgB,foo)

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:

importFrom(pkgA,foo)
importFrom(pkgB,bar)

and then calling functions pkgA::abc() and pkgB::abc() in your code.

无所谓啦 2024-12-22 21:06:24

很可能不再对您有用,但对其他人可能不再有用:您问题的答案可以在您提到的网站中找到,特别是在这里(引用来源):
“无论你使用@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.

无妨# 2024-12-22 21:06:24

最近我找到了解决这个问题的新方法。我想在开发中导入 dplyr 以及 data.table ,这会发出这些警告。为了删除重叠函数,我使用 importFrom 导入 data.table 中除重叠之外的每个函数。

ls("package:data.table") %>% 
  setdiff(c("last","first","between",":=")) %>% 
  str_c(collapse = " ")

## "%between% %chin% %flike% %ilike% %inrange% %like% address alloc.col as.data.table as.Date.IDate as.IDate as.ITime as.xts.data.table chgroup chmatch chorder CJ copy cube data.table dcast dcast.data.table fcoalesce fifelse fintersect foverlaps frank frankv fread frollapply frollmean frollsum fsetdiff fsetequal fsort funion fwrite getDTthreads getNumericRounding groupingsets haskey hour IDateTime indices inrange is.data.table isoweek key key<- key2 like mday melt melt.data.table merge.data.table minute month nafill quarter rbindlist rleid rleidv rollup rowid rowidv second set set2key set2keyv setalloccol setattr setcolorder setDF setDT setDTthreads setindex setindexv setkey setkeyv setnafill setnames setNumericRounding setorder setorderv shift shouldPrint SJ tables test.data.table timetaken transpose truelength tstrsplit uniqueN update.dev.pkg wday week yday year"

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.

ls("package:data.table") %>% 
  setdiff(c("last","first","between",":=")) %>% 
  str_c(collapse = " ")

## "%between% %chin% %flike% %ilike% %inrange% %like% address alloc.col as.data.table as.Date.IDate as.IDate as.ITime as.xts.data.table chgroup chmatch chorder CJ copy cube data.table dcast dcast.data.table fcoalesce fifelse fintersect foverlaps frank frankv fread frollapply frollmean frollsum fsetdiff fsetequal fsort funion fwrite getDTthreads getNumericRounding groupingsets haskey hour IDateTime indices inrange is.data.table isoweek key key<- key2 like mday melt melt.data.table merge.data.table minute month nafill quarter rbindlist rleid rleidv rollup rowid rowidv second set set2key set2keyv setalloccol setattr setcolorder setDF setDT setDTthreads setindex setindexv setkey setkeyv setnafill setnames setNumericRounding setorder setorderv shift shouldPrint SJ tables test.data.table timetaken transpose truelength tstrsplit uniqueN update.dev.pkg wday week yday year"

the setdiff have included all the conflicted function names. Last I importFrom data.table only the functions above.

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