Skip to content

Ignore empty non-mapped aesthetics #6011

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 4 commits into from
Aug 26, 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
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
Loading