# Testing Shiny server logic with testServer(): defines a pure helper function # for safety population filtering and arm counts on ADSL, a Shiny module # (UI + server) for interactive filtering, and an automated verification runner # using shiny::testServer(). # # Rscript module-demo.R #' Filter safety population and summarize treatment arm counts #' #' Filters an ADSL data frame to the safety population (SAFFL == "Y") and #' optionally to a specific treatment arm (TRT01A), computing participant counts. #' #' @param data An ADSL data frame containing `SAFFL` and `TRT01A`. #' @param arm Treatment arm name to filter by, or "All" for all arms. #' @return A list with `data` (filtered data frame), `n_arm` (subject count in #' selected arm), `n_total` (total subjects in safety population), and #' `arm` (the selected arm). #' @export summarize_safety_arm <- function(data, arm = "All") { if (!is.data.frame(data)) { stop("`data` must be a data frame.", call. = FALSE) } if (!all(c("SAFFL", "TRT01A") %in% names(data))) { stop("`data` must contain `SAFFL` and `TRT01A` columns.", call. = FALSE) } saffl_data <- data[!is.na(data$SAFFL) & data$SAFFL == "Y", , drop = FALSE] n_total <- nrow(saffl_data) if (identical(arm, "All")) { filtered <- saffl_data } else { filtered <- saffl_data[ !is.na(saffl_data$TRT01A) & saffl_data$TRT01A == arm, , drop = FALSE ] } list( data = filtered, n_arm = nrow(filtered), n_total = n_total, arm = arm ) } #' UI for treatment arm filter module #' #' @param id Module namespace id. #' @param arms Character vector of available treatment arms. #' @return A shiny tagList containing input controls and text output. #' @export arm_filter_ui <- function( id, arms = c("All", "Placebo", "Xanomeline Low Dose", "Xanomeline High Dose") ) { ns <- shiny::NS(id) shiny::tagList( shiny::selectInput( inputId = ns("arm"), label = "Treatment Arm:", choices = arms, selected = "All" ), shiny::textOutput(outputId = ns("summary_text")) ) } #' Server logic for treatment arm filter module #' #' @param id Module namespace id. #' @param data ADSL data frame or a reactive expression returning one. #' @return A reactive expression returning the summary list from #' `summarize_safety_arm()`. #' @export arm_filter_server <- function(id, data = pharmaverseadam::adsl) { shiny::moduleServer(id, function(input, output, session) { resolved_data <- shiny::reactive({ if (shiny::is.reactive(data)) { data() } else { data } }) summary_res <- shiny::reactive({ selected_arm <- if (is.null(input$arm)) "All" else input$arm summarize_safety_arm(resolved_data(), arm = selected_arm) }) output$summary_text <- shiny::renderText({ res <- summary_res() sprintf( "%s: %d of %d subjects (safety population)", res$arm, res$n_arm, res$n_total ) }) shiny::reactive(summary_res()) }) } #' Run headless reactive checks on arm_filter_server using testServer #' #' @param data ADSL dataset to test against. #' @return A list containing verified summary counts and status. #' @export run_module_checks <- function(data = pharmaverseadam::adsl) { saffl_sub <- data[!is.na(data$SAFFL) & data$SAFFL == "Y", , drop = FALSE] exp_total <- nrow(saffl_sub) exp_pbo <- sum(saffl_sub$TRT01A == "Placebo", na.rm = TRUE) exp_low <- sum(saffl_sub$TRT01A == "Xanomeline Low Dose", na.rm = TRUE) exp_high <- sum(saffl_sub$TRT01A == "Xanomeline High Dose", na.rm = TRUE) results <- list() shiny::testServer(arm_filter_server, args = list(data = data), { session$setInputs(arm = "All") stopifnot(identical(summary_res()$n_arm, exp_total)) stopifnot(identical(summary_res()$n_total, exp_total)) stopifnot(identical(session$returned()$n_arm, exp_total)) stopifnot(identical(session$returned(), session$getReturned()())) session$setInputs(arm = "Placebo") stopifnot(identical(summary_res()$n_arm, exp_pbo)) stopifnot(identical(session$returned()$n_arm, exp_pbo)) pbo_text_out <- output$summary_text session$setInputs(arm = "Xanomeline Low Dose") stopifnot(identical(summary_res()$n_arm, exp_low)) session$setInputs(arm = "Xanomeline High Dose") stopifnot(identical(summary_res()$n_arm, exp_high)) results <<- list( n_total = exp_total, pbo_count = exp_pbo, low_count = exp_low, high_count = exp_high, pbo_text = pbo_text_out, passed = TRUE ) }) results } # Run only when called as a script (not when sourced by tests). if (sys.nframe() == 0L) { cat("Running testServer verification on arm_filter_server...\n") res <- run_module_checks() cat(sprintf("Total safety population: %d\n", res$n_total)) cat(sprintf("Placebo count: %d\n", res$pbo_count)) cat(sprintf("Xanomeline Low Dose: %d\n", res$low_count)) cat(sprintf("Xanomeline High Dose: %d\n", res$high_count)) cat(sprintf("Placebo output text: %s\n", res$pbo_text)) cat("All reactive tests passed via testServer().\n") }