Skip to content

Commit fcddd94

Browse files
committed
contingencies for old elements
1 parent 2568789 commit fcddd94

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

R/theme-elements.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,10 @@ check_element <- function(el, elname, element_tree, call = caller_env()) {
10121012
class <- eldef$class
10131013
if (inherits(class, "S7_class")) {
10141014
inherit_ok <- S7::S7_inherits(el, class)
1015+
# For backward compatibility
1016+
# TODO: deprecate next release cycle
1017+
old_s3_inherits <- inherits(el, class@name)
1018+
inherit_ok <- inherit_ok || old_s3_inherits
10151019
} else {
10161020
inherit_ok <- inherits(el, class)
10171021
}

R/theme.R

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,8 @@ calc_element <- function(element, theme, verbose = FALSE, skip_blank = FALSE,
779779
if (!is.null(el_out)) {
780780
class <- element_tree[[element]]$class
781781
if (inherits(class, "S7_class")) {
782-
if (!S7::S7_inherits(el_out, class)) {
782+
old_s3_inherit <- inherits(el_out, class@name)
783+
if (!S7::S7_inherits(el_out, class) && !old_s3_inherit) {
783784
cli::cli_abort("Theme element {.var {element}} must have class {.cls {class@name}}.", call = call)
784785
}
785786
} else {
@@ -923,6 +924,22 @@ S7::method(merge_element, list(margin, S7::class_any)) <-
923924
new
924925
}
925926

927+
# For backward compatibility
928+
# TODO: in subsequent release cycle, start deprecation
929+
S7::method(merge_element, list(S7::new_S3_class("element"), S7::class_any)) <-
930+
function(new, old, ...) {
931+
if (is.null(old) || is_theme_element(old, "blank")) {
932+
return(new)
933+
}
934+
if (S7::S7_inherits(old)) {
935+
old <- S7::props(old)
936+
}
937+
idx <- lengths(new) == 0
938+
idx <- names(idx[idx])
939+
new[idx] <- old[idx]
940+
new
941+
}
942+
926943
#' Combine the properties of two elements
927944
#'
928945
#' @param e1 An element object
@@ -965,6 +982,12 @@ combine_elements <- function(e1, e2) {
965982
}
966983
}
967984

985+
# Backward compatbility
986+
# TODO: deprecate next release cycle
987+
if (inherits(e1, "element") && is_theme_element(e2)) {
988+
return(combine_s3_elements(e1, e2))
989+
}
990+
968991
# If neither of e1 or e2 are element_* objects, return e1
969992
if (!is_theme_element(e1) && !is_theme_element(e2)) {
970993
return(e1)
@@ -1010,6 +1033,24 @@ combine_elements <- function(e1, e2) {
10101033
e1
10111034
}
10121035

1036+
# For backward compatibility
1037+
# TODO: in subsequent release cycle, start deprecation
1038+
combine_s3_elements <- function(e1, e2) {
1039+
if (S7::S7_inherits(e2)) {
1040+
e2 <- S7::props(e2)
1041+
}
1042+
if (is_rel(e1$size)) {
1043+
e1$size <- e2$size * unclass(e1$size)
1044+
}
1045+
if (is_rel(e1$linewidth)) {
1046+
e1$linewidth <- e2$linewidth * unclass(e1$linewidth)
1047+
}
1048+
if (inherits(e1, "element_text")) {
1049+
e1$margin <- combine_elements(e1$margin, e2$margin)
1050+
}
1051+
return(e1)
1052+
}
1053+
10131054
#' @export
10141055
`$.ggplot2::theme` <- function(x, ...) {
10151056
.subset2(x, ...)

0 commit comments

Comments
 (0)