Skip to content

Commit

Permalink
Demote errors in legend modified expressions (tidyverse#6267)
Browse files Browse the repository at this point in the history
* handle after scale errors

* add test

* add news bullet

* tweak message

* conditional test
  • Loading branch information
teunbrand authored Feb 4, 2025
1 parent 1bfb3c9 commit b87a6e3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down
13 changes: 9 additions & 4 deletions R/geom-.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/_snaps/guide-legend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# unresolved, modified expressions throw a warning (#6264)

Unable to apply staged modifications.
Caused by error:
! object 'prop' not found

9 changes: 9 additions & 0 deletions tests/testthat/test-guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 ------------------------------------------------------------

Expand Down

0 comments on commit b87a6e3

Please sign in to comment.