insightsengineering / teal.modules.clinical

Provides teal modules for the standard clinical trials outputs
https://insightsengineering.github.io/teal.modules.clinical/
Other
32 stars 17 forks source link

[Question]: `tm_t_summary` Error: Treatment variable is not a factor #1224

Closed botsp closed 2 months ago

botsp commented 2 months ago

What is your question?

adsl.zip When I load a dataset into this module, it report that message and failed to create the Demographic table; image

This might caused by the dataset that I loaded, as if use a dataset from random.cdisc.data, it would works well; But the loaded datasets is merely a common ADSL. Confused about the reasons.

Here I create two S4 object, data contains ADSL from random.cdisc.data, data_my contains a loaded ADSL; The app related codes are same.

library(teal.modules.general)
library(teal.modules.clinical)
options(shiny.useragg = FALSE)

## Data reproducible code ----
data <- within(teal_data(), {
  library(random.cdisc.data)
  library(dplyr)
  library(nestcolor)
  # optional libraries
  library(sparkline)
  ADSL <- radsl(seed = 1)
})

datanames <- c("ADSL")
datanames(data) <- datanames
join_keys(data) <- default_cdisc_join_keys[datanames]

## App configuration ----
ADSL <- data[["ADSL"]]
#################################################################
library(teal)
library(haven)
library(teal.data)

file_names <- c("ADSL")
for (i in seq_along(file_paths)) {
  data_my <- within(
    teal_data(), 
    {ADSL<- read_xpt("..../cdiscpilot01/adam/adsl.xpt" )
}
  )
}
datanames(data_my) <-  file_names
join_keys(data_my)  <-join_keys(
  # Primary keys
  join_key("ADSL", keys = c("STUDYID","USUBJID")))

#################################################################
## App header and footer ----
nest_logo <- "https://raw.githubusercontent.com/insightsengineering/hex-stickers/main/PNG/nest.png"
app_source <- "https://github.com/insightsengineering/teal.gallery/tree/main/safety"
gh_issues_page <- "https://github.com/insightsengineering/teal.gallery/issues"

header <- tags$span(
  style = "display: flex; align-items: center; justify-content: space-between; margin: 10px 0 10px 0;",
  tags$span("My first teal app", style = "font-size: 30px;"),
  tags$span(
    style = "display: flex; align-items: center;",
    tags$img(src = nest_logo, alt = "NEST logo", height = "45px", style = "margin-right:10px;"),
    tags$span(style = "font-size: 24px;", "NEST @ Roche")
  )
)

footer <- tags$p(
  "This teal app is brought to you by the NEST Team at Roche/Genentech.
        For more information, please visit:",
  tags$a(href = app_source, target = "_blank", "Source Code"), ", ",
  tags$a(href = gh_issues_page, target = "_blank", "Report Issues")
)

## Setup App
app <- teal::init(
  data = data_my ,
  title = build_app_title("Safety Analysis Teal Demo App", nest_logo),
  header = header,
  footer = footer,
  # Set initial filter state as safety-evaluable population
  filter = teal_slices(
    count_type = "all",
    teal_slice(dataname = "ADSL", varname = "SAFFL", selected = "Y"),
    teal_slice(dataname = "ADSL", varname = "SEX"),
    teal_slice(dataname = "ADSL", varname = "AGE")
  ),
  modules = modules(
    tm_front_page(
      label = "App Info",
      header_text = c("Info about input data source" = "This app uses CDISC ADaM datasets randomly generated by `random.cdisc.data` R packages"),
      tables = list(`NEST packages used in this demo app` = data.frame(
        Packages = c(
          "teal.modules.general",
          "teal.modules.clinical",
          "random.cdisc.data"
        )
      ))
    ),
    tm_data_table("Data Table"),
    tm_variable_browser("Variable Browser"),
    tm_t_summary(
      label = "Demographic Table",
      dataname = "ADSL",
      arm_var =   choices_selected("ARM", "ARM"),
      summarize_vars = choices_selected(
        c("SEX", "RACE", "AGE"),
        selected = c("SEX", "AGE", "RACE")
      )
    )
  )
)

shinyApp(app$ui, app$server)

Code of Conduct

Contribution Guidelines

Security Policy

botsp commented 2 months ago

I found the reason. Although teal_data() and teal_data_module() can successfully create a verified S4 object, the data types in R are quite different from those generated by cdisc_data().

The data produced by cdisc_data() has predefined data types, such as setting ARM as a factor; and this is the data type required by tm_t_summary(). image