diff --git a/R/count_patients_with_flags.R b/R/count_patients_with_flags.R index a8eb6b27a9..39c4ac21bc 100644 --- a/R/count_patients_with_flags.R +++ b/R/count_patients_with_flags.R @@ -155,13 +155,15 @@ a_count_patients_with_flags <- function(df, NULL } .indent_mods <- get_indents_from_stats(.stats, .indent_mods, row_nms = flag_variables) - .indent_mods <- sapply(names(.indent_mods), function(x) { - if (.indent_mods[x] == 0 && !is.null(length(indent_stat_def))) { - idx <- which(names(indent_stat_def) == gsub("\\..*", "", x)) - if (length(idx) > 0) .indent_mods[[x]] <- indent_stat_def[[idx]] - } - .indent_mods[x] - }) + if (length(names(.indent_mods)) > 1) { + .indent_mods <- sapply(names(.indent_mods), function(x) { + if (.indent_mods[x] == 0 && !is.null(length(indent_stat_def))) { + idx <- which(names(indent_stat_def) == gsub("\\..*", "", x)) + if (length(idx) > 0) .indent_mods[[x]] <- indent_stat_def[[idx]] + } + .indent_mods[x] + }) + } if ("count_fraction_fixed_dp" %in% .stats) x_stats[["count_fraction_fixed_dp"]] <- x_stats[["count_fraction"]] x_stats <- x_stats[.stats] diff --git a/tests/testthat/_snaps/count_patients_with_flags.md b/tests/testthat/_snaps/count_patients_with_flags.md index 0fe626c72b..98f24e4342 100644 --- a/tests/testthat/_snaps/count_patients_with_flags.md +++ b/tests/testthat/_snaps/count_patients_with_flags.md @@ -467,3 +467,14 @@ SAE 53 (26.2%) 49 (27.7%) 39 (24.1%) -1.4 (-10.4 - 7.5) SAE with fatal outcome 50 (24.8%) 47 (26.6%) 42 (25.9%) -1.8 (-10.6 - 7.0) +# count_patients_with_flags works with single indent mod value + + Code + res + Output + A B + (N=6) (N=4) + —————————————————————————————————————————————————————————————————————————————————————— + Total number of patients with at least one adverse event 1 (16.7%) 1 (25.0%) + Total number of patients with fatal AEs 0 1 (25.0%) + diff --git a/tests/testthat/test-count_patients_with_flags.R b/tests/testthat/test-count_patients_with_flags.R index a2c8f7776f..2c615fe4c3 100644 --- a/tests/testthat/test-count_patients_with_flags.R +++ b/tests/testthat/test-count_patients_with_flags.R @@ -386,3 +386,45 @@ testthat::test_that("count_patients_with_flags works as expected with risk diffe res <- testthat::expect_silent(result) testthat::expect_snapshot(res) }) + +testthat::test_that("count_patients_with_flags works with single indent mod value", { + test_data <- tibble::tibble( + SUBJID = c("1001", "1001", "1001", "1002", "1002", "1002", "1003", "1003", "1003"), + ARM = factor(c("A", "A", "A", "A", "A", "A", "B", "B", "B"), levels = c("A", "B")), + TRTEMFL = c("Y", "", "", "NA", "", "", "Y", "", ""), + AEOUT = c("", "", "", "", "", "", "FATAL", "", "FATAL") + ) %>% + dplyr::mutate( + flag1 = TRTEMFL == "Y", + flag2 = TRTEMFL == "Y" & AEOUT == "FATAL", + ) + labels <- c( + "A", + "B", + "C", + "D", + "Total number of patients with at least one adverse event", + "Total number of patients with fatal AEs" + ) + formatters::var_labels(test_data) <- labels + + test_adsl_like <- tibble::tibble( + SUBJID = as.character(1001:1010), + ARM = factor(c("A", "A", "B", "B", "A", "A", "A", "B", "B", "A"), levels = c("A", "B")), + stringsAsFactors = FALSE + ) + + lyt <- basic_table() %>% + split_cols_by("ARM") %>% + add_colcounts() %>% + count_patients_with_flags( + "SUBJID", + flag_variables = formatters::var_labels(test_data[, c("flag1", "flag2")]), + denom = "N_col", + .indent_mods = 3L + ) + result <- build_table(lyt, df = test_data, alt_counts_df = test_adsl_like) + + res <- testthat::expect_silent(result) + testthat::expect_snapshot(res) +})