---
title: AET03
subtitle: Adverse Events by Greatest Intensity
---
------------------------------------------------------------------------
{{< include ../../_utils/envir_hook.qmd >}}
:::: {.panel-tabset}
```{r setup, echo = FALSE}
library(tern)
library(dplyr)
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) %>%
var_relabel(
AEBODSYS = "MedDRA System Organ Class",
AEDECOD = "MedDRA Preferred Term"
) %>%
filter(ANL01FL == "Y")
```
For illustrative purposes, we will create a new factor variable in ADAE, `ASEV` , with all intensity levels including `"LIFE THREATENING"` .
```{r pre-processing}
#| code-fold: show
adae <- adae %>% mutate(ASEV = as.character(AESEV))
adae$ASEV[1:15] <- "LIFE THREATENING"
adae <- adae %>% mutate(ASEV = factor(ASEV, levels = c("MILD", "MODERATE", "SEVERE", "LIFE THREATENING")))
```
## Standard Table
::: {.panel-tabset .nav-justified group="webr"}
## {{< fa regular file-lines sm fw >}} Preview
```{r variant1, test = list(result_v1 = "result")}
grade_groups <- list("- Any Intensity -" = c("MILD", "MODERATE", "SEVERE", "LIFE THREATENING"))
split_fun <- trim_levels_in_group
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by("ACTARM") %>%
count_occurrences_by_grade(
var = "ASEV",
grade_groups = grade_groups
) %>%
split_rows_by(
"AEBODSYS",
child_labels = "visible",
nested = TRUE,
split_fun = split_fun("ASEV"),
label_pos = "topleft",
split_label = obj_label(adae$AEBODSYS)
) %>%
summarize_occurrences_by_grade(
var = "ASEV",
grade_groups = grade_groups
) %>%
split_rows_by(
"AEDECOD",
child_labels = "visible",
nested = TRUE,
indent_mod = -1L,
split_fun = split_fun("ASEV"),
label_pos = "topleft",
split_label = obj_label(adae$AEDECOD)
) %>%
summarize_num_patients(
var = "USUBJID",
.stats = "unique",
.labels = c("- Any Intensity -")
) %>%
count_occurrences_by_grade(
var = "ASEV",
.indent_mods = -1L
) %>%
append_varlabels(adae, "AESEV", indent = 2L)
result <- lyt %>%
build_table(
adae,
alt_counts_df = adsl
) %>%
sort_at_path(
path = "AEBODSYS",
scorefun = cont_n_allcols,
decreasing = TRUE
) %>%
sort_at_path(
path = c("AEBODSYS", "*", "AEDECOD"),
scorefun = cont_n_allcols,
decreasing = TRUE
)
result
```
<!-- TODO -->
`r webr_code_labels <- c("setup", "pre-processing", "variant1")`
{{< 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
})
datanames <- c("ADSL", "ADAE")
datanames(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_by_grade(
label = "Adverse Events by Grade Table",
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", "AESOC")),
selected = "AEBODSYS"
),
grade = choices_selected(
choices = variable_choices(ADAE, c("AETOXGR", "AESEV")),
selected = "AESEV"
)
)
)
)
shinyApp(app$ui, app$server)
```
{{< include ../../_utils/shinylive.qmd >}}
:::
{{< include ../../repro.qmd >}}