在构建包时导入包的所有功能,除了一个 [英] import all the functions of a package except one when building a package
问题描述
我正在构建一个导入 data.table 和另一个包(我们称之为 myotherpackage)的 R 包(mypackage).
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"时将之前的导入data.table::first"替换为dplyr::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)
这使您无需完全限定的参考 PACKAGE::foo()
即可访问 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屋!