Skip to content

Commit 77cb52d

Browse files
committed
workaround for old R versions
1 parent 5d41f0e commit 77cb52d

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ S3method("$",ggproto)
66
S3method("$",ggproto_parent)
77
S3method("$<-","ggplot2::gg")
88
S3method("$<-","ggplot2::mapping")
9-
S3method("+",gg)
109
S3method("[","ggplot2::gg")
1110
S3method("[","ggplot2::mapping")
1211
S3method("[",mapped_discrete)

R/plot-construction.R

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
#' @param e1 An object of class [ggplot()] or a [theme()].
2525
#' @param e2 A plot component, as described below.
2626
#' @seealso [theme()]
27-
#' @export
28-
#' @method + gg
2927
#' @rdname gg-add
3028
#' @examples
3129
#' base <-
@@ -39,7 +37,7 @@
3937
#' # Alternatively, you can add multiple components with a list.
4038
#' # This can be useful to return from a function.
4139
#' base + list(subset(mpg, fl == "p"), geom_smooth())
42-
"+.gg" <- function(e1, e2) {
40+
add_gg <- function(e1, e2) {
4341
if (missing(e2)) {
4442
cli::cli_abort(c(
4543
"Cannot use {.code +} with a single argument.",
@@ -52,6 +50,8 @@
5250
e2name <- deparse(substitute(e2))
5351

5452
if (is.theme(e1)) add_theme(e1, e2, e2name)
53+
# The `add_ggplot()` branch here is for backward compatibility with R < 4.3.0
54+
else if (is.ggplot(e1)) add_ggplot(e1, e2, e2name)
5555
else if (is.ggproto(e1)) {
5656
cli::cli_abort(c(
5757
"Cannot add {.cls ggproto} objects together.",
@@ -60,6 +60,10 @@
6060
}
6161
}
6262

63+
if (getRversion() < "4.3.0") {
64+
S7::method(`+`, list(class_S3_gg, S7::class_any)) <- add_gg
65+
}
66+
6367
S7::method(`+`, list(class_ggplot, S7::class_any)) <- function(e1, e2) {
6468
e2name <- deparse(substitute(e2, env = caller_env(2)))
6569
add_ggplot(e1, e2, e2name)
@@ -73,7 +77,13 @@ S7::method(`+`, list(class_theme, S7::class_any)) <- function(e1, e2) {
7377

7478
#' @rdname gg-add
7579
#' @export
76-
"%+%" <- function(e1, e2) e1 + e2
80+
"%+%" <- function(e1, e2) {
81+
if (getRversion() < "4.3.0") {
82+
add_gg(e1, e2)
83+
} else {
84+
`+`(e1, e2)
85+
}
86+
}
7787

7888
add_ggplot <- function(p, object, objectname) {
7989
if (is.null(object)) return(p)

R/zzz.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ on_load(
3030
vars <- dplyr::vars
3131
}
3232
)
33+
on_load(
34+
if (getRversion() > "4.3.0") registerS3method("+", "gg", add_gg)
35+
)
3336
on_load(S7::methods_register())
3437
.onLoad <- function(...) {
3538
run_on_load()

man/gg-add.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)