Conflicting function names in R: reset precedence of a package namespace over more recently loaded packages

落花浮王杯 提交于 2021-01-26 08:54:10

问题


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?

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)

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?


回答1:


I would suggest taking a look at the conflicted package.




回答2:


Prefix the package name with a double colon: <package>::<function>()

For instance:

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

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



来源:https://stackoverflow.com/questions/36330105/conflicting-function-names-in-r-reset-precedence-of-a-package-namespace-over-mo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!