Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix na_level bug in summarize_vars #923

Merged
merged 4 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

### Bug Fixes
* Fixed missing label for `TRTEDTM` in `tern` datasets.
* Fixed improper implementation of `na_level` argument in `summarize_vars` preventing it from having an effect.

### Enhancements
* Added `section_div` and `na_level` (`na_str`) to `summarize_vars`.
Expand Down
5 changes: 2 additions & 3 deletions R/summarize_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ s_summary.factor <- function(x,
#' @param verbose (`logical`)\cr Defaults to `TRUE`, which prints out warnings and messages. It is mainly used
#' to print out information about factor casting.
#'
#' @note Automatic conversion of character to factor does not guarantee that the table
#' @note
#' * Automatic conversion of character to factor does not guarantee that the table
#' can be generated correctly. In particular for sparse tables this very likely can fail.
#' It is therefore better to always pre-process the dataset such that factors are manually
#' created from character variables before passing the dataset to [rtables::build_table()].
Expand Down Expand Up @@ -722,7 +723,6 @@ create_afun_summary <- function(.stats, .formats, .labels, .indent_mods) {
summarize_vars <- function(lyt,
vars,
var_labels = vars,
na_level = NA_character_,
nested = TRUE,
...,
show_labels = "default",
Expand All @@ -739,7 +739,6 @@ summarize_vars <- function(lyt,
vars = vars,
var_labels = var_labels,
afun = afun,
na_str = na_level,
nested = nested,
extra_args = list(...),
inclNAs = TRUE,
Expand Down
5 changes: 3 additions & 2 deletions man/summarize_variables.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions tests/testthat/_snaps/summarize_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -1128,3 +1128,26 @@
Median NA 3.5 5.5
Min - Max NA 3.0 - 4.0 5.0 - 6.0

# `summarize_vars` na_level argument works with non-default input

Code
res
Output
all obs
———————————
n 4
a 2 (50%)
b 2 (50%)

---

Code
res
Output
all obs
—————————————
n 6
a 2 (33.3%)
b 2 (33.3%)
c 2 (33.3%)

20 changes: 20 additions & 0 deletions tests/testthat/test-summarize_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,23 @@ testthat::test_that("`summarize_vars` works with empty named numeric variables",
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})

testthat::test_that("`summarize_vars` na_level argument works with non-default input", {
dta <- tibble::tibble(
foo = factor(c("a", "a", "b", "b", "c", "c"), levels = c("a", "b", "c"))
)

result <- basic_table() %>%
summarize_vars(vars = "foo", na_level = "c") %>%
build_table(dta)

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)

result <- basic_table() %>%
summarize_vars(vars = "foo", na_level = "c", na.rm = FALSE) %>%
build_table(dta)

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})