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

make_table_10_gt #171

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
871dcea
-update gt
YoshitoKoujin Nov 17, 2023
d5a96fd
Merge branch 'main' into 156-create-make_table_10_gt@main
YoshitoKoujin Nov 17, 2023
c5e23b0
remove extra files. merge changes from main
ayogasekaram Nov 17, 2023
01a7150
quarto page update
YoshitoKoujin Nov 27, 2023
7198a56
update test that files
YoshitoKoujin Nov 29, 2023
76eb292
Merge branch 'main' of https://github.com/pharmaverse/falcon into 156…
YoshitoKoujin Nov 30, 2023
26fd953
Merge 76eb29246434c64327a0e27c97baa9b8d92bd0ff into f482f809142f7ff5b…
YoshitoKoujin Nov 30, 2023
d8056ba
[skip actions] Restyle files
github-actions[bot] Nov 30, 2023
04c94a7
Updated to align with FDA table design
YoshitoKoujin Dec 7, 2023
0ec689b
Merge 04c94a7b333cca73533797e44321928306183d97 into f482f809142f7ff5b…
YoshitoKoujin Dec 7, 2023
3ca1f87
[skip actions] Restyle files
github-actions[bot] Dec 7, 2023
3cf6441
initial commit with gt updates.
ayogasekaram Jan 23, 2024
ab6e869
rework table 10 gt function with gtsummary package.
ayogasekaram Jan 29, 2024
9b8c054
update make_table_10_gtsum as
YoshitoKoujin Feb 3, 2024
9755485
Merge branch 'main' into 156-create-make_table_10_gt@main
YoshitoKoujin Feb 5, 2024
9416b12
Merge 97554858e8ddcf99c99996895262d51ae0afccf9 into 1cef836235b78597d…
YoshitoKoujin Feb 5, 2024
50bbb39
[skip actions] Restyle files
github-actions[bot] Feb 5, 2024
cfbda4b
update
YoshitoKoujin Feb 5, 2024
a6eb522
Merge cfbda4bd646b2aa93f49d6ddc9f5bff645b8fae5 into 1cef836235b78597d…
YoshitoKoujin Feb 5, 2024
2caa1cf
[skip actions] Restyle files
github-actions[bot] Feb 5, 2024
f8fe81a
Merge branch 'main' into 156-create-make_table_10_gt@main
edelarua Apr 25, 2024
09e4b51
Refresh documentation after merging in main
edelarua Apr 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Imports:
formatters (>= 0.5.6),
ggplot2 (>= 3.4.0),
gt,
gtreg,
gtsummary,
lubridate,
magrittr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export(make_table_09)
export(make_table_09_gtsum)
export(make_table_09_tplyr)
export(make_table_10)
export(make_table_10_gtsum)
export(make_table_11)
export(make_table_12)
export(make_table_13)
Expand Down
156 changes: 152 additions & 4 deletions R/fda-table_10.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @details
#' * `adae` must contain the variables `AEBODSYS`, `AESER`, and the variables specified by
#' `arm_var`, `id_var`, `saffl_var`, `fmqsc_var`, and `fmqnam_var`.
#' * If specified, `alt_counts_df` must contain `USUBJID` and the variables specified by `arm_var` and `saffl_var`.
#' * If specified, `alt_counts_df` must contain `id_var` and the variables specified by `arm_var` and `saffl_var`.
#' * `fmqsc_var` must contain "BROAD" or "NARROW" values, one of which will be displayed in the table. Narrow is
#' selected by default (see `fmq_scope` argument).
#' * Flag variables (i.e. `XXXFL`) are expected to have two levels: `"Y"` (true) and `"N"` (false). Missing values in
Expand Down Expand Up @@ -42,6 +42,7 @@
id_var = "USUBJID",
arm_var = "ARM",
saffl_var = "SAFFL",
soc_var = "AEBODSYS",
fmqsc_var = "FMQ01SC",
fmqnam_var = "FMQ01NAM",
fmq_scope = "NARROW",
Expand All @@ -50,8 +51,8 @@
prune_0 = TRUE,
na_level = "<Missing>",
annotations = NULL) {
assert_subset(c(
"AEBODSYS", "AESER", arm_var, id_var, saffl_var, fmqsc_var, fmqnam_var
checkmate::assert_subset(c(
id_var, soc_var, "AESER", arm_var, saffl_var, fmqsc_var, fmqnam_var
), names(adae))
assert_flag_variables(adae, c(saffl_var, "AESER"))
assert_subset(toupper(fmq_scope), c("NARROW", "BROAD"))
Expand All @@ -66,7 +67,7 @@
lyt <- basic_table_annot(show_colcounts, annotations) %>%
split_cols_by_arm(arm_var, lbl_overall, risk_diff) %>%
split_rows_by(
"AEBODSYS",
soc_var,
label_pos = "topleft",
split_label = obj_label(adae$AEBODSYS)
) %>%
Expand All @@ -82,3 +83,150 @@

tbl
}


#' @describeIn make_table_10 Create FDA table 10 using functions from `gtsummary`.
#'
#' @inheritParams argument_convention
#' @param saffl_var (`character`)\cr safety population flag variable from `adae` to include in the table.
#' @param ser_var (`character`)\cr serious Event variable from `adae` to include in the table.
#' @param soc_var (`character`)\cr system organ class variable from `adae` to include in the table.
#' @param lbl_soc_var (`character`)\cr label corresponding to system organ class variable
#' `soc_var` to print in the table.
#' @param annotations (named `list` of `character`)\cr list of annotations to add to the table. Valid
#' annotation types are `title`, `subtitles`, and a list of characters called `footnotes`.
#' Each name-value pair should use the annotation type as name and the desired string as the value.
#' @param risk_diff (`list` of `character` vectors)\cr List of character vectors. Each vector must be
#' of length 2 and contain the name of treatment arms to calculate risk difference and its 95% CI for. Those names
#' must exist in the `arm_var` column of the dataset specified in `adae`.
#'
#' @return
#' * `make_table_10_gtsum` returns a `gt_tbl` object.
#'
#' @examples
#' tbl <- make_table_10_gtsum(adae = adae, alt_counts_df = adsl)
#' tbl
#'
#' annotations <- list(
#' title = "Table 10. Patients With Serious Adverse Events ",
#' subtitle = "by System Organ Class and FDA Medical Query (Narrow),
#' Safety Population, Pooled Analyses",
#' footnotes = list(
#' "Duration = [e.g., X week double-blind treatment period or median and a range
#' indicating pooled trial durations]."
#' )
#' )
#' tbl <- make_table_10_gtsum(adae,
#' alt_counts_df = adsl,
#' annotations = annotations
#' )
#' tbl
#'
#' @export
make_table_10_gtsum <- function(adae,
alt_counts_df = NULL,
show_colcounts = TRUE,
saffl_var = "SAFFL",
ser_var = "AESER",
arm_var = "ARM",
id_var = "USUBJID",
soc_var = "AEBODSYS",
fmqsc_var = "FMQ01SC",
fmqnam_var = "FMQ01NAM",
fmq_scope = "NARROW",
lbl_soc_var = formatters::var_labels(adae, fill = TRUE)[soc_var],
lbl_overall = NULL,
annotations = NULL) {
checkmate::assert_data_frame(adae)
checkmate::assert_subset(c(saffl_var, id_var, soc_var, fmqnam_var, arm_var), names(adae))
assert_flag_variables(adae, saffl_var)
checkmate::assert_factor(adae[[arm_var]])
checkmate::assert_logical(show_colcounts)
if (!is.null(alt_counts_df)) {
checkmate::assert_data_frame(alt_counts_df)
checkmate::assert_subset(c(id_var, arm_var), names(alt_counts_df))
}

adae <- adae %>%
filter(.data[[saffl_var]] == "Y", .data[[ser_var]] == "Y", .data[[fmqsc_var]] == fmq_scope)

gt_table <-
# build primary table
gtsummary::tbl_strata2(
data = adae,
strata = soc_var,
~ gtreg::tbl_ae(
data = .x |> dplyr::mutate(!!soc_var := .y),
strata = arm_var,
id = id_var,
soc = soc_var,
ae = fmqnam_var,
statistic = "{n} ({p}%)",
id_df = alt_counts_df
) %>%
{
if (!is.null(lbl_overall)) gtreg::add_overall(., across = "strata") else .
},
.combine_with = "tbl_stack",
.combine_args = list(group_header = NULL)
) |> # remove stats from SOC row
gtsummary::modify_table_body(
~ .x |>
dplyr::mutate(
dplyr::across(
gtsummary::all_stat_cols(),
function(x) ifelse(.data$variable %in% "soc", "", x)
)
)
) |>
gtsummary::modify_spanning_header(gtreg::all_ae_cols() ~ NA) |>
# bold SOC rows
gtsummary::modify_table_styling(
columns = "label",
rows = variable %in% "soc",
text_format = "bold"
)

# update column headers
if (show_colcounts) {
gt_table <- gt_table |> gtsummary::modify_header(gtreg::all_ae_cols() ~ "**{strata}** \nN = {n}")
} else {
gt_table <- gt_table |> gtsummary::modify_header(gtreg::all_ae_cols() ~ "**{strata}**")
}

# update overall column headers
if (!is.null(lbl_overall)) {
gt_table$table_styling$header <- gt_table$table_styling$header %>%
mutate(label = ifelse(stringr::str_detect(column, "^stat_1") &
stringr::str_detect(label, "\\*\\*Overall\\*\\*"),

Check warning on line 201 in R/fda-table_10.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/fda-table_10.R,line=201,col=8,[indentation_linter] Indentation should be 10 spaces but is 8 spaces.
stringr::str_replace(label, "Overall", lbl_overall), label
))
}

# update column headers and annotations
gt_table <- gt_table %>%
gtsummary::as_gt() %>%
gt::cols_label(label = gt::md(paste0(
"**System Organ Class** <br>",
"FMQ (", tools::toTitleCase(tolower(fmq_scope)), ")"
)))

if (!is.null(annotations)) {
if (!is.null(annotations[["title"]])) {
gt_table <- gt_table %>%
gt::tab_header(annotations[["title"]])
}
if (!is.null(annotations[["subtitle"]])) {
gt_table <- gt_table %>%
gt::tab_header(annotations[["title"]], subtitle = annotations[["subtitle"]])
}
if (!is.null(annotations[["footnotes"]])) {
lapply(annotations[["footnotes"]], function(x) {
gt_table <<- gt_table %>%
gt::tab_footnote(x)
})
}
}

gt_table
}
76 changes: 60 additions & 16 deletions man/make_table_10.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading