Skip to content

Commit

Permalink
Close #90: Get thematic working with packages that use registerS3meth…
Browse files Browse the repository at this point in the history
…od() inside .onLoad() to register ggplot2 methods
  • Loading branch information
cpsievert committed Feb 17, 2021
1 parent 03b8c7b commit 0cb0a1e
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions R/ggplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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) {
Expand Down

0 comments on commit 0cb0a1e

Please sign in to comment.