From 28c202695348b939f36f9440c9e3430ce1925477 Mon Sep 17 00:00:00 2001 From: Daniel Sjoberg Date: Fri, 17 Sep 2021 12:03:42 -0400 Subject: [PATCH] Fixes in `tbl_summary()` and `add_p.tbl_summary()` when fct with all NA passed (#978) * #977 fixes * increment version --- DESCRIPTION | 2 +- NEWS.md | 4 ++++ R/utils-add_p.R | 2 +- R/utils-tbl_summary.R | 7 ++++++- tests/testthat/test-add_p.tbl_summary.R | 6 +++--- tests/testthat/test-tbl_summary.R | 17 +++++++++++++++++ 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3cf4b00fa2..1b27240d59 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: gtsummary Title: Presentation-Ready Data Summary and Analytic Result Tables -Version: 1.4.2.9008 +Version: 1.4.2.9009 Authors@R: c(person(given = "Daniel D.", family = "Sjoberg", diff --git a/NEWS.md b/NEWS.md index b7ecbd6fd4..0d8b71aaee 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # gtsummary (development version) +* Fix in `tbl_summary()` when a factor variable is passed that is all NA with no specified levels. (#977) + +* Fix in `add_p.tbl_summary()` when a factor variable with all NA values is passed. (#977) + * New function `modify_cols_merge()` that can merge two or more columns in a gtsummary table. (#939) * Added new function `add_prop_ci()` that adds a new column with the confidence interval for proportions reported in `tbl_summary()`. Cannot be used if `tbl_summary()` included a `by=` argument. (#868) diff --git a/R/utils-add_p.R b/R/utils-add_p.R index ec8cb45ca0..d2f978d8bd 100644 --- a/R/utils-add_p.R +++ b/R/utils-add_p.R @@ -241,7 +241,7 @@ ) # if expected counts >= 5 for all cells, chisq, otherwise Fishers exact - if (min_exp >= 5) { + if (isTRUE(min_exp >= 5 || is.nan(min_exp))) { test_func <- get_theme_element("add_p.tbl_summary-attr:test.categorical") %||% getOption("gtsummary.add_p.test.categorical", default = "chisq.test.no.correct") diff --git a/R/utils-tbl_summary.R b/R/utils-tbl_summary.R index 5ce9e24e2b..bab8872466 100644 --- a/R/utils-tbl_summary.R +++ b/R/utils-tbl_summary.R @@ -204,9 +204,14 @@ assign_summary_type <- function(data, variable, summary_type, value, if (inherits(data[[variable]], "character")) { return("dichotomous") } - if (inherits(data[[variable]], "factor")) { + if (inherits(data[[variable]], "factor") && + !rlang::is_empty(attr(data[[variable]], "levels"))) { return("categorical") } + if (inherits(data[[variable]], "factor") && + rlang::is_empty(attr(data[[variable]], "levels"))) { + return("dichotomous") + } } # numeric variables that are 0 and 1 only, will be dichotomous diff --git a/tests/testthat/test-add_p.tbl_summary.R b/tests/testthat/test-add_p.tbl_summary.R index d7f8486561..c9f378ab07 100644 --- a/tests/testthat/test-add_p.tbl_summary.R +++ b/tests/testthat/test-add_p.tbl_summary.R @@ -398,13 +398,13 @@ test_that("no error with missing data", { expect_message( t1 <- mtcars %>% - mutate(mpg = NA, hp = NA) %>% - select(mpg, hp, am) %>% + mutate(mpg = NA, hp = NA, has_banana = factor(NA, levels = c("Yes", "No"))) %>% + select(has_banana, mpg, hp, am) %>% tbl_summary(by = "am", type = hp ~ "continuous") %>% add_p() ) expect_equal( t1 %>% as_tibble(col_labels = FALSE) %>% dplyr::pull(p.value), - rep_len(NA_character_, 4) + rep_len(NA_character_, 8) ) }) diff --git a/tests/testthat/test-tbl_summary.R b/tests/testthat/test-tbl_summary.R index c307008d44..82b586c2c1 100644 --- a/tests/testthat/test-tbl_summary.R +++ b/tests/testthat/test-tbl_summary.R @@ -643,3 +643,20 @@ test_that("no error when by variable omitted from include", { NA ) }) + +test_that("no error with factor variable with all NA and no specifed levels", { + expect_error( + tbl <- + trial %>% + dplyr::mutate( + has_banana = factor(NA) # We don't know which patients have a banana + ) %>% + tbl_summary(by = trt, include = has_banana) %>% + as_tibble(col_labels = FALSE), + NA + ) + expect_equal( + tbl$stat_1, + c("0 (NA%)", "98") + ) +})