Skip to content
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

Demote errors in legend modified expressions #6267

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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)

* Staged expressions are handled more gracefully if legends cannot resolve them
(@teunbrand, #6264).
* `guide_*()` can now accept two inside legend theme elements:
`legend.position.inside` and `legend.justification.inside`, allowing inside
legends to be placed at different positions. Only inside legends with the same
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()
}
Comment on lines +166 to +174
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This setup means that an issue in one staged expression nullifies all staged expressions, right?

Maybe this is ok as it is not often we have a huge amount of staged expressions in the same layer...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is right. I think it is already a pretty niche situation when you run into one of these errors in the first place.

)

# 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

7 changes: 7 additions & 0 deletions tests/testthat/test-guide-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ test_that("legends can be forced to display unrelated geoms", {
)
})

test_that("unresolved, modified expressions throw a warning (#6264)", {
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