Skip to content

Commit b87a6e3

Browse files
authored
Demote errors in legend modified expressions (#6267)
* handle after scale errors * add test * add news bullet * tweak message * conditional test
1 parent 1bfb3c9 commit b87a6e3

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@
302302
particularly for data-points with a low radius near the center
303303
(@teunbrand, #5023).
304304
* All scales now expose the `aesthetics` parameter (@teunbrand, #5841)
305+
* Staged expressions are handled more gracefully if legends cannot resolve them
306+
(@teunbrand, #6264).
305307
* New `theme(legend.key.justification)` to control the alignment of legend keys
306308
(@teunbrand, #3669).
307309
* Added `scale_{x/y}_time(date_breaks, date_minor_breaks, date_labels)`

R/geom-.R

+9-4
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,15 @@ Geom <- ggproto("Geom",
163163
# If any after_scale mappings are detected they will be resolved here
164164
# This order means that they will have access to all default aesthetics
165165
if (length(modifiers) != 0) {
166-
# Set up evaluation environment
167-
modified_aes <- eval_aesthetics(
168-
substitute_aes(modifiers), data,
169-
mask = list(stage = stage_scaled)
166+
modified_aes <- try_fetch(
167+
eval_aesthetics(
168+
substitute_aes(modifiers), data,
169+
mask = list(stage = stage_scaled)
170+
),
171+
error = function(cnd) {
172+
cli::cli_warn("Unable to apply staged modifications.", parent = cnd)
173+
data_frame0()
174+
}
170175
)
171176

172177
# Check that all output are valid data

tests/testthat/_snaps/guide-legend.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# unresolved, modified expressions throw a warning (#6264)
2+
3+
Unable to apply staged modifications.
4+
Caused by error:
5+
! object 'prop' not found
6+

tests/testthat/test-guide-legend.R

+9
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ test_that("legends can be forced to display unrelated geoms", {
136136
)
137137
})
138138

139+
test_that("unresolved, modified expressions throw a warning (#6264)", {
140+
# Snapshot is unstable in lesser R versions
141+
skip_if_not(getRversion() >= "4.3.0")
142+
p <- ggplot(mpg, aes(drv)) +
143+
geom_bar(
144+
aes(fill = stage(drv, after_scale = alpha(fill, prop)))
145+
)
146+
expect_snapshot_warning(ggplot_build(p))
147+
})
139148

140149
# Visual tests ------------------------------------------------------------
141150

0 commit comments

Comments
 (0)