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

查看:132
本文介绍了使用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.

我对描述进行了如下修改:

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

并将其添加到一些.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都导出一个函数 叫做foo.如果创建的软件包pkgC具有import(pkgA)和 NAMESPACE中的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的名称空间应为

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天全站免登陆