在构建包时导入除一个包外的所有功能 [英] import all the functions of a package except one when building a package
问题描述
我正在构建一个 R 包 (mypackage),用于导入 data.table 和另一个包(我们称之为 myotherpackage).
I'm building an R package (mypackage) that imports data.table and another package (let's call it myotherpackage).
Imports: data.table, myotherpackage
在mypackage的DESCRIPTION文件中.
Imports: data.table, myotherpackage
is in the DESCRIPTION file of mypackage.
myotherpackage 导入了 dplyr,它有几个类似 data.table 函数的函数,所以我每次加载 mypackage 时都会收到这样的警告:
myotherpackage imports dplyr, which has several functions named like the data.table functions, so I get warnings like this everytime I load mypackage:
警告:加载‘mypackage’时用‘dplyr::first’替换之前的导入‘data.table::first’
有没有办法导入data.table的所有功能,例如first"除外?如果需要,我会在代码中使用 data.table::first.或者有更好的处理方法吗?每次有人导入包时,我都试图避免警告.谢谢!
Is there a way to import all the functions of data.table except "first" for example? I'd then use data.table::first in the code if I need to use it. Or is there a better way to handle it? I'm trying to avoid the warning every time someones imports the package. Thank you!
推荐答案
NAMESPACE 文件在这里有点灵活,如 编写 R 扩展.
The NAMESPACE file is somewhat flexible here, as described in Writing R Extensions.
两个主要的导入指令是:
The two main import directives are:
import(PACKAGE)
将命名空间中的所有对象导入到您的包中.第二个选项是使用以下方法进行特定导入:
which imports all objects in the namespace into your package. The second option is to do specific imports using:
importFrom(PACKAGE, foo)
它使您可以访问 foo()
而无需完全限定的引用 PACKAGE::foo()
.
which gives you access to foo()
without needing the fully qualified reference PACKAGE::foo()
.
但这并不是仅有的两种选择.您还可以使用 except
参数来排除少量导入:
But these aren't the only two options. You can also use the except
argument to exclude just a handful of imports:
import(PACKAGE, except=c(foo,bar))
它为您提供了 PACKAGE 命名空间中的所有内容,但 foo()
和 bar()
.这对于避免冲突很有用 - 就像您的情况一样.
which gives you everything from PACKAGE's namespace but foo()
and bar()
. This is useful - as in your case - for avoiding conflicts.
对于 roxygen 来说,弄清楚你可以做什么:
For roxygen, great catch on figuring out that you can do:
#' @rawNamespace import(PACKAGE, except = foo)
通过 roxygen 传递原始 NAMESPACE 指令.
to pass a raw NAMESPACE directive through roxygen.
这篇关于在构建包时导入除一个包外的所有功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!