diff --git a/NEWS.md b/NEWS.md index 74b049ace5..ef806483df 100644 --- a/NEWS.md +++ b/NEWS.md @@ -302,6 +302,8 @@ particularly for data-points with a low radius near the center (@teunbrand, #5023). * All scales now expose the `aesthetics` parameter (@teunbrand, #5841) +* Staged expressions are handled more gracefully if legends cannot resolve them + (@teunbrand, #6264). * New `theme(legend.key.justification)` to control the alignment of legend keys (@teunbrand, #3669). * Added `scale_{x/y}_time(date_breaks, date_minor_breaks, date_labels)` diff --git a/R/geom-.R b/R/geom-.R index f8b5027438..843bd4c11c 100644 --- a/R/geom-.R +++ b/R/geom-.R @@ -163,10 +163,15 @@ Geom <- ggproto("Geom", # If any after_scale mappings are detected they will be resolved here # This order means that they will have access to all default aesthetics if (length(modifiers) != 0) { - # Set up evaluation environment - modified_aes <- eval_aesthetics( - substitute_aes(modifiers), data, - mask = list(stage = stage_scaled) + modified_aes <- try_fetch( + eval_aesthetics( + substitute_aes(modifiers), data, + mask = list(stage = stage_scaled) + ), + error = function(cnd) { + cli::cli_warn("Unable to apply staged modifications.", parent = cnd) + data_frame0() + } ) # Check that all output are valid data diff --git a/tests/testthat/_snaps/guide-legend.md b/tests/testthat/_snaps/guide-legend.md new file mode 100644 index 0000000000..7369171c92 --- /dev/null +++ b/tests/testthat/_snaps/guide-legend.md @@ -0,0 +1,6 @@ +# unresolved, modified expressions throw a warning (#6264) + + Unable to apply staged modifications. + Caused by error: + ! object 'prop' not found + diff --git a/tests/testthat/test-guide-legend.R b/tests/testthat/test-guide-legend.R index c68ab03297..cd2311ee93 100644 --- a/tests/testthat/test-guide-legend.R +++ b/tests/testthat/test-guide-legend.R @@ -136,6 +136,15 @@ test_that("legends can be forced to display unrelated geoms", { ) }) +test_that("unresolved, modified expressions throw a warning (#6264)", { + # Snapshot is unstable in lesser R versions + skip_if_not(getRversion() >= "4.3.0") + p <- ggplot(mpg, aes(drv)) + + geom_bar( + aes(fill = stage(drv, after_scale = alpha(fill, prop))) + ) + expect_snapshot_warning(ggplot_build(p)) +}) # Visual tests ------------------------------------------------------------