Skip to content

Commit aa14c88

Browse files
committed
contingencies for inherits(x, <S7>) on old R versions
1 parent b38f3e5 commit aa14c88

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

R/theme-elements.R

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -948,21 +948,36 @@ check_element <- function(el, elname, element_tree, call = caller_env()) {
948948

949949
# NULL values for elements are OK
950950
if (is.null(el)) return()
951+
951952
class <- eldef$class
952-
if (inherits(class, "S7_class") && S7::S7_inherits(el)) {
953-
if (S7::S7_inherits(el, class) || is_theme_element(el, "blank")) {
953+
if (inherits(class, "S7_class")) {
954+
inherit_ok <- S7::S7_inherits(el, class)
955+
} else {
956+
inherit_ok <- inherits(el, class)
957+
}
958+
959+
if (is.character(class) && any(c("margin", "ggplot2::margin") %in% class)) {
960+
if ("rel" %in% class && is.rel(el)) {
954961
return()
955962
}
963+
if (is.unit(el) && length(el) == 4) {
964+
return()
965+
}
966+
cli::cli_abort(
967+
"The {.var {elname}} theme element must be a {.cls unit} vector of length 4",
968+
call = call
969+
)
956970
}
957971

958-
if (is.character(class) && "margin" %in% class) {
959-
if (!is.unit(el) && length(el) == 4)
960-
cli::cli_abort("The {.var {elname}} theme element must be a {.cls unit} vector of length 4.", call = call)
961-
} else if (!inherits(el, class) && !is_theme_element(el, "blank")) {
962-
if (inherits(class, "S7_class")) {
963-
class <- class@name
964-
}
965-
cli::cli_abort("The {.var {elname}} theme element must be a {.cls {class}} object.", call = call)
972+
# Maybe we should check that `class` is an element class before approving of
973+
# blank elements?
974+
if (inherit_ok || is_theme_element(el, "blank")) {
975+
return()
966976
}
967-
invisible()
977+
978+
class_name <- if (inherits(class, "S7_class")) class@name else class
979+
cli::cli_abort(
980+
"The {.var {elname}} theme element must be a {.cls {class_name}} object.",
981+
call = call
982+
)
968983
}

0 commit comments

Comments
 (0)