Skip to content

Commit

Permalink
Fix indent modifier bug in count_patients_with_flags()
Browse files Browse the repository at this point in the history
  • Loading branch information
edelarua committed Nov 6, 2024
1 parent 435fd51 commit ad85b10
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
16 changes: 9 additions & 7 deletions R/count_patients_with_flags.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/_snaps/count_patients_with_flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -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%)

42 changes: 42 additions & 0 deletions tests/testthat/test-count_patients_with_flags.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit ad85b10

Please sign in to comment.