diff --git a/DESCRIPTION b/DESCRIPTION index 56b97e04bb..e361c6d128 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: tern Title: Create Common TLGs Used in Clinical Trials -Version: 0.8.5.9004 -Date: 2023-08-02 +Version: 0.8.5.9005 +Date: 2023-08-04 Authors@R: c( person("Joe", "Zhu", , "joe.zhu@roche.com", role = c("aut", "cre")), person("Daniel", "Sabanés Bové", , "daniel.sabanes_bove@roche.com", role = "aut"), diff --git a/NEWS.md b/NEWS.md index 04f9c07aff..e1085f55a4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,8 @@ -# tern 0.8.5.9004 +# tern 0.8.5.9005 ### Miscellaneous * Fix swapped descriptions for the `.N_row` and `.N_col` parameters. +* Fix bug in `analyze_vars_in_cols` when categorical data was used. # tern 0.8.5 diff --git a/R/analyze_vars_in_cols.R b/R/analyze_vars_in_cols.R index 88eef5523f..91eea31965 100644 --- a/R/analyze_vars_in_cols.R +++ b/R/analyze_vars_in_cols.R @@ -256,6 +256,14 @@ analyze_vars_in_cols <- function(lyt, # Main statistics res <- s_summary(u, ...)[[stat]] + if (is.list(res)) { + if (length(res) > 1) { + stop("The analyzed column produced more than one category of results.") + } else { + res <- unlist(res) + } + } + # Label from context label_from_context <- .spl_context$value[nrow(.spl_context)] diff --git a/tests/testthat/_snaps/analyze_vars_in_cols.md b/tests/testthat/_snaps/analyze_vars_in_cols.md index d3febcac6c..f6d6bc55ae 100644 --- a/tests/testthat/_snaps/analyze_vars_in_cols.md +++ b/tests/testthat/_snaps/analyze_vars_in_cols.md @@ -126,3 +126,41 @@ B 9 28.0 0.0 0.0 0.0 0.0 C 9 37.6 0.0 0.0 0.0 0.0 +# analyze_vars_in_cols works well with categorical data + + Code + build_table(lyt = lyt, df = adpp %>% mutate(counter = factor("n"))) + Output + STRATA1 A: Drug X B: Placebo C: Combination + SEX + ————————————————————————————————————————————————— + A + F 0 0 81 (100%) + M 0 0 81 (100%) + B + F 0 0 90 (100%) + M 0 0 81 (100%) + C + F 0 0 117 (100%) + M 0 0 72 (100%) + +--- + + Code + basic_table(show_colcounts = TRUE) %>% split_rows_by(var = "STRATA1", + label_pos = "topleft") %>% split_cols_by("ARM") %>% analyze(vars = "SEX", + afun = count_fraction) %>% append_topleft(" SEX") %>% build_table(adpp) + Output + STRATA1 A: Drug X B: Placebo C: Combination + SEX (N=0) (N=0) (N=522) + ————————————————————————————————————————————————— + A + F 0 0 81 (16%) + M 0 0 81 (16%) + B + F 0 0 90 (17%) + M 0 0 81 (16%) + C + F 0 0 117 (22%) + M 0 0 72 (14%) + diff --git a/tests/testthat/test-analyze_vars_in_cols.R b/tests/testthat/test-analyze_vars_in_cols.R index 9d1c975d29..7985b6fb11 100644 --- a/tests/testthat/test-analyze_vars_in_cols.R +++ b/tests/testthat/test-analyze_vars_in_cols.R @@ -208,3 +208,65 @@ testthat::test_that("summarize works with nested analyze", { testthat::expect_snapshot(tbl_sorted) }) + +testthat::test_that("analyze_vars_in_cols works well with categorical data", { + # Regression test after #1013 + adpp <- tern_ex_adpp %>% h_pkparam_sort() + + lyt <- basic_table() %>% + split_rows_by(var = "STRATA1", label_pos = "topleft") %>% + split_rows_by( + var = "SEX", + label_pos = "topleft", + child_label = "hidden" + ) %>% + # split_cols_by("STRATA1") %>% + analyze_vars_in_cols( + vars = "ARM", + .stats = c("n", "count_fraction"), + .labels = c("count_fraction" = "argh") + ) + testthat::expect_error( + result <- build_table(lyt = lyt, df = adpp), + "The analyzed column produced more than one category of results." + ) + + lyt <- basic_table() %>% + split_rows_by(var = "STRATA1", label_pos = "topleft") %>% + split_rows_by( + var = "SEX", + label_pos = "topleft", + child_label = "hidden" + ) %>% + split_cols_by("ARM") %>% + analyze_vars_in_cols( + vars = "counter", + .stats = c("count_fraction"), + .labels = c("count_fraction" = " ") + ) + testthat::expect_snapshot(build_table( + lyt = lyt, + df = adpp %>% mutate(counter = factor("n")) + )) + + # Alternative to discuss (xxx) + count_fraction <- function(x, .spl_context, .N_col) { # nolint + ret_list <- as.list(table(x)) + if (length(x) == 0) { + aform <- "xx" + } else { + ret_list <- lapply(ret_list, function(i) { + c(i, i / .N_col) + }) + aform <- "xx. (xx.%)" + } + in_rows(.list = ret_list, .formats = aform) + } + + testthat::expect_snapshot(basic_table(show_colcounts = TRUE) %>% + split_rows_by(var = "STRATA1", label_pos = "topleft") %>% + split_cols_by("ARM") %>% + analyze(vars = "SEX", afun = count_fraction) %>% + append_topleft(" SEX") %>% + build_table(adpp)) +})