From 6002c9fcadda6b9a5e11c9f3b17624a28e3c3c71 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 6 Jan 2025 09:14:00 +0100 Subject: [PATCH 1/5] handle after scale errors --- R/geom-.R | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/R/geom-.R b/R/geom-.R index 50bdeb66a6..6c9db7c043 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 legend modifications.", parent = cnd) + data_frame0() + } ) # Check that all output are valid data From 1d3a84524ca4f7e98ee631b679747c444ee1dff5 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 6 Jan 2025 09:20:17 +0100 Subject: [PATCH 2/5] add test --- tests/testthat/_snaps/guide-legend.md | 6 ++++++ tests/testthat/test-guide-legend.R | 7 +++++++ 2 files changed, 13 insertions(+) create mode 100644 tests/testthat/_snaps/guide-legend.md diff --git a/tests/testthat/_snaps/guide-legend.md b/tests/testthat/_snaps/guide-legend.md new file mode 100644 index 0000000000..2d16fa75bf --- /dev/null +++ b/tests/testthat/_snaps/guide-legend.md @@ -0,0 +1,6 @@ +# unresolved, modified expressions throw a warning (#6264) + + Unable to apply legend 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 d4a47c145e..929f5e7e5d 100644 --- a/tests/testthat/test-guide-legend.R +++ b/tests/testthat/test-guide-legend.R @@ -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 ------------------------------------------------------------ From 59083277dc3922755943c6232e36605c15749ee4 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 6 Jan 2025 09:21:21 +0100 Subject: [PATCH 3/5] add news bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index dd574ac1aa..3c3c9f777b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 From 1a290e57d9fba97d6d07ab14d3c1fa85a80b2b65 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Fri, 10 Jan 2025 09:32:58 +0100 Subject: [PATCH 4/5] tweak message --- R/geom-.R | 2 +- tests/testthat/_snaps/guide-legend.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/geom-.R b/R/geom-.R index 6c9db7c043..322114d032 100644 --- a/R/geom-.R +++ b/R/geom-.R @@ -169,7 +169,7 @@ Geom <- ggproto("Geom", mask = list(stage = stage_scaled) ), error = function(cnd) { - cli::cli_warn("Unable to apply legend modifications.", parent = cnd) + cli::cli_warn("Unable to apply staged modifications.", parent = cnd) data_frame0() } ) diff --git a/tests/testthat/_snaps/guide-legend.md b/tests/testthat/_snaps/guide-legend.md index 2d16fa75bf..7369171c92 100644 --- a/tests/testthat/_snaps/guide-legend.md +++ b/tests/testthat/_snaps/guide-legend.md @@ -1,6 +1,6 @@ # unresolved, modified expressions throw a warning (#6264) - Unable to apply legend modifications. + Unable to apply staged modifications. Caused by error: ! object 'prop' not found From abf23f3d5f4cad5ef53c7aa91925b11d26c75b4b Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Tue, 4 Feb 2025 11:26:53 +0100 Subject: [PATCH 5/5] conditional test --- tests/testthat/test-guide-legend.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testthat/test-guide-legend.R b/tests/testthat/test-guide-legend.R index 6f7234d232..cd2311ee93 100644 --- a/tests/testthat/test-guide-legend.R +++ b/tests/testthat/test-guide-legend.R @@ -137,6 +137,8 @@ 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)))