Skip to content

Commit 50b3515

Browse files
authored
Unknown theme elements throw warnings instead of errors (#5743)
* demote error to warning * filter known elements * adjust test * add news bullet
1 parent ce6eed8 commit 50b3515

File tree

4 files changed

+6
-3
lines changed

4 files changed

+6
-3
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Patterns and gradients are now also enabled in `geom_sf()`
1313
(@teunbrand, #5716).
1414
* `stat_bin()` deals with non-finite breaks better (@teunbrand, #5665).
15+
* Theme elements that do not exist now throw warnings instead of errors (#5719).
1516
* Fixed bug in `coord_radial()` where full circles were not treated as such
1617
(@teunbrand, #5750).
1718
* When legends detect the presence of values in a layer, `NA` is now detected

R/theme-elements.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,8 @@ validate_element <- function(el, elname, element_tree, call = caller_env()) {
620620
eldef <- element_tree[[elname]]
621621

622622
if (is.null(eldef)) {
623-
cli::cli_abort("The {.var {elname}} theme element is not defined in the element hierarchy.", call = call)
623+
cli::cli_warn("The {.var {elname}} theme element is not defined in the element hierarchy.", call = call)
624+
return()
624625
}
625626

626627
# NULL values for elements are OK

R/theme.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,8 @@ plot_theme <- function(x, default = theme_get()) {
575575
# Check that all elements have the correct class (element_text, unit, etc)
576576
validate_theme(theme)
577577

578-
theme
578+
579+
theme[intersect(names(theme), names(get_element_tree()))]
579580
}
580581

581582
#' Modify properties of an element in a theme object

tests/testthat/test-theme.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ test_that("incorrect theme specifications throw meaningful errors", {
293293
test_that("element tree can be modified", {
294294
# we cannot add a new theme element without modifying the element tree
295295
p <- ggplot() + theme(blablabla = element_text(colour = "red"))
296-
expect_snapshot_error(print(p))
296+
expect_snapshot_warning(print(p))
297297

298298
register_theme_elements(
299299
element_tree = list(blablabla = el_def("character", "text"))

0 commit comments

Comments
 (0)