From 35c370ff6216f1ddd9ffe0ee54c38b4d781b3c24 Mon Sep 17 00:00:00 2001 From: Valentine Rahier Date: Thu, 8 Oct 2026 09:30:18 +0200 Subject: [PATCH 1/2] fix: fix mixture detection when Dominance contains NA --- R/aesthetics.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/aesthetics.R b/R/aesthetics.R index 5b0a0d4..049301e 100644 --- a/R/aesthetics.R +++ b/R/aesthetics.R @@ -26,7 +26,9 @@ detect_mixture <- function(sim_situation) { is_Dominance <- grep("Dominance", x = colnames(sim_situation), fixed = TRUE) if (length(is_Dominance) > 0) { - is_mixture <- length(unique(sim_situation[[is_Dominance]])) > 1 + # NA values come from binding versions without a Dominance column: + dominance <- sim_situation[[is_Dominance]] + is_mixture <- length(unique(dominance[!is.na(dominance)])) > 1 } else { is_mixture <- FALSE } From 838585a3df189f1763701e0065f69f8d14816f8b Mon Sep 17 00:00:00 2001 From: Valentine Rahier Date: Thu, 8 Oct 2026 11:00:11 +0200 Subject: [PATCH 2/2] add tests --- tests/testthat/test-detect_mixture.R | 8 ++++++++ tests/testthat/test-generic_statistics.R | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/tests/testthat/test-detect_mixture.R b/tests/testthat/test-detect_mixture.R index 210276d..0007b87 100644 --- a/tests/testthat/test-detect_mixture.R +++ b/tests/testthat/test-detect_mixture.R @@ -9,4 +9,12 @@ test_that("detect_mixture", { sim_data <- data.frame(lai = c(1, 1.2)) expect_false(CroPlotR:::detect_mixture(sim_data)) + + # NA values (e.g. versions bound together, only one with a Dominance column) + # are not counted as a distinct value: + sim_data <- data.frame(Dominance = c("0", "0", NA, NA)) + expect_false(CroPlotR:::detect_mixture(sim_data)) + + sim_data <- data.frame(Dominance = c("Principal", "Associated", NA)) + expect_true(CroPlotR:::detect_mixture(sim_data)) }) diff --git a/tests/testthat/test-generic_statistics.R b/tests/testthat/test-generic_statistics.R index 21499fd..06ae280 100644 --- a/tests/testthat/test-generic_statistics.R +++ b/tests/testthat/test-generic_statistics.R @@ -66,6 +66,24 @@ test_that("statistics summary: three groups", { expect_equal(length(unique(df_stats$situation)), 1) }) +test_that("statistics summary: sole crop with Dominance in one group only", { + # Only one group has a (single valued) Dominance column for the sole crops, + # binding the groups must not detect them as mixtures: + sim_dom <- sim + sim_dom$`SC_Pea_2005-2006_N0`$Dominance <- "0" + sim_dom$`SC_Wheat_2005-2006_N0`$Dominance <- "0" + + df_stats <- summary( + stics_1 = sim, stics_2 = sim_dom, obs = obs, + all_situations = FALSE + ) + df_ref <- summary( + stics_1 = sim, stics_2 = sim, obs = obs, + all_situations = FALSE + ) + expect_equal(df_stats, df_ref) +}) + test_that("statistics summary: no obs", { ## when computing statistics for each situation one by one