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

Add hierarchy group levels to table_body in tbl_hierarchical() #2072

Merged
merged 9 commits into from
Nov 25, 2024
23 changes: 22 additions & 1 deletion R/brdg_hierarchical.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,28 @@ brdg_hierarchical <- function(cards,
table_body <- dplyr::bind_rows(over_row, table_body)
}

table_body <- table_body |> select(-cards::all_ard_groups())
# add hierarchy levels to table_body for sorting & filtering -----------------
table_body <- table_body |>
mutate(across(cards::all_ard_groups(), .fns = ~str_replace(., "^ $", NA)))
if (n_by > 0 && length(variables) > 1) {
which_gps <- which(names(table_body) %in% (table_body |> select(cards::all_ard_groups()) |> names()))
if (n_by > 0) {
names(table_body)[which_gps] <- sapply(
names(table_body)[which_gps],
function(x) {
n <- as.numeric(gsub(".*([0-9]+).*", "\\1", x)) - n_by
gsub("[0-9]+", n, x)
}
)
}
for (i in which_gps[c(TRUE, FALSE)]) {
lbl_row <- which(is.na(table_body[i]) & !is.na(table_body[i + 1]))
table_body[lbl_row, i] <- table_body$variable[lbl_row]
}
}
if (overall_row && "group1" %in% names(table_body)) {
table_body$group1[table_body$variable == "..ard_hierarchical_overall.."] <- "..ard_hierarchical_overall.."
}

# construct default table_styling --------------------------------------------
x <- .create_gtsummary_object(table_body)
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/_snaps/tbl_hierarchical.md
Original file line number Diff line number Diff line change
Expand Up @@ -975,3 +975,23 @@
188 <NA>
189 1

# tbl_hierarchical_count table_body enables sorting

Code
res$table_body
Output
# A tibble: 21 x 11
row_type var_label variable label group1 group1_level group2 group2_level stat_1 stat_2 stat_3
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 level <NA> ..ard_hierarchical_overall.. Number of patients with event ..ard_hierarchical_overall.. <NA> <NA> <NA> 26 (30%) 42 (50%) 40 (48%)
2 level <NA> SEX F SEX F <NA> <NA> 13 (25%) 18 (45%) 23 (46%)
3 level <NA> AESOC CARDIAC DISORDERS SEX F AESOC CARDIAC DISORDERS 0 (0%) 1 (2.5%) 0 (0%)
4 level <NA> AETERM ATRIOVENTRICULAR BLOCK SECOND DEGREE SEX F AESOC CARDIAC DISORDERS 0 (0%) 1 (2.5%) 0 (0%)
5 level <NA> AESOC GASTROINTESTINAL DISORDERS SEX F AESOC GASTROINTESTINAL DISORDERS 3 (5.7%) 0 (0%) 3 (6.0%)
6 level <NA> AETERM DIARRHOEA SEX F AESOC GASTROINTESTINAL DISORDERS 3 (5.7%) 0 (0%) 3 (6.0%)
7 level <NA> AESOC GENERAL DISORDERS AND ADMINISTRATION SITE CONDITIONS SEX F AESOC GENERAL DISORDERS AND ADMINISTRATION SITE CONDITIONS 5 (9.4%) 11 (28%) 13 (26%)
8 level <NA> AETERM APPLICATION SITE ERYTHEMA SEX F AESOC GENERAL DISORDERS AND ADMINISTRATION SITE CONDITIONS 2 (3.8%) 5 (13%) 5 (10%)
9 level <NA> AETERM APPLICATION SITE PRURITUS SEX F AESOC GENERAL DISORDERS AND ADMINISTRATION SITE CONDITIONS 4 (7.5%) 10 (25%) 12 (24%)
10 level <NA> AESOC SKIN AND SUBCUTANEOUS TISSUE DISORDERS SEX F AESOC SKIN AND SUBCUTANEOUS TISSUE DISORDERS 6 (11%) 7 (18%) 9 (18%)
# i 11 more rows

24 changes: 24 additions & 0 deletions tests/testthat/test-tbl_hierarchical.R
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,27 @@ test_that("tbl_hierarchical_count with 10+ hierarchy variables", {
)
expect_snapshot(res |> as.data.frame())
})

# tbl_hierarchical_count table_body enables sorting ----------------------------------------
test_that("tbl_hierarchical_count table_body enables sorting", {
withr::local_options(list(width = 250))

ADAE_subset <- cards::ADAE |>
dplyr::filter(
AESOC %in% unique(cards::ADAE$AESOC)[1:5],
AETERM %in% unique(cards::ADAE$AETERM)[1:5]
)

res <- expect_silent(
tbl_hierarchical(
data = ADAE_subset,
variables = c(SEX, AESOC, AETERM),
by = TRTA,
denominator = cards::ADSL |> mutate(TRTA = ARM),
id = USUBJID,
overall_row = TRUE
)
)

expect_snapshot(res$table_body)
})
Loading