覆盖R

时间:2016-11-21 16:38:00

标签: r ggplot2 r-environment

在R中,似乎有一种方法可以覆盖命名空间中的函数(参见here)。我想知道,是否可以从未导出的包中覆盖一个函数

例如,ggplot_build()导出ggplot2。我可以使用以下代码示例覆盖它:

library(ggplot2)    
g <- ggplot(mtcars, aes(x=mpg)) + geom_density()

my.ggplot_build <- function(plot) print(class(plot))

ggplot_build(g)
# Plot rendered

unlockBinding("ggplot_build", as.environment("package:ggplot2"))
assign("ggplot_build", my.ggplot_build, as.environment("package:ggplot2"))
lockBinding("ggplot_build", as.environment("package:ggplot2"))

ggplot_build(g)
# [1] "gg"     "ggplot"

但是,有没有办法覆盖print.ggplot()未导出的ggplot函数?

我可以通过:等三ggplot2:::print.ggplot()访问未导出的功能。有没有办法覆盖这些功能?

0 个答案:

没有答案