Skip to content

Commit 458bf80

Browse files
authored
Only warn about empty aesthetics when data size is known (#6690)
* move warning about empty aesthetics * test for warning during build phase
1 parent cd852f7 commit 458bf80

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

R/layer.R

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,6 @@ layer <- function(geom = NULL, stat = NULL,
142142
if (any(pattern)) {
143143
aes_params[pattern] <- lapply(aes_params[pattern], list)
144144
}
145-
# Drop empty aesthetics
146-
empty_aes <- names(aes_params)[lengths(aes_params) == 0]
147-
if (length(empty_aes) > 0) {
148-
cli::cli_warn(
149-
"Ignoring empty aesthetic{?s}: {.arg {empty_aes}}.",
150-
call = call_env
151-
)
152-
aes_params <- aes_params[setdiff(names(aes_params), empty_aes)]
153-
}
154145

155146
# Warn about extra params and aesthetics
156147
extra_param <- setdiff(names(params), all)
@@ -786,6 +777,16 @@ Layer <- ggproto("Layer", NULL,
786777
# Combine aesthetics, defaults, & params
787778
if (empty(data)) return(data)
788779

780+
# Drop empty aesthetics
781+
empty_aes <- names(params)[lengths(params) == 0]
782+
if (length(empty_aes) > 0) {
783+
cli::cli_warn(
784+
"Ignoring empty aesthetic{?s}: {.arg {empty_aes}}.",
785+
call = self$constructor
786+
)
787+
params <- params[setdiff(names(params), empty_aes)]
788+
}
789+
789790
aesthetics <- self$computed_mapping
790791
modifiers <- aesthetics[is_scaled_aes(aesthetics) | is_staged_aes(aesthetics) | is_themed_aes(aesthetics)]
791792

tests/testthat/test-layer.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ test_that("unknown aesthetics create warning", {
2929
})
3030

3131
test_that("empty aesthetics create warning", {
32-
expect_snapshot_warning(geom_point(fill = NULL, shape = character()))
32+
p <- ggplot(mtcars) + geom_point(aes(disp, mpg), fill = NULL, shape = character())
33+
expect_snapshot_warning(ggplot_build(p))
3334
})
3435

3536
test_that("invalid aesthetics throws errors", {

0 commit comments

Comments
 (0)