处理 R 中冲突的命名空间(不同包中的相同函数名):重置包命名空间的优先级 [英] Dealing with conflicting namespaces in R (same function names in different packages): reset precedence of a package namespace

查看:76
本文介绍了处理 R 中冲突的命名空间(不同包中的相同函数名):重置包命名空间的优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R 中来自不同包的命名空间之间的名称冲突可能是危险的,不幸的是 package::function 的使用没有在 R 中通用代码>...

The name conflicts between namespaces from different packages in R can be dangerous, and the use of package::function is unfortunately not generalized in R...

难道没有一个函数可以重置包命名空间的优先级,而不是当前加载的所有其他命名空间?当然我们可以分离然后重新加载包,但没有其他更实用的(一个命令)方法吗?

Isn't there a function that can reset the precedence of a package namespace over all the others currently loaded? Surely we can detach and then reload the package, but isn't there any other, more practical (one-command) way?

因为我经常在 R 会话中遇到许多包和名称冲突,所以我使用以下函数来做到这一点:

Because I often end up with many packages and name conflicts in my R sessions, I use the following function to do that:

set_precedence <- function(pckg) {
  pckg <- deparse(substitute(pckg))
  detach(paste("package", pckg, sep = ":"), unload=TRUE, character.only=TRUE)
  library(pckg, character.only=TRUE)
}
# Example
set_precedence(dplyr)

没有在单个命令中实现此目的的内置方法吗?或者不暗示 detach 并重新加载包的方式,以防加载繁重,并直接在命名空间上工作?

No built-in way to achieve this in a single command? Or a way that doesn't imply detaching and reloading the package, in case it is heavy to load, and working directly on namespaces?

推荐答案

在包名前加上双冒号:::()

例如:

ggplot2::ggplot(data=data, ggplot2::aes(x=x)) +
    ggplot2::geom_histogram()

打字更多,但现在我发现使用 R 时感到不那么焦虑了.

More typing, but I feel so much less anxious using R now that I have found this.

这篇关于处理 R 中冲突的命名空间(不同包中的相同函数名):重置包命名空间的优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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