Skip to content

Commit 2607597

Browse files
committed
backward compatibility for ggplot class
1 parent 0310be8 commit 2607597

File tree

8 files changed

+57
-10
lines changed

8 files changed

+57
-10
lines changed

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method("$","ggplot2::ggplot")
34
S3method("$","ggplot2::theme")
45
S3method("$",ggproto)
56
S3method("$",ggproto_parent)
7+
S3method("$<-","ggplot2::ggplot")
68
S3method("$<-","ggplot2::mapping")
79
S3method("+",gg)
10+
S3method("[","ggplot2::ggplot")
811
S3method("[","ggplot2::mapping")
912
S3method("[",mapped_discrete)
13+
S3method("[<-","ggplot2::ggplot")
1014
S3method("[<-","ggplot2::mapping")
1115
S3method("[<-",mapped_discrete)
16+
S3method("[[","ggplot2::ggplot")
1217
S3method("[[",ggproto)
18+
S3method("[[<-","ggplot2::ggplot")
1319
S3method("[[<-","ggplot2::mapping")
1420
S3method(.DollarNames,ggproto)
1521
S3method(as.data.frame,mapped_discrete)

R/bench.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
benchplot <- function(x) {
1616
x <- enquo(x)
1717
construct <- system.time(x <- eval_tidy(x))
18-
check_inherits(x, "ggplot")
18+
check_inherits(x, "ggplot2::ggplot")
1919

2020
build <- system.time(data <- ggplot_build(x))
2121
render <- system.time(grob <- ggplot_gtable(data))

R/facet-.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ check_vars <- function(x) {
493493
}
494494
# Native pipe have higher precedence than + so any type of gg object can be
495495
# expected here, not just ggplot
496-
if (inherits(x, "gg")) {
496+
if (S7::S7_inherits(x, class_gg)) {
497497
cli::cli_abort(c(
498498
"Please use {.fn vars} to supply facet variables.",
499499
"i" = "Did you use {.code %>%} or {.code |>} instead of {.code +}?"

R/facet-grid-.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ grid_as_facets_list <- function(rows, cols) {
196196
msg <- "{.arg rows} must be {.code NULL} or a {.fn vars} list if {.arg cols} is a {.fn vars} list."
197197
# Native pipe have higher precedence than + so any type of gg object can be
198198
# expected here, not just ggplot
199-
if (inherits(rows, "gg")) {
199+
if (S7::S7_inherits(rows, class_gg)) {
200200
msg <- c(
201201
msg,
202202
"i" = "Did you use {.code %>%} or {.code |>} instead of {.code +}?"

R/layer-sf.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ LayerSf <- ggproto("LayerSf", Layer,
4545
# automatically determine the name of the geometry column
4646
# and add the mapping if it doesn't exist
4747
if ((isTRUE(self$inherit.aes) && is.null(self$computed_mapping$geometry) &&
48-
is.null(plot$computed_mapping$geometry)) ||
48+
is.null(self$computed_mapping$geometry)) ||
4949
(!isTRUE(self$inherit.aes) && is.null(self$computed_mapping$geometry))) {
5050
if (is_sf(data)) {
5151
geometry_col <- attr(data, "sf_column")

R/layer.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ validate_mapping <- function(mapping, call = caller_env()) {
205205
msg <- "{.arg mapping} must be created by {.fn aes}."
206206
# Native pipe have higher precedence than + so any type of gg object can be
207207
# expected here, not just ggplot
208-
if (inherits(mapping, "gg")) {
208+
if (S7::S7_inherits(mapping, class_gg)) {
209209
msg <- c(msg, "i" = "Did you use {.code %>%} or {.code |>} instead of {.code +}?")
210210
}
211211

R/plot.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ ggplot.default <- function(data = NULL, mapping = aes(), ...,
153153
layout = ggproto(NULL, Layout),
154154
labels = labs()
155155
)
156+
class(p) <- union("ggplot", class(p))
156157

157158
set_last_plot(p)
158159
p
@@ -243,3 +244,38 @@ S7::method(print, class_ggplot) <-
243244

244245
invisible(x)
245246
}
247+
248+
#' @export
249+
`$.ggplot2::ggplot` <- function(x, i) {
250+
`[[`(S7::props(x), i)
251+
}
252+
253+
#' @export
254+
`$<-.ggplot2::ggplot` <- function(x, i, value) {
255+
S7::props(x) <- `$<-`(S7::props(x), i, value)
256+
x
257+
}
258+
259+
#' @export
260+
`[.ggplot2::ggplot` <- function(x, i) {
261+
`[`(S7::props(x), i)
262+
}
263+
264+
#' @export
265+
`[<-.ggplot2::ggplot` <- function(x, i, value) {
266+
S7::props(x) <- `[<-`(S7::props(x), i, value)
267+
x
268+
}
269+
270+
#' @export
271+
`[[.ggplot2::ggplot` <- function(x, i) {
272+
`[[`(S7::props(x), i)
273+
}
274+
275+
#' @export
276+
`[[<-.ggplot2::ggplot` <- function(x, i, value) {
277+
S7::props(x) <- `[[<-`(S7::props(x), i, value)
278+
x
279+
}
280+
281+

tests/testthat/test-qplot.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@ test_that("qplot works with variables in data frame and parent env", {
33
y <- 1:10
44
b <- 1:10
55

6+
67
lifecycle::expect_deprecated(
7-
expect_s3_class(qplot(x, y, data = df), "ggplot")
8+
p <- qplot(x, y, data = df)
89
)
10+
expect_s7_class(p, class_ggplot)
911
lifecycle::expect_deprecated(
10-
expect_s3_class(qplot(x, y, data = df, colour = a), "ggplot")
12+
p <- qplot(x, y, data = df, colour = a)
1113
)
14+
expect_s7_class(p, class_ggplot)
1215
lifecycle::expect_deprecated(
13-
expect_s3_class(qplot(x, y, data = df, colour = b), "ggplot")
16+
p <- qplot(x, y, data = df, colour = b)
1417
)
18+
expect_s7_class(p, class_ggplot)
1519

1620
bin <- 1
1721
lifecycle::expect_deprecated(
18-
expect_s3_class(qplot(x, data = df, binwidth = bin), "ggplot")
22+
p <- qplot(x, data = df, binwidth = bin)
1923
)
24+
expect_s7_class(p, class_ggplot)
2025
})
2126

2227
test_that("qplot works in non-standard environments", {
@@ -27,7 +32,7 @@ test_that("qplot works in non-standard environments", {
2732
qplot(x, breaks = 0:`-1-`)
2833
})
2934
)
30-
expect_s3_class(p, "ggplot")
35+
expect_s7_class(p, class_ggplot)
3136
})
3237

3338
test_that("qplot() evaluates constants in the right place", {

0 commit comments

Comments
 (0)