diff --git a/R/ggplot.R b/R/ggplot.R index 1512ad0b..a82a43a8 100644 --- a/R/ggplot.R +++ b/R/ggplot.R @@ -18,6 +18,7 @@ ggplot_build_set <- function() { ggplot_build <- getFromNamespace("ggplot_build", "ggplot2") .globals$ggplot_build <- getFromNamespace("ggplot_build.ggplot", "ggplot2") assign_in_namespace <- assignInNamespace + ensure_s3_methods_matrix() assign_in_namespace("ggplot_build.ggplot", ggthematic_build, "ggplot2") } @@ -30,6 +31,15 @@ ggplot_build_restore <- function() { } } +# Turns out that calling registerS3method() in an onLoad() hook causes +# assignInNamespace() to no longer work, but here's a workaround for the issue +# https://github.com/rstudio/thematic/issues/90#issuecomment-780224962 +ensure_s3_methods_matrix <- function(pkg = "ggplot2") { + S3 <- .getNamespaceInfo(asNamespace(pkg), "S3methods") + S3 <- matrix(as.character(S3), nrow = nrow(S3), ncol = ncol(S3)) + setNamespaceInfo(asNamespace(pkg), "S3methods", S3) +} + # N.B. If you make changes here, plotly might have to as well! # https://github.com/ropensci/plotly/pull/1801/files#diff-3afd3a8e6a2cbc84a7afc6d2d06ec5e3R429 ggthematic_build <- function(p, ggplot_build = NULL, theme = NULL) {