Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get thematic working with packages that use registerS3method() inside .onLoad() to register ggplot2 methods #91

Merged
merged 4 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
14 changes: 14 additions & 0 deletions R/ggplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,32 @@ 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")
}

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) {
Expand Down
Binary file modified tests/testthat/agg_png/tests/shinytest/mytest-expected-mac/001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/testthat/agg_png/tests/shinytest/mytest-expected-win/001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
},
"output": {
"__reactivedoc__": {
"html": "[html data sha1: bba70ec94501a4898cb719daf599f07ff70cd290]",
"html": "[html data sha1: af3206cd4165f61da1bc6a760fc097ceb10273e5]",
"deps": [
{
"name": "jquery",
Expand Down