Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions R/plot.estimate_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ data_plot.estimate_density <- function(

# Handle case where Parameter column exists but is empty or malformed
if ("Parameter" %in% names(dataplot)) {
# Check for various problematic conditions
if (length(dataplot$Parameter) == 0 ||
all(is.na(dataplot$Parameter)) ||
all(dataplot$Parameter == "") ||
length(unique(dataplot$Parameter[!is.na(dataplot$Parameter)])) == 0) {
# Check for various problematic conditions with descriptive variables
parameter_is_empty <- length(dataplot$Parameter) == 0
parameter_all_na <- all(is.na(dataplot$Parameter))
parameter_all_empty_strings <- all(dataplot$Parameter == "")
parameter_no_unique_values <- length(unique(dataplot$Parameter[!is.na(dataplot$Parameter)])) == 0

parameter_is_problematic <- parameter_is_empty || parameter_all_na ||
parameter_all_empty_strings || parameter_no_unique_values

if (parameter_is_problematic) {
dataplot$Parameter <- rep("Distribution", nrow(dataplot))
}
}
Expand All @@ -35,10 +40,15 @@ data_plot.estimate_density <- function(
dataplot <- .fix_facet_names(dataplot)

# Safely convert Parameter to factor, ensuring it has valid data
if ("Parameter" %in% names(dataplot) &&
length(dataplot$Parameter) > 0 &&
length(dataplot$Parameter) == nrow(dataplot) &&
!all(is.na(dataplot$Parameter))) {
parameter_column_exists <- "Parameter" %in% names(dataplot)
parameter_has_data <- length(dataplot$Parameter) > 0
parameter_length_matches <- length(dataplot$Parameter) == nrow(dataplot)
parameter_not_all_na <- !all(is.na(dataplot$Parameter))

parameter_is_valid_for_factor <- parameter_column_exists && parameter_has_data &&
parameter_length_matches && parameter_not_all_na

if (parameter_is_valid_for_factor) {
dataplot$Parameter <- factor(dataplot$Parameter)
dataplot$Parameter <- factor(
dataplot$Parameter,
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-plot.estimate_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ 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)
)
})


Expand Down