问题描述
有没有办法在 R 中导入另一个名称的包,就像在 Python 中使用 import as
一样,例如将 numpy 导入为 np
?我最近开始使用 package::function
来避免 Hmisc::summarize
和 plyr::summarize
之间的冲突.
Is there a way to import a package with another name in R, the way you might with import as
in Python, e.g. import numpy as np
? I've been starting to use package::function
lately to avoid conflicts between, say, Hmisc::summarize
and plyr::summarize
.
我希望能够分别编写 h::summarize
和 p::summarize
.这在 R 中可能吗?
I'd like to be able to instead write h::summarize
and p::summarize
, respectively. Is this possible in R?
推荐答案
使用 namespace 包以生成另一个命名空间,别名为您感兴趣的命名空间.
Use the namespace package to generate another namespace aliased to the one you are interested in.
library(namespace)
registerNamespace('ggp', loadNamespace('ggplot2'))
data(iris)
ggp::ggplot(iris, ggp::aes(x = Petal.Length, y = Sepal.Length)) + ggp::geom_point()
请注意,这样做的缺点是使包版本控制/安装要求对脚本更加不透明.
Note this has the disadvantage of making the package versioning/installation requirements more opaque for scripts.
这篇关于“导入为"在 R 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!