简体   繁体   English

使用 roxygen2 导入两个同名函数

[英]Importing two functions with same name using roxygen2

I'm a maintainer of a CRAN package and get the following messages when loading:我是 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’

Because I use the plotrix and scales packages as well as the NLP and ggplot packages.因为我使用 plotrix 和 scales 包以及 NLP 和 ggplot 包。 They have the functions rescale and annotate in common.它们具有共同的rescaleannotate功能。 This results in a significant warning with the latest CRAN check.这会导致最新的 CRAN 检查出现严重警告。 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 <tyler.rinker@gmail.com>
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:并将其添加到一些 .R 文件中:

#' @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?如何正确使用roxygen2importFrom标签?

I have read: https://github.com/hadley/devtools/wiki/Namespaces我已阅读: 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:我不确定如何正确格式化描述文件以及使用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’

Here is the qdap GitHub Repo这是qdap GitHub Repo

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.假设有两个包 pkgA 和 pkgB,它们都导出一个名为 foo 的函数。 If you create a package, pkgC, that has import(pkgA) and import(pkgB) in the NAMESPACE.如果您创建了一个包 pkgC,它在 NAMESPACE 中具有import(pkgA)import(pkgB) Now, when you call library(pkgC) you'll get a warning:现在,当您调用library(pkgC)您会收到警告:

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

Now, suppose someone creates another package, pkgD, that has this in the NAMESPACE file:现在,假设有人创建了另一个包 pkgD,它在 NAMESPACE 文件中有这个:

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

Then, library(pkgD ) will give 2 warnings:然后, library(pkgD ) 将给出 2 个警告:

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.如果每个人都采用导入整个命名空间的做法,那么从现在起 30 年,这样的警告会很多。

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.相反,由于您的包中只能有一个“foo”,因此您应该明确导入您希望包使用的“foo”(和其他函数)。 In the above example, the NAMESPACE for pkgD should be在上面的例子中,pkgD 的 NAMESPACE 应该是

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:如果您确实需要从两个不同的包中使用具有相同名称的两个函数,您可以执行的一种技巧是从每个包中导入其他函数以确保安装了这些包并加载了它们的命名空间,然后参考您的函数需要使用::表示法,将它放在您的 NAMESPACE 中:

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

and then calling functions pkgA::abc() and pkgB::abc() in your code.然后在代码中调用函数pkgA::abc()pkgB::abc()

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

So the correct use of roxygen2's tag @importFrom is: @importFrom package_name function_name .所以roxygen2的标签@importFrom的正确使用是: @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).没有逗号,括号,什么都没有,只有两个名字用空格隔开(很明显,可能适用于 1 个以上的函数)。

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.我希望它有帮助。

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.我想在开发中导入 dplyr 以及 data.table,它会给出这些警告。 To remove the overlap functions, I used importFrom to import every function in data.table except for the overlaps.为了删除重叠函数,我使用 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"

the setdiff have included all the conflicted function names. setdiff 已包含所有冲突的函数名称。 Last I importFrom data.table only the functions above.最后我 importFrom data.table 只有上面的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM