0

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

例如,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(). 有没有办法覆盖这些功能?

4

0 回答 0