---
title: LBT01
subtitle: Laboratory Test Results and Change from Baseline by Visit
---
------------------------------------------------------------------------
{{< include ../../_utils/envir_hook.qmd >}}
```{r setup, echo = FALSE, warning = FALSE, message = FALSE}
library(tern)
library(dplyr)
adsl <- random.cdisc.data::cadsl
adlb <- random.cdisc.data::cadlb
# Ensure character variables are converted to factors and empty strings and NAs are explicit missing levels.
adsl <- df_explicit_na(adsl)
adlb <- df_explicit_na(adlb) %>%
filter(ANL01FL == "Y")
# For illustration purpose, the example focuses on "Alanine Aminotransferase
# Measurement" starting from baseline, while excluding visit at week 1 for
# subjects who were prescribed a placebo.
adlb_f <- adlb %>%
dplyr::filter(
PARAM == "Alanine Aminotransferase Measurement" &
!(ACTARM == "B: Placebo" & AVISIT == "WEEK 1 DAY 8") &
AVISIT != "SCREENING"
)
```
```{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
afun <- function(x, .var, .spl_context, ...) {
n_fun <- sum(!is.na(x), na.rm = TRUE)
if (n_fun == 0) {
mean_sd_fun <- c(NA, NA)
median_fun <- NA
min_max_fun <- c(NA, NA)
} else {
mean_sd_fun <- c(mean(x, na.rm = TRUE), sd(x, na.rm = TRUE))
median_fun <- median(x, na.rm = TRUE)
min_max_fun <- c(min(x), max(x))
}
is_chg <- .var == "CHG"
is_baseline <- .spl_context$value[which(.spl_context$split == "AVISIT")] == "BASELINE"
if (is_baseline && is_chg) n_fun <- mean_sd_fun <- median_fun <- min_max_fun <- NULL
in_rows(
"n" = n_fun,
"Mean (SD)" = mean_sd_fun,
"Median" = median_fun,
"Min - Max" = min_max_fun,
.formats = list("n" = "xx", "Mean (SD)" = "xx.xx (xx.xx)", "Median" = "xx.xx", "Min - Max" = "xx.xx - xx.xx"),
.format_na_strs = list("n" = "NE", "Mean (SD)" = "NE (NE)", "Median" = "NE", "Min - Max" = "NE - NE")
)
}
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by("ACTARM") %>%
split_rows_by("PARAM", split_fun = split_fun, label_pos = "topleft", split_label = obj_label(adlb_f$PARAM)) %>%
split_rows_by("AVISIT", split_fun = split_fun, label_pos = "topleft", split_label = obj_label(adlb_f$AVISIT)) %>%
split_cols_by_multivar(
vars = c("AVAL", "CHG"),
varlabels = c("Value at Visit", "Change from\nBaseline")
) %>%
analyze_colvars(afun = afun)
result <- build_table(lyt, adlb_f)
result
```
```{r include = FALSE}
webr_code_labels <- c("variant1")
```
{{< include ../../_utils/webr.qmd >}}
:::
## Data Setup
In order to generate the `LBT01` standard tabulation, the `adlb` dataset may be pre-processed so as to discriminate baseline from follow-up visits.
```{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
ADLB <- random.cdisc.data::cadlb
# Ensure character variables are converted to factors and empty strings and NAs are explicit missing levels.
ADSL <- df_explicit_na(ADSL)
ADLB <- df_explicit_na(ADLB)
})
datanames <- c("ADSL", "ADLB")
names(data) <- datanames
join_keys(data) <- default_cdisc_join_keys[datanames]
## Reusable Configuration For Modules
ADSL <- data[["ADSL"]]
ADLB <- data[["ADLB"]]
## Setup App
app <- init(
data = data,
modules = modules(
tm_t_summary_by(
label = "Laboratory Test Results and Change from Baseline by Visit",
dataname = "ADLB",
arm_var = choices_selected(
choices = variable_choices(ADSL, c("ARM", "ARMCD")),
selected = "ARM"
),
by_vars = choices_selected(
choices = variable_choices(ADLB, c("PARAM", "AVISIT")),
selected = c("AVISIT")
),
summarize_vars = choices_selected(
choices = variable_choices(ADLB, c("AVAL", "CHG")),
selected = c("AVAL", "CHG")
),
useNA = "ifany",
paramcd = choices_selected(
choices = value_choices(ADLB, "PARAMCD", "PARAM"),
selected = "ALT"
),
parallel_vars = TRUE
)
),
filter = teal_slices(teal_slice("ADLB", "AVAL", selected = NULL))
)
shinyApp(app$ui, app$server)
```
{{< include ../../_utils/shinylive.qmd >}}
:::
{{< include ../../repro.qmd >}}