Skip to content

Commit

Permalink
Ignore empty non-mapped aesthetics (#6011)
Browse files Browse the repository at this point in the history
* drop empty aesthetics with warning

* add test

* add news bullet
  • Loading branch information
teunbrand committed Aug 26, 2024
1 parent 8bb9641 commit e69687a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* Passing empty unmapped aesthetics to layers raises a warning instead of
throwing an error (@teunbrand, #6009).
* Moved {mgcv} from Imports to Suggests (@teunbrand, #5986)
* New `reset_geom_defaults()` and `reset_stat_defaults()` to restore all geom or
stat default aesthetics at once (@teunbrand, #5975).
Expand Down
9 changes: 9 additions & 0 deletions R/layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ layer <- function(geom = NULL, stat = NULL,
if (any(pattern)) {
aes_params[pattern] <- lapply(aes_params[pattern], list)
}
# Drop empty aesthetics
empty_aes <- names(aes_params)[lengths(aes_params) == 0]
if (length(empty_aes) > 0) {
cli::cli_warn(
"Ignoring empty aesthetic{?s}: {.arg {empty_aes}}.",
call = call_env
)
aes_params <- aes_params[setdiff(names(aes_params), empty_aes)]
}

# Warn about extra params and aesthetics
extra_param <- setdiff(names(params), all)
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ test_that("unknown aesthetics create warning", {
expect_warning(geom_point(aes(blah = "red")), "unknown aesthetics")
})

test_that("empty aesthetics create warning", {
expect_warning(
geom_point(fill = NULL, shape = character()),
"Ignoring empty aesthetics"
)
})

test_that("invalid aesthetics throws errors", {
# We want to test error and ignore the scale search message
suppressMessages({
Expand Down

0 comments on commit e69687a

Please sign in to comment.