From c647647a884e311913650520d592b045cc50f405 Mon Sep 17 00:00:00 2001 From: Mark Colley <39366189+M-Colley@users.noreply.github.com> Date: Thu, 3 Dec 2026 12:14:28 +0000 Subject: [PATCH] Preserve data frames with one supported column --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/report.data.frame.R | 10 +++++-- tests/testthat/test-report.data.frame.R | 39 ++++++++++++++++++++----- 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9a2e89df..2a1b0b9b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: report Type: Package Title: Automated Reporting of Results and Statistical Models -Version: 0.6.4.14 +Version: 0.6.4.15 Authors@R: c(person(given = "Dominique", family = "Makowski", diff --git a/NEWS.md b/NEWS.md index 1193aa3c..03741aa6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -18,6 +18,8 @@ Bug fixes * Fixed an issue in `report()` where the reference level for logical predictors was incorrectly displayed as `[?]` instead of `FALSE` for the intercept (@M-Colley, #598). +* `report()` preserves data-frame reports and variable names when only one supported column remains. + * `report()` handles `var.test()` results and reports the F statistic with both degrees of freedom, the p-value, and the variance ratio with its confidence interval (#238). * Fixed `report()` text for `stanreg` and `brmsfit` models with predictors out of alphabetical order, which gave parameters the wrong Rhat and ESS. Earlier reports of such models had wrong values (@jmgirard, #343). diff --git a/R/report.data.frame.R b/R/report.data.frame.R index cb197f20..cbcb1d3e 100644 --- a/R/report.data.frame.R +++ b/R/report.data.frame.R @@ -57,12 +57,18 @@ report.data.frame <- function( # remove list columns if (.has_groups(x) && !inherits(x, "tbl_df")) { x <- .groups_set( - x[, vapply(x, Negate(inherits), what = "list", FUN.VALUE = TRUE)], + x[, + vapply(x, Negate(inherits), what = "list", FUN.VALUE = TRUE), + drop = FALSE + ], groups = .group_vars(x), drop = .groups_drop(x) ) } else { - x <- x[, vapply(x, Negate(inherits), what = "list", FUN.VALUE = TRUE)] + x <- x[, + vapply(x, Negate(inherits), what = "list", FUN.VALUE = TRUE), + drop = FALSE + ] } result_table <- diff --git a/tests/testthat/test-report.data.frame.R b/tests/testthat/test-report.data.frame.R index 50b7c542..b7ad8729 100644 --- a/tests/testthat/test-report.data.frame.R +++ b/tests/testthat/test-report.data.frame.R @@ -22,22 +22,22 @@ test_that("report.numeric", { test_that("report.character", { x <- c("A", "B", "C", "A", "B", "B", "D", "E", "B", "D", "A") r <- report(x) - expect_equal(nrow(as.data.frame(r)), 1, tolerance = 0) + expect_length(as.data.frame(r)$n_Obs, 1) expect_null(as.data.frame(r)$Median) r <- report(x, levels_percentage = FALSE, missing_percentage = TRUE) - expect_equal(nrow(as.data.frame(r)), 1, tolerance = 0) + expect_length(as.data.frame(r)$n_Obs, 1) expect_equal(as.data.frame(r)$percentage_Missing[1], 0, tolerance = 0) }) test_that("report.factor", { r <- report(factor(rep(c("A", "B", "C"), 10))) - expect_equal(nrow(as.data.frame(r)), 3, tolerance = 0) + expect_length(as.data.frame(r)$n_Obs, 3) expect_null(as.data.frame(r)$Median) expect_snapshot(variant = "windows", r) r <- report(factor(rep(c("A", "B", "C", NA), 10)), levels_percentage = FALSE) - expect_equal(nrow(as.data.frame(r)), 4, tolerance = 0) + expect_length(as.data.frame(r)$n_Obs, 4) }) test_that("report.Date", { @@ -62,7 +62,7 @@ test_that("report.data.frame", { skip_if_not_installed("dplyr") r <- report(iris) - expect_equal(nrow(as.data.frame(r)), 7, tolerance = 0) + expect_length(as.data.frame(r)$n_Obs, 7) expect_equal(mean(as.data.frame(r)$Median, na.rm = TRUE), 3.6125) r <- report( @@ -73,11 +73,11 @@ test_that("report.data.frame", { range = FALSE, dispersion = FALSE ) - expect_equal(nrow(as.data.frame(r)), 7, tolerance = 0) + expect_length(as.data.frame(r)$n_Obs, 7) expect_equal(mean(as.data.frame(r)$n_Obs), 107, tolerance = 0.01) r <- report(dplyr::group_by(iris, Species)) - expect_equal(nrow(as.data.frame(r)), 12, tolerance = 0) + expect_length(as.data.frame(r)$n_Obs, 12) expect_equal(mean(as.data.frame(r)$n_Obs), 50, tolerance = 0) expect_snapshot(variant = "windows", r) @@ -100,3 +100,28 @@ test_that("report.data.frame - with list columns", { set.seed(123) expect_snapshot(variant = "windows", report(dplyr::starwars)) }) + +test_that("report.data.frame preserves a single supported column", { + with_list <- data.frame(score = 1:5) + with_list$details <- as.list(letters[1:5]) + datasets <- list( + data.frame(score = 1:5), + data.frame(group = factor(c("A", "B", "A", "B", "A"))), + with_list + ) + + for (data in datasets) { + result <- report(data) + table <- as.report_table(result) + text <- paste(as.character(result), collapse = "\n") + + expect_match( + text, + "The data contains 5 observations of the following 1 variables:", + fixed = TRUE + ) + expect_match(text, paste0(" - ", names(data)[1], ":"), fixed = TRUE) + expect_identical(unique(table$Variable), names(data)[1]) + expect_identical(unique(summary(table)$Variable), names(data)[1]) + } +})