You can install the development version from GitHub with:
# install.packages("devtools")
devtools::install_github("TylerGrantSmith/s3sink")
library(s3sink)
plotting_function <- function() {
g <- ggplot2::ggplot(mtcars) + ggplot2::geom_point(ggplot2::aes(mpg, disp))
print(g)
invisible(NULL)
}
sink_s3("print.ggplot", "ggplot2")
plotting_function()
#> Registered S3 method overwritten by 'ggplot2':
#> method from
#> print.ggplot
out <- unsink_s3("print.ggplot")
str(out, 1)
#> List of 1
#> $ :List of 9
#> ..- attr(*, "class")= chr [1:2] "gg" "ggplot"