使用 roxygen2 导入两个同名函数 [英] Importing two functions with same name using roxygen2

查看:20
本文介绍了使用 roxygen2 导入两个同名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 CRAN 包的维护者,在加载时收到以下消息:

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’

因为我使用 plotrix 和 scales 包以及 NLP 和 ggplot 包.它们具有共同的功能 rescaleannotate.这会导致最新的 CRAN 检查出现严重警告.所以我决定修复"它.

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.

我做了这样的描述:

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 文件中:

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'

如何正确使用roxygen2importFrom标签?

我已阅读:https://github.com/hadley/devtools/wiki/Namespaces

但我从一个必须这样做的例子中学到了最好的东西.我不确定如何正确格式化DESCRIPTION文件以及使用roxygen2标签来避免:

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’

这里是 qdap GitHub 存储库

推荐答案

要记住的是,你不能拥有多个函数在您的包的命名空间中具有相同的名称.

The thing to keep in mind is that you cannot have more than one function with the same name in your package's namespace.

假设有两个包,pkgA 和 pkgB,它们都导出一个函数叫富.如果您创建一个包 pkgC,它具有 import(pkgA)import(pkgB) 在命名空间中.现在,当你调用 library(pkgC) 你会得到警告:

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'. 

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

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

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

然后,library(pkgD) 会给出 2 个警告:

Then, library(pkgD) will give 2 warnings:

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

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

If everyone adopts the practice of importing entire namespaces, then 30 years from now, there will be a lot of these warnings.

相反,由于您的包中只能有一个foo",因此您应该显式导入你想要你的包的foo"(和其他函数)使用.在上面的例子中,pkgD 的 NAMESPACE 应该是

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)

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

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)

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

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

这篇关于使用 roxygen2 导入两个同名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆