-
Notifications
You must be signed in to change notification settings - Fork 52
Restore compatibility with release ggplot2 #753
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
base: main
Are you sure you want to change the base?
Changes from all commits
ce735f4
befc295
9d38239
9340992
562fe56
3bacebc
876afd7
0a71085
d2179aa
2e694b7
9c33bd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| ^compile_commands\.json$ | ||
| ^\.cache$ | ||
| ^\.vscode$ | ||
| ^\.mcp-console$ | ||
| ^revdep$ | ||
| ^[.]?air[.]toml$ | ||
| ^\.claude$ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| on: | ||
| push: | ||
| branches: [main, master] | ||
| pull_request: | ||
|
|
||
| name: test-ggplot2.yaml | ||
|
|
||
| permissions: read-all | ||
|
|
||
| jobs: | ||
| ggplot2: | ||
| runs-on: ubuntu-latest | ||
| name: ggplot2 (release) | ||
| env: | ||
| GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v6 | ||
|
|
||
| - uses: r-lib/actions/setup-r@v2 | ||
| with: | ||
| r-version: release | ||
|
|
||
| - uses: r-lib/actions/setup-r-dependencies@v2 | ||
| with: | ||
| dependencies: '"hard"' | ||
| extra-packages: any::ggplot2, local::. | ||
| install-pandoc: false | ||
|
|
||
| - name: Test ggplot2 compatibility | ||
| run: | | ||
| library(S7) | ||
| library(ggplot2) | ||
|
|
||
| output <- tempfile(fileext = ".png") | ||
| grDevices::png(filename = output) | ||
|
|
||
| plot <- ggplot( | ||
| data = mtcars, | ||
| mapping = aes(wt, mpg, colour = factor(cyl)) | ||
| ) + | ||
| geom_point() + | ||
| theme(legend.key.spacing = grid::unit(1, "cm")) | ||
| built <- ggplot_build(plot) | ||
| grob <- ggplotGrob(plot) | ||
| print(plot) | ||
| invisible(grDevices::dev.off()) | ||
|
|
||
| stopifnot( | ||
| inherits(plot, "ggplot"), | ||
| inherits(built, "ggplot_built"), | ||
| inherits(grob, "gtable"), | ||
| file.size(output) > 0 | ||
| ) | ||
| shell: Rscript {0} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,4 @@ compile_commands.json | |
| .cache | ||
| .vscode | ||
| .claude/worktrees | ||
| .mcp-console/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -326,10 +326,16 @@ class_inherits <- function(x, what) { | |
| missing = FALSE, | ||
| any = TRUE, | ||
| S4 = methods::is(x, what), | ||
| S7 = has_S7_class(x) && inherits(x, S7_class_name(what)), | ||
| # Class-vector-only objects have no stored class for `has_S7_class()`. | ||
| S7 = inherits(x, "S7_object") && inherits(x, S7_class_name(what)), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this fallback live in |
||
| S7_base = what$class == base_class(x), | ||
| S7_union = some(what$classes, class_inherits, x = x), | ||
| S7_S3 = !isS4(x) && class_dispatch_extends(what$class, class(x)), | ||
| S7_S3 = !isS4(x) && | ||
| class_dispatch_inherits( | ||
| what$class, | ||
| class(x), | ||
| version = attr(what, "_version", exact = TRUE) | ||
| ), | ||
| S7_external = inherits(x, "S7_object") && inherits(x, what$class_name), | ||
| ) | ||
| } | ||
|
|
@@ -373,6 +379,12 @@ class_extends <- function(child, parent) { | |
| } else if (is_external_class(parent)) { | ||
| parent <- resolve_external_class_req(parent) | ||
| class_extends(child, parent) | ||
| } else if (is_S3_class(child) && is_S3_class(parent)) { | ||
| class_dispatch_inherits( | ||
| parent$class, | ||
| child$class, | ||
| version = attr(parent, "_version", exact = TRUE) | ||
| ) | ||
| } else if (is_S4_class(child) || is_S4_class(parent)) { | ||
| child <- class_extends_S4_name(child) | ||
| parent <- class_extends_S4_name(parent) | ||
|
|
@@ -431,6 +443,37 @@ obj_dispatch <- function(x) { | |
|
|
||
| # helpers ----------------------------------------------------------------- | ||
|
|
||
| # Does `child`'s S3 dispatch inherit from `parent`'s? | ||
| # | ||
| # ggplot2 4.0.x relies on the S7 0.2.2 behavior where an S3 class | ||
| # specification can match before shared trailing classes (#747), e.g. `"Coord"` | ||
| # in c("CoordCartesian", "Coord", "ggproto", "gg"). Unversioned definitions | ||
| # preserve that behavior for backward compatibility. Current definitions and | ||
| # downcasts use strict tail matching. | ||
| # S7 wrappers of base/S3 types append "S7_object", which we ignore. | ||
| class_dispatch_inherits <- function(parent, child, version) { | ||
| if (!is.null(version)) { | ||
| return(class_dispatch_extends(parent, child)) | ||
| } | ||
|
|
||
| parent <- drop_S7_object(parent) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only use this path if |
||
| child <- drop_S7_object(child) | ||
| n <- length(parent) | ||
| if (length(child) < n) { | ||
| return(FALSE) | ||
| } | ||
| if (n == 1L) { | ||
| return(parent[[1L]] %in% child) | ||
| } | ||
|
|
||
| for (start in seq_len(length(child) - n + 1L)) { | ||
| if (identical(child[seq.int(start, length.out = n)], parent)) { | ||
| return(TRUE) | ||
| } | ||
| } | ||
| FALSE | ||
| } | ||
|
|
||
| # Does `child`'s dispatch extend `parent`'s? Subclassing only ever prepends | ||
| # more specific classes, so `parent`'s classes must form the tail of `child`'s. | ||
| # S7 wrappers of base/S3 types append "S7_object", which we ignore. | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it would be better to add an explicit
_versionattribute to classes so we could make it clear when we're adding backward compatibility shims?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a
_versionattribute, starting at1L. I used an integer representation version rather than the S7 package version because it only needs to change when the representation changes. Older class definitions remain unversioned, and the backwards compatibility code only runs for those definitions.