From 861518ad236d9b9a3f41558921f2c39eae40b356 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 8 Dec 2025 14:16:23 +0100 Subject: [PATCH 1/2] improve error message --- R/scale-.R | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/R/scale-.R b/R/scale-.R index 47b63961ea..42bf786c28 100644 --- a/R/scale-.R +++ b/R/scale-.R @@ -1339,8 +1339,11 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale, pal_match <- vec_slice(pal, match(as.character(x), limits, nomatch = vec_size(pal))) - if (!is.na(na_value)) { - vec_slice(pal_match, vec_detect_missing(x)) <- na_value + if (!is.na(na_value) && vec_any_missing(x)) { + pal_match <- vec_assign( + pal_match, vec_detect_missing(x), na_value, + x_arg = "palette", value_arg = "na.value" + ) } pal_match }, From a3bc14a94e833fbd8a594aea59294e3f4d5f0593 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 8 Dec 2025 14:23:04 +0100 Subject: [PATCH 2/2] add snapshot test --- tests/testthat/_snaps/scale-.md | 8 ++++++++ tests/testthat/test-scale-.R | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/tests/testthat/_snaps/scale-.md b/tests/testthat/_snaps/scale-.md index 8c26a8fc9f..570850316b 100644 --- a/tests/testthat/_snaps/scale-.md +++ b/tests/testthat/_snaps/scale-.md @@ -20,6 +20,14 @@ The `scale_name` argument of `binned_scale()` is deprecated as of ggplot2 3.5.0. +# mismatch between `na.value` and `palette` throws error + + Code + sc$map(x) + Condition + Error in `vec_assign()`: + ! Can't convert `na.value` to match type of `palette` . + # continuous scales warn about faulty `limits` Code diff --git a/tests/testthat/test-scale-.R b/tests/testthat/test-scale-.R index 49ec77652f..80bad5f4eb 100644 --- a/tests/testthat/test-scale-.R +++ b/tests/testthat/test-scale-.R @@ -138,6 +138,16 @@ test_that("Using `scale_name` prompts deprecation message", { }) +test_that("mismatch between `na.value` and `palette` throws error", { + x <- c("A", "B", "C", NA) + sc <- scale_shape_manual( + values = c("circle", "triangle", "square"), + na.value = 4 + ) + sc$train(x) + expect_snapshot(sc$map(x), error = TRUE) +}) + # Continuous scales ------------------------------------------------------- test_that("limits with NA are replaced with the min/max of the data for continuous scales", {