Exercise 2

Enhance your application with pre-built modules!

Reference:

Example

library(dplyr)
library(teal.modules.general)
library(teal.modules.clinical)

# Prepare data object
data <- teal_data()
data <- within(data, {
  ADSL <- rADSL
})
join_keys(data) <- default_cdisc_join_keys["ADSL"]


# Prepare module inputs
ADSL <- data[["ADSL"]]

cs_arm_var <- choices_selected(
  choices = variable_choices(ADSL, subset = c("ARMCD", "ARM")),
  selected = "ARM"
)

demog_vars_adsl <- ADSL |>
  select(where(is.numeric) | where(is.factor)) |>
  names()


# Create app
app <- init(
  data = data,
  modules = list(
    tm_data_table("Data Table"),
    tm_t_summary(
      label = "Demographic Table",
      dataname = "ADSL",
      arm_var = cs_arm_var,
      summarize_vars = choices_selected(
        choices = variable_choices(ADSL, demog_vars_adsl),
        selected = c("SEX", "AGE", "RACE")
      )
    )
  )
)

if (Sys.getenv("QUARTO_ROOT") == "") {
  shinyApp(app$ui, app$server)
}

App

Exercise

Open editor in Shinylive

The module output requires further tweaks:

  • We dont’ want to have “ALL PATIENT” column - let’s remove it. Please read function documentation and identify the argument to be changed.

    tm_t_summary(
      ...,
      add_total = FALSE
    )
  • It is possible to select “ARM” in the “Summarize Variables” input which does not make much sense. Let’s limit the selection to only a few interesting columns of your choice. Which object to change?

    # explicit
    demog_vars_adsl <- c("AGE", "SEX", "RACE", ...)
    # alternatively: exclude
    demog_vars_adsl <- ... |> setdiff(c("AGE", ...))
  • Let’s create two modules in the same application - one with “ARM” column as arm_var and the other with “ARMCD”.

    app <- init(
      ...
      modules = list(
        ...,
        tm_t_summary(
          ...,
          arm_var = choices_selected("ARM", "ARM", fixed = TRUE)
        ),
        tm_t_summary(
          ...,
          arm_var = choices_selected("ARMCD", "ARMCD", fixed = TRUE)
        )
      )
    )