Skip to content

Commit

Permalink
Fixes in tbl_summary() and add_p.tbl_summary() when fct with all …
Browse files Browse the repository at this point in the history
…NA passed (#978)

* #977 fixes

* increment version
  • Loading branch information
ddsjoberg authored Sep 17, 2021
1 parent 83cd7a2 commit 28c2026
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion R/utils-add_p.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 6 additions & 1 deletion R/utils-tbl_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-add_p.tbl_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
})
17 changes: 17 additions & 0 deletions tests/testthat/test-tbl_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
})

0 comments on commit 28c2026

Please sign in to comment.