使用roxygen2导入两个具有相同名称的函数

时间:2013-08-20 19:13:29

标签: r package 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和scale包以及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 <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

并将其添加到某些.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的{​​{1}}代码?

我看过:https://github.com/hadley/devtools/wiki/Namespaces

但是我从一个有人必须这样做的例子中学得最好。我不确定如何正确格式化DESCRIPTION文件以及使用importFrom标记来避免:

roxygen2

以下是qdap GitHub Repo

3 个答案:

答案 0 :(得分:21)

要记住的是,你不能拥有多个功能 包名称中的相同名称。

假设有两个包,pkgA和pkgB,它们都导出一个函数 叫做foo。如果您创建一个包含import(pkgA)和的包pkgC NAMESPACE中的import(pkgB)。现在,当你致电library(pkgC)时,你会得到 警告:

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

现在,假设某人在NAMESPACE文件中创建了另一个包pkgD:

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的NAMESPACE应该是

importFrom(pkgB,foo)

如果你真的需要在两个不同的包中使用同名的两个函数,你可以执行的一个hack是从每个包导入其他函数以确保安装包并加载它们的命名空间,但是然后引用使用::表示法所需的功能,将其放入NAMESPACE:

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

然后在代码中调用函数pkgA::abc()pkgB::abc()

答案 1 :(得分:4)

很可能不再对您有用,但也许对其他人有用:您提到的问题的答案可以在您提到的网站上找到,特别是在这里(引自来源): “无论你使用@importFrom foo bar多少次。”

因此正确使用roxygen2的标签@importFrom是:@importFrom package_name function_name。没有逗号,括号,没有,只是用空格分隔的两个名称(可能以明显的方式适用于多于1个函数)。

我刚刚在为我的某个软件包的新版本生成文档时自己尝试了这个,所以它应该可以工作。

我希望它有所帮助。

答案 2 :(得分:2)

最近,我找到了解决此问题的新方法。我想在开发中导入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已包含所有冲突的函数名称。最后,我只能从data.table importFrom上面的功能。