导入软件包的所有功能(构建软件包时除外) [英] import all the functions of a package except one when building a package

查看:89
本文介绍了导入软件包的所有功能(构建软件包时除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个R程序包( mypackage ),该程序包将导入data.table和另一个程序包(简称为 myotherpackage )。

I'm building an R package (mypackage) that imports data.table and another package (let's call it myotherpackage).

导入: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:

警告:加载时,将先前导入的 data.table :: first替换为 dplyr :: first mypackage'

是否有一种方法可以导入data.table的所有功能,例如 first除外?然后,如果需要使用代码,请先在代码中使用data.table :::。
还是有更好的方法来处理它?我试图避免每次有人进口包裹时发出警告。谢谢!

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)

,您无需完全限定的引用<$ c $就可以访问 foo() c> 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屋!

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