Skip to content

Unknown theme elements throw warnings instead of errors #5743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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).
* Fixed bug in `coord_radial()` where full circles were not treated as such
(@teunbrand, #5750).
* When legends detect the presence of values in a layer, `NA` is now detected
Expand Down
3 changes: 2 additions & 1 deletion R/theme-elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this require an update of the snapshot

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message is exactly the same so the snapshot file hasn't changed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

expect_snapshot_warning(print(p))

register_theme_elements(
element_tree = list(blablabla = el_def("character", "text"))
Expand Down