diff --git a/NEWS.md b/NEWS.md index b176ff66..f4bd4d73 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # thematic 0.1.1.9000 +* Closed #90: Enabling thematic no longer fails when used with package(s) that resgiter `{ggplot2}` methods via `registerS3method()` within `.onLoad()` (`{zoo}` is one such package that does this). (#90) * Closed #86: thematic_rmd() now works as expected in `shiny: runtime` Rmd documents (#87) * Closed #82: Fixed a bug with element_blank() in plot-specific user code not being respected by thematic. (#83) diff --git a/R/ggplot.R b/R/ggplot.R index 1512ad0b..e40c9bc4 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") } @@ -25,11 +26,24 @@ ggplot_build_restore <- function() { if (is.function(.globals$ggplot_build)) { ggplot_build <- getFromNamespace("ggplot_build", "ggplot2") assign_in_namespace <- assignInNamespace + ensure_s3_methods_matrix() assign_in_namespace("ggplot_build.ggplot", .globals$ggplot_build, "ggplot2") rm("ggplot_build", envir = .globals) } } +# When registerS3method() is called in an onLoad() hook, it causes +# assignInNamespace() to no longer work for S3 methods because this +# S3methods namespace info gets coerced into a list +# (which assignInNamespace() isn't expecting). Thus, here, we make +# sure that the info is a matrix before calling assignInNamespace() +# 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) { diff --git a/tests/testthat/agg_png/tests/shinytest/mytest-expected-mac/001.png b/tests/testthat/agg_png/tests/shinytest/mytest-expected-mac/001.png index 8f618815..52e03895 100644 Binary files a/tests/testthat/agg_png/tests/shinytest/mytest-expected-mac/001.png and b/tests/testthat/agg_png/tests/shinytest/mytest-expected-mac/001.png differ diff --git a/tests/testthat/agg_png/tests/shinytest/mytest-expected-win/001.png b/tests/testthat/agg_png/tests/shinytest/mytest-expected-win/001.png index caa0b08f..af623ab3 100644 Binary files a/tests/testthat/agg_png/tests/shinytest/mytest-expected-win/001.png and b/tests/testthat/agg_png/tests/shinytest/mytest-expected-win/001.png differ diff --git a/tests/testthat/shiny_runtime/tests/shinytest/mytest-expected-win/001.json b/tests/testthat/shiny_runtime/tests/shinytest/mytest-expected-win/001.json index 3ff9c91b..4913abeb 100644 --- a/tests/testthat/shiny_runtime/tests/shinytest/mytest-expected-win/001.json +++ b/tests/testthat/shiny_runtime/tests/shinytest/mytest-expected-win/001.json @@ -4,7 +4,7 @@ }, "output": { "__reactivedoc__": { - "html": "[html data sha1: bba70ec94501a4898cb719daf599f07ff70cd290]", + "html": "[html data sha1: af3206cd4165f61da1bc6a760fc097ceb10273e5]", "deps": [ { "name": "jquery",