---
title: TSFAE02
subtitle: Subjects With Treatment-emergent Adverse Events by System Organ Class and Preferred Term
---
------------------------------------------------------------------------
{{< include ../../_utils/envir_hook.qmd >}}
```{r setup, echo = FALSE, warning = FALSE, message = FALSE}
options(docx.add_datetime = FALSE, tidytlg.add_datetime = FALSE)
envsetup_config_name <- "default"
# Path to the combined config file
envsetup_file_path <- file.path("../..", "envsetup.yml")
Sys.setenv(ENVSETUP_ENVIRON = '')
library(envsetup)
loaded_config <- config::get(config = envsetup_config_name, file = envsetup_file_path)
envsetup::rprofile(loaded_config)
dpscomp <- compound
dpspdr <- paste(protocol,dbrelease,rpteff,sep="__")
aptcomp <- compound
aptpdr <- paste(protocol,dbrelease,rpteff,sep="__")
###### Study specific updates (formerly in envre)
dpscomp <- "standards"
dpspdr <- "jjcs__NULL__jjcs - core"
apt <- FALSE
library(junco)
default_str_map <- rbind(default_str_map, c("&ctcae", "5.0"))
```
## Output
:::: panel-tabset
## {{< fa regular file-lines sm fw >}} Preview
```{r variant1, results='hide', warning = FALSE, message = FALSE}
# Program Name: tsfae02.R
# Prep Environment
library(envsetup)
library(tern)
library(dplyr)
library(rtables)
library(junco)
# Define script level parameters:
# - Define output ID and file location
tblid <- "TSFAE02"
fileid <- tblid
tab_titles <- get_titles_from_file(input_path = '../../_data/', tblid)
string_map <- default_str_map
trtvar <- "TRT01A"
popfl <- "SAFFL"
combined_colspan_trt <- TRUE
risk_diff <- TRUE
rr_method <- "wald"
ctrl_grp <- "Placebo"
if (combined_colspan_trt == TRUE) {
# Set up levels and label for the required combined columns
add_combo <- add_combo_facet(
"Combined",
label = "Combined",
levels = c("Xanomeline High Dose", "Xanomeline Low Dose")
)
# choose if any facets need to be removed - e.g remove the combined column for placebo
rm_combo_from_placebo <- cond_rm_facets(
facets = "Combined",
ancestor_pos = NA,
value = " ",
split = "colspan_trt"
)
mysplit <- make_split_fun(post = list(add_combo, rm_combo_from_placebo))
}
# Process Data:
adsl <- pharmaverseadamjnj::adsl %>%
filter(!!rlang::sym(popfl) == "Y") %>%
select(STUDYID, USUBJID, all_of(trtvar), all_of(popfl))
adae <- pharmaverseadamjnj::adae %>%
filter(TRTEMFL == "Y") %>%
select(USUBJID, TRTEMFL, AEBODSYS, AEDECOD)
adsl$colspan_trt <- factor(
ifelse(adsl[[trtvar]] == "Placebo", " ", "Active Study Agent"),
levels = c("Active Study Agent", " ")
)
if (risk_diff == TRUE) {
adsl$rrisk_header <- "Risk Difference (%) (95% CI)"
adsl$rrisk_label <- paste(adsl[[trtvar]], paste("vs", ctrl_grp))
}
# join data together
ae <- adae %>% right_join(., adsl, by = c("USUBJID"))
colspan_trt_map <- create_colspan_map(
adsl,
non_active_grp = ctrl_grp,
non_active_grp_span_lbl = " ",
active_grp_span_lbl = "Active Study Agent",
colspan_var = "colspan_trt",
trt_var = trtvar
)
ref_path <- c("colspan_trt", " ", trtvar, ctrl_grp)
# Define layout and build table:
extra_args_rr2 <- list(
denom = "n_altdf",
riskdiff = TRUE,
ref_path = ref_path,
method = "wald",
.stats = c("count_unique_fraction"),
.formats = c(
"rr_ci_3d" = jjcsformat_xx("xx.x (xx.x, xx.x)"),
"n_df" = "xx",
"aaa" = "xx"
)
)
lyt <- basic_table(
top_level_section_div = " ",
show_colcounts = TRUE,
colcount_format = "N=xx"
) %>%
split_cols_by(
"colspan_trt",
split_fun = trim_levels_to_map(map = colspan_trt_map)
)
if (combined_colspan_trt == TRUE) {
lyt <- lyt %>%
split_cols_by(trtvar, split_fun = mysplit)
} else {
lyt <- lyt %>%
split_cols_by(trtvar)
}
if (risk_diff == TRUE) {
lyt <- lyt %>%
split_cols_by("rrisk_header", nested = FALSE) %>%
split_cols_by(
trtvar,
labels_var = "rrisk_label",
split_fun = remove_split_levels("Placebo")
)
}
lyt <- lyt %>%
analyze(
"TRTEMFL",
afun = a_freq_j,
extra_args = append(
extra_args_rr2,
list(val = "Y", label = "Subjects with >=1 AE")
)
) %>%
split_rows_by(
"AEBODSYS",
split_label = "System Organ Class",
split_fun = trim_levels_in_group("AEDECOD"),
label_pos = "topleft",
section_div = c(" "),
nested = FALSE
) %>%
summarize_row_groups(
"AEBODSYS",
cfun = a_freq_j,
extra_args = extra_args_rr2
) %>%
analyze("AEDECOD", afun = a_freq_j, extra_args = extra_args_rr2) %>%
append_topleft(" Preferred Term, n (%)")
result <- build_table(lyt, ae, alt_counts_df = adsl)
## Remove the N=xx column headers for the risk difference columns
result <- remove_col_count(result)
# If there is no data remove top row and display "No data to display" text
if (length(adae$TRTEMFL) == 0) {
result <- safe_prune_table(
result,
prune_func = remove_rows(removerowtext = "Subjects with >=1 AE")
)
}
# Post-Processing step to sort by descending count on chosen active treatment columns.
if (length(adae$TRTEMFL) != 0) {
result <- sort_at_path(
result,
c("root", "AEBODSYS"),
scorefun = jj_complex_scorefun()
)
result <- sort_at_path(
result,
c("root", "AEBODSYS", "*", "AEDECOD"),
scorefun = jj_complex_scorefun()
)
}
# Add titles and footnotes:
result <- set_titles(result, tab_titles)
# Convert to tbl file and output table
tt_to_tlgrtf(string_map = string_map, tt = result, file = fileid, orientation = "landscape")
```
```{r result1, echo=FALSE, message=FALSE, warning=FALSE, test = list(result_v1 = "result")}
tt_to_flextable_j(result, tblid, string_map = string_map)
```
[Download RTF file](`r paste0(tolower(tblid), '.rtf')`)
::::