---
title: MHT01
subtitle: Medical History
---
------------------------------------------------------------------------
{{< include ../../_utils/envir_hook.qmd >}}
```{r setup, echo = FALSE, warning = FALSE, message = FALSE}
library(tern)
library(dplyr)
adsl <- random.cdisc.data::cadsl
admh <- random.cdisc.data::cadmh
# Ensure character variables are converted to factors and empty strings and NAs are explicit missing levels.
adsl <- df_explicit_na(adsl)
admh <- df_explicit_na(admh)
adsl_f <- adsl %>%
filter(SAFFL == "Y") %>%
select(USUBJID, ACTARM)
admh_f <- admh %>%
filter(SAFFL == "Y" & MHBODSYS != "" & MHDECOD != "") %>%
var_relabel(
MHBODSYS = "MedDRA System Organ Class",
MHDECOD = "MedDRA Preferred Term"
)
```
```{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")}
split_fun <- drop_split_levels
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by("ACTARM") %>%
analyze_num_patients(
vars = "USUBJID",
.stats = c("unique", "nonunique"),
.labels = c("Total number of patients with at least one condition", "Total number of conditions")
) %>%
split_rows_by(
var = "MHBODSYS",
split_fun = split_fun,
label_pos = "topleft",
split_label = obj_label(admh_f$MHBODSYS)
) %>%
analyze_num_patients(
vars = "USUBJID",
.stats = c("unique", "nonunique"),
.labels = c("Total number of patients with at least one condition", "Total number of conditions"),
show_labels = "hidden"
) %>%
count_occurrences(vars = "MHDECOD") %>%
append_varlabels(admh_f, "MHDECOD", indent = 1L)
result <- build_table(lyt, admh_f, alt_counts_df = adsl_f) %>%
prune_table()
result
```
```{r include = FALSE}
webr_code_labels <- c("variant1")
```
{{< include ../../_utils/webr.qmd >}}
:::
## Table of History <br/> Prior to Study
::: {.panel-tabset .nav-justified group="webr"}
## {{< fa regular file-lines sm fw >}} Preview
```{r variant2, test = list(result_v2 = "result")}
admh_f_prior <- admh_f %>%
filter(ASTDY <= 0)
result <- build_table(lyt, admh_f_prior, alt_counts_df = adsl_f) %>%
prune_table()
result
```
```{r include = FALSE}
webr_code_labels <- c("variant1", "variant2")
```
{{< include ../../_utils/webr.qmd >}}
:::
## Table with Total Number <br/> of Conditions Suppressed
::: {.panel-tabset .nav-justified group="webr"}
## {{< fa regular file-lines sm fw >}} Preview
```{r variant3, test = list(result_v3 = "result")}
split_fun <- drop_split_levels
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by("ACTARM") %>%
analyze_num_patients(
vars = "USUBJID",
.stats = c("unique"),
.labels = c("Total number of patients with at least one condition")
) %>%
split_rows_by(
var = "MHBODSYS",
split_fun = split_fun,
label_pos = "topleft",
split_label = obj_label(admh_f$MHBODSYS)
) %>%
analyze_num_patients(
vars = "USUBJID",
.stats = c("unique"),
.labels = c("Total number of patients with at least one condition"),
show_labels = "hidden"
) %>%
count_occurrences(vars = "MHDECOD") %>%
append_varlabels(admh_f, "MHDECOD", indent = 1L)
result <- build_table(lyt, admh_f, alt_counts_df = adsl) %>%
prune_table()
result
```
```{r include = FALSE}
webr_code_labels <- c("variant3")
```
{{< include ../../_utils/webr.qmd >}}
:::
## Table with Total Number of Conditions Per Body <br/> System After The Summary of Patients
Not currently supported by `rtables`. Users, please notify us if this variation is important to you.
## Table Showing Additional <br/> "All Patients" Column
Conditions are further sorted by decreasing high level terms and low level terms.
::: {.panel-tabset .nav-justified group="webr"}
## {{< fa regular file-lines sm fw >}} Preview
```{r variant4, test = list(result_v4 = "result")}
split_fun <- drop_split_levels
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by("ACTARM") %>%
add_overall_col("All Patients") %>%
analyze_num_patients(
"USUBJID",
.stats = c("unique", "nonunique"),
.labels = c(unique = "Total number of patients with at least one event", nonunique = "Total number of conditions")
) %>%
split_rows_by(
var = "MHBODSYS",
split_fun = split_fun,
child_labels = "visible",
label_pos = "topleft",
split_label = obj_label(admh_f$MHBODSYS)
) %>%
summarize_num_patients(
"USUBJID",
.stats = c("unique", "nonunique"),
.labels = c(unique = "Total number of patients with at least one event", nonunique = "Total number of conditions")
) %>%
count_occurrences(vars = "MHDECOD", .indent_mods = -1L) %>%
append_varlabels(admh_f, "MHDECOD", indent = 1L)
scorefun_hlt <- cont_n_allcols
scorefun_llt <- score_occurrences_cols(col_indices = nlevels(adsl_f$ACTARM) + 1)
result <- build_table(lyt, admh_f, alt_counts_df = adsl_f) %>%
prune_table() %>%
sort_at_path(path = c("MHBODSYS"), scorefun = scorefun_hlt) %>%
sort_at_path(path = c("MHBODSYS", "*", "MHDECOD"), scorefun = scorefun_llt)
result
```
```{r include = FALSE}
webr_code_labels <- c("variant4")
```
{{< 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, {
library(dplyr)
ADSL <- random.cdisc.data::cadsl %>%
filter(SAFFL == "Y")
ADMH <- random.cdisc.data::cadmh %>%
filter(SAFFL == "Y" & MHBODSYS != "" & MHDECOD != "")
})
datanames <- c("ADSL", "ADMH")
names(data) <- datanames
join_keys(data) <- default_cdisc_join_keys[datanames]
## Reusable Configuration For Modules
ADMH <- data[["ADMH"]]
## Setup App
app <- init(
data = data,
modules = modules(
tm_t_events(
label = "Medical History Table",
dataname = "ADMH",
arm_var = choices_selected(c("ARM", "ARMCD"), "ARM"),
llt = choices_selected(
choices = variable_choices(ADMH, c("MHTERM", "MHDECOD")),
selected = c("MHDECOD")
),
hlt = choices_selected(
choices = variable_choices(ADMH, c("MHBODSYS", "MHSOC")),
selected = "MHBODSYS"
),
add_total = TRUE,
event_type = "condition"
)
)
)
shinyApp(app$ui, app$server)
```
{{< include ../../_utils/shinylive.qmd >}}
:::
{{< include ../../repro.qmd >}}