---
title: AET09
subtitle: Adverse Events Related to Study Drug
---
------------------------------------------------------------------------
{{< include ../../_utils/envir_hook.qmd >}}
```{r setup, echo = FALSE, warning = FALSE, message = FALSE}
library(dplyr)
library(tern)
adsl <- random.cdisc.data::cadsl
adae <- random.cdisc.data::cadae
# Ensure character variables are converted to factors and empty strings and NAs are explicit missing levels.
adsl <- df_explicit_na(adsl)
adae <- df_explicit_na(adae)
adae_labels <- var_labels(adae)
adae_f <- adae %>%
filter(AEREL == "Y")
var_labels(adae_f) <- adae_labels
```
```{r include = FALSE}
webr_code_labels <- c("setup")
```
{{< include ../../_utils/webr_no_include.qmd >}}
## Output
::::: panel-tabset
## Standard Table
::: {.panel-tabset .nav-justified group="webr"}
## {{< fa regular file-lines sm fw >}} Preview
```{r variant1, test = list(result_v1 = "result")}
# Define the split function
split_fun <- drop_split_levels
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by(var = "ARM") %>%
add_overall_col(label = "All Patients") %>%
analyze_num_patients(
vars = "USUBJID",
.stats = c("unique", "nonunique"),
.labels = c(
unique = "Total number of patients with at least one adverse event related to study drug",
nonunique = "Overall total number of events related to study drug"
)
) %>%
split_rows_by(
"AEBODSYS",
child_labels = "visible",
nested = FALSE,
split_fun = split_fun,
label_pos = "topleft",
split_label = obj_label(adae_f$AEBODSYS)
) %>%
summarize_num_patients(
var = "USUBJID",
.stats = c("unique", "nonunique"),
.labels = c(
unique = "Total number of patients with at least one adverse event related to study drug",
nonunique = "Total number of events related to study drug"
)
) %>%
count_occurrences(
vars = "AEDECOD",
.indent_mods = -1L
) %>%
append_varlabels(adae_f, "AEDECOD", indent = 1L)
tbl1 <- build_table(
lyt = lyt,
df = adae_f,
alt_counts_df = adsl
) %>%
prune_table()
result <- tbl1 %>%
prune_table() %>%
sort_at_path(
path = c("AEBODSYS"),
scorefun = cont_n_allcols
) %>%
sort_at_path(
path = c("AEBODSYS", "*", "AEDECOD"),
scorefun = score_occurrences
)
result
```
```{r include = FALSE}
webr_code_labels <- c("variant1")
```
{{< include ../../_utils/webr.qmd >}}
:::
## Table Including High-Level Terms
::: {.panel-tabset .nav-justified group="webr"}
## {{< fa regular file-lines sm fw >}} Preview
```{r variant2, test = list(result_v2 = "result")}
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by(var = "ARM") %>%
analyze_num_patients(
vars = "USUBJID",
.stats = c("unique", "nonunique"),
.labels = c(
unique = "Total number of patients with at least one adverse event related to study drug",
nonunique = "Overall total number of events related to study drug"
)
) %>%
split_rows_by(
"AEBODSYS",
child_labels = "visible",
nested = FALSE,
split_fun = split_fun,
indent_mod = 1L,
label_pos = "topleft",
split_label = obj_label(adae_f$AEBODSYS)
) %>%
summarize_num_patients(
var = "USUBJID",
.stats = c("unique", "nonunique"),
.labels = c(
unique = "Total number of patients with at least one adverse event related to study drug",
nonunique = "Total number of events related to study drug"
)
) %>%
split_rows_by(
"AEHLT",
child_labels = "visible",
nested = TRUE,
indent_mod = -1L,
split_fun = split_fun,
label_pos = "topleft",
split_label = obj_label(adae_f$AEHLT)
) %>%
summarize_num_patients(
var = "USUBJID",
.stats = c("unique", "nonunique"),
.labels = c(
unique = "Total number of patients with at least one adverse event related to study drug",
nonunique = "Total number of events related to study drug"
)
) %>%
count_occurrences(
vars = "AEDECOD",
.indent_mods = -1L
) %>%
append_varlabels(adae_f, c("AEDECOD"), indent = 2L)
tbl2 <- build_table(
lyt = lyt,
df = adae_f,
alt_counts_df = adsl
) %>%
prune_table()
result <- tbl2 %>%
sort_at_path(
path = c("AEBODSYS"),
scorefun = cont_n_allcols
) %>%
sort_at_path(
path = c("AEBODSYS", "*", "AEHLT"),
scorefun = cont_n_allcols
) %>%
sort_at_path(
path = c("AEBODSYS", "*", "AEHLT", "*", "AEDECOD"),
scorefun = score_occurrences
)
result
```
```{r include = FALSE}
webr_code_labels <- c("variant2")
```
{{< include ../../_utils/webr.qmd >}}
:::
## Data Setup
```{r setup}
#| code-fold: show
```
:::::
{{< include ../../_utils/save_results.qmd >}}
## `teal` App
::: {.panel-tabset .nav-justified}
## {{< fa regular file-lines fa-sm fa-fw >}} Preview
```{r teal, opts.label = c("skip_if_testing", "app")}
library(teal.modules.clinical)
## Data reproducible code
data <- teal_data()
data <- within(data, {
ADSL <- random.cdisc.data::cadsl
ADAE <- random.cdisc.data::cadae
# Ensure character variables are converted to factors and empty strings and NAs are explicit missing levels.
ADSL <- df_explicit_na(ADSL)
ADAE <- df_explicit_na(ADAE)
})
datanames <- c("ADSL", "ADAE")
names(data) <- datanames
join_keys(data) <- default_cdisc_join_keys[datanames]
## Reusable Configuration For Modules
ADAE <- data[["ADAE"]]
## Setup App
app <- init(
data = data,
modules = modules(
tm_t_events(
label = "Adverse Events Related to Study Drug",
dataname = "ADAE",
arm_var = choices_selected(c("ARM", "ARMCD"), "ARM"),
llt = choices_selected(
choices = variable_choices(ADAE, c("AETERM", "AEDECOD")),
selected = c("AEDECOD")
),
hlt = choices_selected(
choices = variable_choices(ADAE, c("AEBODSYS", "AEHLT")),
selected = c("AEBODSYS")
),
add_total = FALSE,
event_type = "adverse event related to study drug", # define event type here
)
),
filter = teal_slices(teal_slice("ADAE", "AEREL", selected = "Y")) # related AEs only
)
shinyApp(app$ui, app$server)
```
{{< include ../../_utils/shinylive.qmd >}}
:::
{{< include ../../repro.qmd >}}