-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtbl_hierarchical_rate_by_grade.R
More file actions
607 lines (564 loc) · 24.1 KB
/
Copy pathtbl_hierarchical_rate_by_grade.R
File metadata and controls
607 lines (564 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#' AE Rates by Highest Toxicity Grade
#'
#' @description
#'
#' A wrapper function for [gtsummary::tbl_hierarchical()] to calculate rates of highest toxicity grades with the options
#' to add rows for grade groups and additional summary sections at each variable level.
#'
#' Only the highest grade level recorded for each subject will be analyzed. Prior to running the function, ensure that
#' the toxicity grade variable (`grade`) is a factor variable, with factor levels ordered lowest to highest.
#'
#' Grades will appear in rows in the order of the factor levels given, with each grade group appearing prior to the
#' first level in its group.
#'
#' @inheritParams gtsummary::tbl_hierarchical
#' @inheritParams gtsummary::sort_hierarchical
#' @inheritParams gtsummary::add_overall.tbl_hierarchical
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' A character vector or tidy-selector of 3 columns in `data` specifying a system organ class variable,
#' an adverse event terms variable, and a toxicity grade level variable, respectively.
#' @param include_overall ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' Variables from `variables` for which an overall section at that hierarchy level should be computed.
#' An overall section at the SOC variable level will have label `"- Any adverse events -"`. An overall section at
#' the AE term variable level will have label `"- Overall -"`. If the grade level variable is included it has no
#' effect. The default is `everything()`.
#' @param filter (`expression`)\cr
#' An expression that is used to filter rows of the table. Filter will be applied to the second variable (adverse
#' event terms) specified via `variables`. See the Details section below for more information.
#' @param grade_groups (`named list`)\cr
#' A named list of grade groups for which rates should be calculated. Grade groups must be mutually exclusive, i.e.
#' each grade cannot be assigned to more than one grade group. Each grade group must be specified in the list as a
#' character vector of the grades included in the grade group, named with the corresponding name of the grade group,
#' e.g. `"Grade 1-2" = c("1", "2")`.
#' @param grades_exclude (`character`)\cr
#' A vector of grades to omit individual rows for when printing the table. These grades will still be used when
#' computing overall totals and grade group totals. For example, to avoid duplication, if a grade group is defined as
#' `"Grade 5" = "5"`, the individual rows corresponding to grade 5 can be excluded by setting `grades_exclude = "5"`.
#' @param keep_zero_rows (`logical`)\cr
#' Whether rows containing zero rates across all columns should be kept. If `FALSE`, this filter will be applied
#' prior to any filters specified via the `filter` argument which may still remove these rows. Defaults to `FALSE`.
#' @param x (`tbl_hierarchical_rate_by_grade`)\cr
#' A gtsummary table of class `'tbl_hierarchical_rate_by_grade'`.
#'
#' @details
#' This function returns a structurally pristine table where the `label` column retains unique grade text
#' (e.g., "1", "2", "Grade 1-2"). This preserves row uniqueness required by [gtsummary::tbl_merge()] and
#' [tbl_with_pools()]. To apply visual formatting (grade column, label blanking, header styling), pipe the
#' result through [add_grade_column()] **after** any merging operations.
#'
#' When using the `filter` argument, the filter will be applied to the second variable from `variables`, i.e. the
#' adverse event terms variable. If an AE does not meet the filtering criteria, the AE overall row as well as all grade
#' and grade group rows within an AE section will be excluded from the table. Filtering out AEs does not exclude the
#' records corresponding to these filtered out rows from being included in rate calculations for overall sections. If
#' all AEs for a given SOC have been filtered out, the SOC will be excluded from the table. If all AEs are filtered out
#' and the SOC variable is included in `include_overall` the `- Any adverse events -` section will still be kept.
#'
#' See [gtsummary::filter_hierarchical()] for more details and examples.
#'
#' @returns a gtsummary table of class `"tbl_hierarchical_rate_by_grade"`.
#' @name tbl_hierarchical_rate_by_grade
#'
#' @examplesIf identical(Sys.getenv("NOT_CRAN"), "true") || identical(Sys.getenv("IN_PKGDOWN"), "true")
#' theme_gtsummary_roche()
#' ADSL <- cards::ADSL
#' ADAE_subset <- cards::ADAE |>
#' dplyr::filter(
#' AESOC %in% unique(cards::ADAE$AESOC)[1:5],
#' AETERM %in% unique(cards::ADAE$AETERM)[1:10]
#' )
#'
#' grade_groups <- list(
#' "Grade 1-2" = c("1", "2"),
#' "Grade 3-4" = c("3", "4"),
#' "Grade 5" = "5"
#' )
#'
#' # Example 1 ----------------------------------
#' tbl_hierarchical_rate_by_grade(
#' ADAE_subset,
#' variables = c(AEBODSYS, AEDECOD, AETOXGR),
#' denominator = ADSL,
#' by = TRTA,
#' label = list(
#' AEBODSYS = "MedDRA System Organ Class",
#' AEDECOD = "MedDRA Preferred Term",
#' AETOXGR = "Grade"
#' ),
#' grade_groups = grade_groups,
#' grades_exclude = "5"
#' ) |>
#' add_grade_column()
#'
#' # Example 2 ----------------------------------
#' # Filter: Keep AEs with an overall prevalence of greater than 10%
#' tbl_hierarchical_rate_by_grade(
#' ADAE_subset,
#' variables = c(AEBODSYS, AEDECOD, AETOXGR),
#' denominator = ADSL,
#' by = TRTA,
#' grade_groups = list("Grades 1-2" = c("1", "2"), "Grades 3-5" = c("3", "4", "5")),
#' filter = sum(n) / sum(N) > 0.10
#' ) |>
#' add_overall(last = TRUE) |>
#' add_grade_column()
NULL
#' @export
#' @rdname tbl_hierarchical_rate_by_grade
tbl_hierarchical_rate_by_grade <- function(data,
variables,
denominator,
by = NULL,
id = "USUBJID",
include_overall = everything(),
statistic = everything() ~ "{n} ({p}%)",
label = NULL,
digits = NULL,
sort = "alphanumeric",
filter = NULL,
grade_groups = list(),
grades_exclude = NULL,
keep_zero_rows = FALSE) {
# check inputs ---------------------------------------------------------------
set_cli_abort_call()
check_not_missing(data)
check_not_missing(variables)
check_not_missing(denominator)
check_class(grades_exclude, "character", allow_empty = TRUE)
cards::process_selectors(data,
variables = {{ variables }},
by = {{ by }},
id = {{ id }}
)
cards::process_selectors(data[variables], include_overall = {{ include_overall }})
check_length(variables, 3)
filter <- enquo(filter)
# save function inputs
tbl_hierarchical_rate_by_grade_inputs <- as.list(environment())
soc <- variables[1]
ae <- variables[2]
grade <- variables[3]
if (!is_empty(grade_groups) && !(is_named(grade_groups) && all(sapply(grade_groups, is_character)))) {
cli::cli_abort(
paste(
"Grade groups must be specified via a named list where each list element is a character vector of the",
"grades to include in the grade group and each name is the corresponding name of the grade group.",
'For example, {.code "Grade 3-4" = c("3", "4")}.'
)
)
}
if (!is_empty(grade_groups)) {
if (length(unique(unlist(grade_groups))) < length(unlist(grade_groups))) {
cli::cli_abort(
paste(
"Grade groups specified via {.arg grade_groups} cannot overlap.",
"Please ensure that each grade is included in at most one grade group."
)
)
}
}
if (!is.factor(data[[grade]])) {
label_grade <- attr(data[[grade]], "label")
if (!is_empty(grade_groups) && length(unlist(grade_groups)) >= length(unique(data[[grade]]))) {
# if all grades are in a grade group, use the defined grade group order to order factor levels
data[[grade]] <- factor(data[[grade]], levels = unlist(grade_groups))
} else {
# otherwise, use the default order and append any expected (grouped) grades that do not appear
data[[grade]] <- factor(data[[grade]])
if (!is_empty(setdiff(unlist(grade_groups), levels(data[[grade]])))) {
levels(data[[grade]]) <- union(levels(data[[grade]]), unlist(grade_groups))
}
}
attr(data[[grade]], "label") <- label_grade
vec <- cli::cli_vec(
levels(data[[grade]]),
style = list("vec-sep" = " < ", "vec-sep2" = " < ", "vec-last" = " < ", "vec-trunc" = 3)
)
cli::cli_inform("{.var {grade}}: {.val {vec}}")
}
if (!is_empty(grades_exclude) & !is_empty(setdiff(grades_exclude, levels(data[[grade]])))) {
not_a_grade <- setdiff(grades_exclude, levels(data[[grade]]))
cli::cli_abort(
paste(
"Grade(s) {.val {not_a_grade}} supplied to {.arg grades_exclude} {?is/are} invalid.",
"All grades specified via {.arg grades_exclude} must be levels of {.val {grade}}."
)
)
}
# fill in unspecified statistics/labels/digits
cards::process_formula_selectors(data[variables], statistic = statistic, digits = digits, label = label)
cards::fill_formula_selectors(
data[variables],
statistic = eval(formals(gtsummary::tbl_hierarchical)[["statistic"]])
)
cards::fill_formula_selectors(
data[variables],
label = lapply(variables, \(x) attr(data[variables][[x]], "label") %||% x) |> stats::setNames(variables)
)
digits <-
gtsummary::assign_summary_digits(
data = data,
statistic = statistic,
type = rep_named(names(statistic), list("categorical")),
digits = digits
)
digits <- lapply(digits, FUN = \(x) x[intersect(names(x), c("n", "N", "p"))])
# get levels of grade variable
lvls <- levels(data[[grade]])
# add overall sections -------------------------------------------------------
# overall section at SOC level (- Any adverse events -)
if (soc %in% include_overall) {
data <-
dplyr::bind_rows(
data |> dplyr::mutate(across(all_of(c(soc, ae)), ~ factor("- Any adverse events -"))),
data
)
}
# overall section for AE term level (- Overall -)
if (ae %in% include_overall) {
data <- data |>
dplyr::filter(.data[[soc]] != "- Any adverse events -") |>
dplyr::mutate(!!ae := "- Overall -") |>
dplyr::bind_rows(data)
}
# ungrouped grades hierarchical summary --------------------------------------
if (!is_empty(setdiff(lvls, grades_exclude))) {
ard_ungrouped <- .ard_rate_by_grade_ordered(data, variables, by, id, denominator)
# remove grades not part of any grade groups - these will be duplicated in ard_grouped
if (!is_empty(grade_groups) && !is_empty(setdiff(lvls, unlist(grade_groups)))) {
ard_ungrouped <- ard_ungrouped |>
dplyr::filter(!.data[["variable_level"]] %in% setdiff(lvls, unlist(grade_groups)))
}
}
# grouped grades hierarchical summary ----------------------------------------
if (!is_empty(grade_groups)) {
# generate grade groups ARD
ard_grouped <- .ard_rate_by_grade_ordered(data, variables, by, id, denominator, grade_groups) |>
suppressMessages()
ard_args <- attr(ard_grouped, "args") # needed for sorting/filtering
if (is_empty(setdiff(lvls, grades_exclude))) {
# if all individual grades excluded, only grade groups included in final ARD
ard_final <- ard_grouped
} else {
# if both grouped and ungrouped grade rows exist, combine the two ARDs
ard_final <- cards::bind_ard(list(ard_ungrouped, ard_grouped), .quiet = TRUE)
}
} else {
# if no grade groups, only individual grades included in final ARD
ard_final <- ard_ungrouped
ard_args <- attr(ard_ungrouped, "args") # needed for sorting/filtering
}
# format the final ARD -------------------------------------------------------
# if `keep_zero_rows` is FALSE, filter all-zero rows out
if (!keep_zero_rows) {
n <- NULL # fix warning for undefined variable
ard_final <- ard_final |> cards::filter_ard_hierarchical(filter = sum(n) > 0)
}
if (!quo_is_null(filter)) {
# if filtering is used, don't categorize the SOC overall sections as AEs
ae_gp_lvl <- paste0("group", length(by) + 2, "_level")
# isolate the SOC overall section rows
ard_aes_overall <- ard_final |>
dplyr::filter(.data[[ae_gp_lvl]] %in% "- Overall -" | .data[["variable_level"]] %in% "- Overall -")
ard_final <- ard_final |>
dplyr::filter(!.data[[ae_gp_lvl]] %in% "- Overall -" | .data[["variable_level"]] %in% "- Overall -")
# apply filtering (without SOC overall sections)
ard_final <- ard_final |>
cards::filter_ard_hierarchical(filter = !!filter, var = all_of(ae))
# if all AEs in a SOC are removed by the filter, remove the SOC overall section too
socs_keep <- ard_final |>
dplyr::filter(.data[[paste0("group", length(by) + 2)]] == ae) |>
dplyr::select(cards::all_ard_group_n(1 + length(by))) |>
dplyr::distinct()
# add remaining SOC overall sections back
ard_final <- ard_final |>
dplyr::bind_rows(
ard_aes_overall |>
dplyr::inner_join(
socs_keep,
by = names(socs_keep)
)
)
}
# apply digits
ard_final <-
ard_final |>
dplyr::rows_update(
imap(
digits,
~ enframe(.x, "stat_name", "fmt_fun") |>
dplyr::mutate(variable = .y)
) |>
dplyr::bind_rows(),
by = c("variable", "stat_name"),
unmatched = "ignore"
) |>
cards::apply_fmt_fun()
# generate table from the final ARD ------------------------------------------
tbl_final <-
gtsummary::tbl_ard_hierarchical(
cards = ard_final,
variables = all_of(variables),
by = all_of(by),
statistic = statistic,
label = label
)
# apply sorting
tbl_final <- tbl_final |> gtsummary::sort_hierarchical(sort)
# format the final table -----------------------------------------------------
# arrange grade rows by level, with all groups prior to their first level
tbl_final <- tbl_final |>
gtsummary::modify_table_body(
function(table_body) {
table_body |>
# save original row indices to keep outer hierarchy sections in the same order
dplyr::mutate(idx = dplyr::row_number()) |>
dplyr::group_by(dplyr::pick(cards::all_ard_groups(), "variable")) |>
dplyr::group_split() |>
map(function(dat) {
# only rearrange at the grade/grade group level
if (dat$variable[1] != grade) {
dat
} else {
dat$idx_grade <- NA # initialize variable to track new order of grade row indices
if (any(unlist(dat$label) %in% lvls)) {
# if individual grade levels present, arrange them in their original order (unsorted)
dat$idx_grade[is.na(dat$idx_grade) & !unlist(dat$label) %in% names(grade_groups)] <-
sapply(
dat$label[is.na(dat$idx_grade) & !unlist(dat$label) %in% names(grade_groups)],
\(x) which(lvls == unlist(x))
)
}
if (any(unlist(dat$label) %in% names(grade_groups))) {
# if grade groups present, arrange so each grade group row
# appears directly above the first grade in the group
dat$idx_grade[unlist(dat$label) %in% names(grade_groups)] <-
sapply(
dat$label[unlist(dat$label) %in% names(grade_groups)],
\(x) min(which(lvls %in% grade_groups[[unlist(x)]])) - 0.5
)
}
# arrange rows at grade level using new order, soc/ae levels using their original (sorted) order
dat |>
dplyr::arrange(.data$idx_grade, .data$idx) |>
# save new ordering
dplyr::mutate(idx = sort(dat$idx)) |>
dplyr::select(-"idx_grade")
}
}) |>
dplyr::bind_rows() |>
# apply new ordering
dplyr::arrange(.data$idx) |>
dplyr::select(-"idx")
}
)
# structural cleanup: remove duplicate/empty rows but preserve label uniqueness
tbl_final <- tbl_final |>
gtsummary::modify_table_body(
\(table_body) {
table_body |>
# remove duplicate Any AE label row
dplyr::filter(!(.data$label == "- Any adverse events -" & .data$variable != soc)) |>
# remove soc summary rows if all sub-rows filtered out
dplyr::filter(!(.data$variable == soc & dplyr::lead(.data$variable) %in% c(soc, NA))) |>
# remove rows for grades in grades_exclude
dplyr::filter(!.data$label %in% grades_exclude)
}
)
# return final table ---------------------------------------------------------
tbl_final$call_list <- list(tbl_hierarchical_rate_by_grade = match.call())
tbl_final$cards <- list(
tbl_hierarchical_rate_by_grade = list(tbl_hierarchical = tbl_final$cards$tbl_ard_hierarchical)
)
tbl_final$inputs <- tbl_hierarchical_rate_by_grade_inputs
# inject metadata for downstream post-processing by add_grade_column()
tbl_final$custom_info <- list(
soc = soc,
ae = ae,
grade = grade,
grade_groups = grade_groups,
lvls = lvls,
label_list = label
)
tbl_final |>
structure(class = c("tbl_hierarchical_rate_by_grade", "gtsummary"))
}
# function to generate the hierarchical ARD(s) by highest grade
.ard_rate_by_grade_ordered <- function(data,
variables,
by,
id,
denominator,
grade_groups = NULL) {
# get name of grade variable
grade <- dplyr::last(variables)
if (!is_empty(grade_groups)) {
keep_grade <- vapply(grade_groups, function(x) {
any(x %in% levels(data[[grade]]))
}, TRUE)
# if any grade groups present, replace grades with their grade groups
data[[grade]] <- do.call(fct_collapse, args = c(list(f = data[[grade]]), grade_groups[keep_grade]))
}
# move grade variable to `by` to get rates by highest grade
cards_ord <- cards::ard_stack_hierarchical(
data = data,
variables = all_of(setdiff(variables, grade)),
by = all_of(c(by, grade)),
id = all_of(id),
denominator = denominator,
include = all_of(dplyr::nth(variables, -2)),
total_n = (is_empty(by) && length(variables) == 1)
) |>
suppressMessages()
# retain original input args
attr(cards_ord, "args") <- list(by = by, variables = variables, include = variables)
# update structure to match results for the soc/ae variables
which_var <- which(names(cards_ord) == "variable")
which_h <- which(names(cards_ord) == paste0("group", length(by) + 1))
names(cards_ord) <- names(cards_ord)[
c(0:(which_h - 1), which_var + 0:1, which_h:(which_var - 1), (which_var + 2):length(names(cards_ord)))
]
# bind `cards_ord` to results for the soc/ae variables
variables <- setdiff(variables, grade)
if (!is_empty(by)) {
# remove summary rows that will be duplicated in the following ARD
cards_ord <- cards_ord |>
dplyr::filter(.data$group1 == by[1] | .data$context == "total_n")
}
# get remaining rates for soc/ae variables
cards <- cards::ard_stack_hierarchical(
data = data,
variables = all_of(variables),
by = any_of(by),
id = all_of(id),
denominator = denominator,
total_n = is_empty(by)
) |>
suppressMessages()
# bind ARDs for ordered and non-ordered results and return
cards::bind_ard(cards_ord, cards, .quiet = TRUE)
}
#' @rdname tbl_hierarchical_rate_by_grade
#' @export
add_overall.tbl_hierarchical_rate_by_grade <- asNamespace("gtsummary")[["add_overall.tbl_hierarchical"]]
#' @param x (`gtsummary`)\cr
#' A gtsummary table produced by [tbl_hierarchical_rate_by_grade()], or a merged table
#' (e.g., from [tbl_with_pools()]) where the underlying tables were produced by
#' [tbl_hierarchical_rate_by_grade()].
#'
#' @details
#' ## `add_grade_column()`
#'
#' Post-processing function that applies visual formatting to tables generated by
#' [tbl_hierarchical_rate_by_grade()]. Must be called **after** any merging
#' (e.g., via [tbl_with_pools()]) to avoid Cartesian join explosions caused by blanking
#' the `label` column prior to merge.
#'
#' The function extracts metadata injected by [tbl_hierarchical_rate_by_grade()] via
#' `x$custom_info` (standalone tables) or the first sub-table's `custom_info` (merged tables).
#' If no metadata is found, the function aborts with an informative error.
#'
#' `add_grade_column()` only works on tables produced by
#' [tbl_hierarchical_rate_by_grade()] — it reads the `custom_info` metadata
#' that function stores. They share a help page because they are designed
#' to be used together: build the table first, optionally merge with
#' [gtsummary::tbl_merge()] or [tbl_with_pools()], then call
#' `add_grade_column()` as the final step.
#'
#' @rdname tbl_hierarchical_rate_by_grade
#' @export
add_grade_column <- function(x) {
set_cli_abort_call()
if (!inherits(x, "gtsummary")) {
cli::cli_abort(
"{.arg x} must be a {.cls gtsummary} object.",
call = get_cli_abort_call()
)
}
# idempotency guard: skip if already applied
if ("label_grade" %in% names(x$table_body)) {
return(x)
}
# extract metadata: standalone vs merged table
info <- x$custom_info %||%
Find(Negate(is.null), lapply(x$tbls, \(t) t$custom_info))
if (is.null(info)) {
cli::cli_abort(
c(
"No {.field custom_info} metadata found on the input table.",
"i" = "Ensure the table was created with {.fun tbl_hierarchical_rate_by_grade}."
),
call = get_cli_abort_call()
)
}
soc <- info$soc
ae <- info$ae
grade <- info$grade
grade_groups <- info$grade_groups
lvls <- info$lvls
label_list <- info$label_list
# apply visual formatting to the table body
# TODO: extract anonymous functions into named helpers (#251)
x <- x |>
gtsummary::modify_table_body(
\(table_body) {
table_body |>
dplyr::rowwise() |>
# create label_grade column
dplyr::mutate(
label_grade = dplyr::case_when(
.data$variable == grade ~ .data$label,
.data$variable == ae | .data$label == "- Any adverse events -" ~ "- Any Grade -",
.default = ""
),
.after = "label"
) |>
# blank the label column for grade rows (safe after merge)
dplyr::mutate(label = if (.data$variable == grade) "" else .data$label) |>
# remove statistics from non-summary rows
dplyr::mutate(
across(
gtsummary::all_stat_cols(),
~ if (
.data$variable %in% c(ae, "..ard_hierarchical_overall..") |
.data$label_grade %in% c(lvls, names(grade_groups)) |
.data$label == "- Any adverse events -"
) {
.
} else {
NA
}
)
) |>
dplyr::ungroup()
}
) |>
# show and align label_grade column
gtsummary::modify_column_unhide("label_grade") |>
gtsummary::modify_column_alignment("label_grade", align = "left") |>
# remove default footnote
gtsummary::remove_footnote_header(columns = everything()) |>
# convert "0 (0.0%)" to "0"
gtsummary::modify_post_fmt_fun(
fmt_fun = ~ ifelse(. %in% c("0 (0.0%)", "0 (NA%)"), "0", .),
columns = gtsummary::all_stat_cols()
) |>
# update header labels
gtsummary::modify_header(
label ~ paste0(
label_list[[soc]], " \n",
paste0(rep("\U00A0", 4L), collapse = ""), label_list[[ae]]
),
label_grade ~ label_list[[grade]],
gtsummary::all_stat_cols() ~ "{level} \n(N = {n})"
)
# indent grade level labels within grade groups
if (!is_empty(grade_groups)) {
x <- x |>
gtsummary::modify_indent(
columns = "label_grade",
rows = .data$variable == grade & .data$label_grade %in% unlist(grade_groups),
indent = 4L
)
}
x
}