diff --git a/R/plot.estimate_density.R b/R/plot.estimate_density.R index 51dac57dc..d8b1f34e8 100644 --- a/R/plot.estimate_density.R +++ b/R/plot.estimate_density.R @@ -11,6 +11,26 @@ data_plot.estimate_density <- function( if (!"Parameter" %in% names(dataplot)) { dataplot$Parameter <- "Distribution" } + # Handle case where Parameter column exists but is empty or malformed + if ("Parameter" %in% names(dataplot)) { + # Check for various problematic conditions with descriptive variables + is_parameter_empty <- length(dataplot$Parameter) == 0 + is_parameter_all_na <- all(is.na(dataplot$Parameter)) + has_parameter_all_empty_strings <- all(dataplot$Parameter == "") + has_parameter_no_unique_values <- length(unique(dataplot$Parameter[ + !is.na(dataplot$Parameter) + ])) == + 0 + + is_parameter_problematic <- is_parameter_empty || + is_parameter_all_na || + has_parameter_all_empty_strings || + has_parameter_no_unique_values + + if (is_parameter_problematic) { + dataplot$Parameter <- rep("Distribution", nrow(dataplot)) + } + } # add component and effects columns if (!is.null(data)) { @@ -23,11 +43,27 @@ data_plot.estimate_density <- function( dataplot <- .fix_facet_names(dataplot) - dataplot$Parameter <- factor(dataplot$Parameter) - dataplot$Parameter <- factor( - dataplot$Parameter, - levels = rev(levels(dataplot$Parameter)) - ) + # Safely convert Parameter to factor, ensuring it has valid data + has_parameter_column <- "Parameter" %in% names(dataplot) + has_parameter_data <- length(dataplot$Parameter) > 0 + has_parameter_length_match <- length(dataplot$Parameter) == nrow(dataplot) + has_parameter_not_all_na <- !all(is.na(dataplot$Parameter)) + + is_parameter_valid_for_factor <- has_parameter_column && + has_parameter_data && + has_parameter_length_match && + has_parameter_not_all_na + + if (is_parameter_valid_for_factor) { + dataplot$Parameter <- factor(dataplot$Parameter) + dataplot$Parameter <- factor( + dataplot$Parameter, + levels = rev(levels(dataplot$Parameter)) + ) + } else { + # If Parameter column is still problematic, set a default with correct length + dataplot$Parameter <- factor(rep("Distribution", nrow(dataplot))) + } # summary split_columns <- intersect( @@ -250,6 +286,11 @@ plot.see_estimate_density <- function( p <- p + facet_wrap(~Component, scales = "free", ncol = n_columns) } } + # Handle Group column for grouped data (e.g., from group_by in estimate_density) + # Only add facets if not already faceting by other variables and if we have groups + if ("Group" %in% names(x) && is.null(n_columns)) { + p <- p + facet_wrap(~Group, scales = "free") + } p } diff --git a/tests/testthat/_snaps/plot.estimate_density/plot-estimate-density-with-group-by-and-vector-input.svg b/tests/testthat/_snaps/plot.estimate_density/plot-estimate-density-with-group-by-and-vector-input.svg new file mode 100644 index 000000000..b674f38b6 --- /dev/null +++ b/tests/testthat/_snaps/plot.estimate_density/plot-estimate-density-with-group-by-and-vector-input.svg @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.5 +1.0 +1.5 +2.0 +2.5 +x +y +plot.estimate_density with group_by and vector input + + diff --git a/tests/testthat/test-plot.estimate_density.R b/tests/testthat/test-plot.estimate_density.R index d197600ea..c6cd3cc86 100644 --- a/tests/testthat/test-plot.estimate_density.R +++ b/tests/testthat/test-plot.estimate_density.R @@ -9,6 +9,26 @@ test_that("`plot.see_estimate_density()` works", { }) +test_that("`plot.see_estimate_density()` works with group_by and vector input", { + skip_if_not_installed("bayestestR") + skip_if_not_installed("vdiffr") + # Test case that was failing: vector input with group_by + df <- bayestestR::estimate_density( + iris[c("Species", "Petal.Width")], + group_by = "Species" + ) + + # This should not error + expect_no_error(p <- plot(df)) + expect_s3_class(p, "gg") + # Visual snapshot test + vdiffr::expect_doppelganger( + title = "plot.estimate_density with group_by and vector input", + fig = plot(df) + ) +}) + + test_that("`plot.see_estimate_density()`, adding prior layers works", { skip_if_not_installed("curl") skip_if_offline()