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

时间:2021-10-18 16:53:43

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

因为我使用plotrix和scale包以及NLP和ggplot包。它们具有共同重新缩放和注释的功能。这会导致最新的CRAN检查发出重大警告。所以我决定“修理”它。

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?

如何正确使用roxygen2的importFrom标签?

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:

但是我从一个有人必须这样做的例子中学得最好。我不确定如何正确格式化DESCRIPTION文件以及使用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回购

2 个解决方案

#1


18  

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:

假设有两个包pkgA和pkgB,它们都导出一个名为foo的函数。如果在NAMESPACE中创建包含pkgC的包(pkgA)和导入(pkgB)。现在,当您调用库(pkgC)时,您将收到警告:

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

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

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

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

Then, library(pkgD) will give 2 warnings:

然后,库(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. In the above example, the NAMESPACE for pkgD should be

相反,由于您的包中只能有一个“foo”,因此您应该明确导入您希望包使用的“foo”(以及其他功能)。在上面的例子中,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:

如果你真的需要使用两个不同包中具有相同名称的两个函数,你可以执行的一个hack是从每个包导入其他函数以确保安装包并加载它们的命名空间,但是然后参考你的函数需要在你的NAMESPACE中使用:: notation:

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

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

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

#2


3  

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. No commas, parenthesis, nothing, just the two names separated by a space (possibly applicable to more than 1 function, in the obvious way).

因此正确使用roxygen2的标签@importFrom是:@importFrom package_name function_name。没有逗号,括号,没有,只是由空格分隔的两个名称(可能以明显的方式适用于多于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.

我希望它有所帮助。

#1


18  

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:

假设有两个包pkgA和pkgB,它们都导出一个名为foo的函数。如果在NAMESPACE中创建包含pkgC的包(pkgA)和导入(pkgB)。现在,当您调用库(pkgC)时,您将收到警告:

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

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

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

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

Then, library(pkgD) will give 2 warnings:

然后,库(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. In the above example, the NAMESPACE for pkgD should be

相反,由于您的包中只能有一个“foo”,因此您应该明确导入您希望包使用的“foo”(以及其他功能)。在上面的例子中,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:

如果你真的需要使用两个不同包中具有相同名称的两个函数,你可以执行的一个hack是从每个包导入其他函数以确保安装包并加载它们的命名空间,但是然后参考你的函数需要在你的NAMESPACE中使用:: notation:

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

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

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

#2


3  

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. No commas, parenthesis, nothing, just the two names separated by a space (possibly applicable to more than 1 function, in the obvious way).

因此正确使用roxygen2的标签@importFrom是:@importFrom package_name function_name。没有逗号,括号,没有,只是由空格分隔的两个名称(可能以明显的方式适用于多于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.

我希望它有所帮助。