From 50d337687e0611ca4756c764c9995b0f44e86951 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 4 Mar 2024 12:06:15 +0100 Subject: [PATCH 1/4] demote error to warning --- R/theme-elements.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/theme-elements.R b/R/theme-elements.R index 7ac53ddffb..b8e83c75e4 100644 --- a/R/theme-elements.R +++ b/R/theme-elements.R @@ -620,7 +620,8 @@ validate_element <- function(el, elname, element_tree, call = caller_env()) { eldef <- element_tree[[elname]] if (is.null(eldef)) { - cli::cli_abort("The {.var {elname}} theme element is not defined in the element hierarchy.", call = call) + cli::cli_warn("The {.var {elname}} theme element is not defined in the element hierarchy.", call = call) + return() } # NULL values for elements are OK From 870ce3c37c1ee8f32b46ff071e77160e137ee8a7 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 4 Mar 2024 12:06:37 +0100 Subject: [PATCH 2/4] filter known elements --- R/theme.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/theme.R b/R/theme.R index ecade9aa62..62ac55f3c6 100644 --- a/R/theme.R +++ b/R/theme.R @@ -575,7 +575,8 @@ plot_theme <- function(x, default = theme_get()) { # Check that all elements have the correct class (element_text, unit, etc) validate_theme(theme) - theme + + theme[intersect(names(theme), names(get_element_tree()))] } #' Modify properties of an element in a theme object From f7ea9fb4dc9ae7248504d61e42ec6a1468ab91a6 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 4 Mar 2024 12:06:47 +0100 Subject: [PATCH 3/4] adjust test --- tests/testthat/test-theme.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-theme.R b/tests/testthat/test-theme.R index 146ad29fc8..895d4cf9fc 100644 --- a/tests/testthat/test-theme.R +++ b/tests/testthat/test-theme.R @@ -293,7 +293,7 @@ test_that("incorrect theme specifications throw meaningful errors", { test_that("element tree can be modified", { # we cannot add a new theme element without modifying the element tree p <- ggplot() + theme(blablabla = element_text(colour = "red")) - expect_snapshot_error(print(p)) + expect_snapshot_warning(print(p)) register_theme_elements( element_tree = list(blablabla = el_def("character", "text")) From e6e7546b467303b07424f1c0f586bf7099636a24 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 5 Mar 2024 09:33:47 +0100 Subject: [PATCH 4/4] add news bullet --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 3e5ba492ef..892f7f7ae7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,7 @@ * Patterns and gradients are now also enabled in `geom_sf()` (@teunbrand, #5716). * `stat_bin()` deals with non-finite breaks better (@teunbrand, #5665). +* Theme elements that do not exist now throw warnings instead of errors (#5719). # ggplot2 3.5.0