0

In R, there appears to be a way to override functions within a namespace (see here). I'm wondering, is it possible to override a function from a package that was not exported.

For example, ggplot_build() is exported by ggplot2. I can override it using the following code example:

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"

However, is there a way to override the print.ggplot() function that is not exported by ggplot?

I can access unexported functions via triple : such as ggplot2:::print.ggplot(). Is there a way to override these functions?

Community
  • 1
  • 1
Megatron
  • 15,909
  • 12
  • 89
  • 97

0 Answers0