From e7f05ba9b6387634794212e8b5a09197fe2f8b4b Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 5 Feb 2025 14:56:40 +0100 Subject: [PATCH 1/4] class for skipping labels --- R/aes.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/aes.R b/R/aes.R index 045d388d8a..5e53443988 100644 --- a/R/aes.R +++ b/R/aes.R @@ -360,7 +360,7 @@ aes_all <- function(vars) { # refer to the data mask structure( lapply(vars, function(x) new_quosure(as.name(x), emptyenv())), - class = "uneval" + class = c("unlabelled_uneval", "uneval") ) } From 1a8cfe3096945b9daa56dab62a337004a0ac41d2 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 5 Feb 2025 14:56:55 +0100 Subject: [PATCH 2/4] skip unlabelled_uneval class --- R/labels.R | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/R/labels.R b/R/labels.R index a736e2bf54..27c1e96de6 100644 --- a/R/labels.R +++ b/R/labels.R @@ -24,10 +24,16 @@ setup_plot_labels <- function(plot, layers, data) { # Find labels from every layer for (i in seq_along(layers)) { layer <- layers[[i]] - exclude <- names(layer$aes_params) + mapping <- layer$computed_mapping + if (inherits(mapping, "unlabelled_uneval")) { + next + } + mapping <- strip_stage(mapping) mapping <- strip_dots(mapping, strip_pronoun = TRUE) + + exclude <- names(layer$aes_params) mapping <- mapping[setdiff(names(mapping), exclude)] # Acquire default labels From 11206862ba97c86cebfe835afa3cc9cfc43adb1a Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 5 Feb 2025 14:57:08 +0100 Subject: [PATCH 3/4] prevent re-classing upon validation --- R/layer.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/layer.R b/R/layer.R index 6be74b5d72..4cf2e09417 100644 --- a/R/layer.R +++ b/R/layer.R @@ -210,6 +210,8 @@ validate_mapping <- function(mapping, call = caller_env()) { } cli::cli_abort(msg, call = call) + } else { + return(mapping) } # For backward compatibility with pre-tidy-eval layers From 1061200ee28b5a1714e2954c4c8dfd060aafc7a8 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Wed, 5 Feb 2025 15:11:51 +0100 Subject: [PATCH 4/4] add test --- tests/testthat/test-labels.R | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/testthat/test-labels.R b/tests/testthat/test-labels.R index 172eca6364..4befce8af6 100644 --- a/tests/testthat/test-labels.R +++ b/tests/testthat/test-labels.R @@ -83,6 +83,13 @@ test_that("Labels from static aesthetics are ignored (#6003)", { expect_null(get_labs(p)$colour) }) +test_that("Labels from annotations are ignored (#6316)", { + df <- data.frame(a = 1, b = 2) + p <- ggplot(df, aes(a, b)) + annotate("point", x = 1, y = 2) + geom_point() + labs <- get_labs(p) + expect_equal(labs[c("x", "y")], list(x = "a", y = "b")) +}) + test_that("alt text is returned", { p <- ggplot(mtcars, aes(mpg, disp)) + geom_point()