# A small Shiny form whose mascot reports what a date check found: the app # from the post's animation. Defines a pure helper that checks visit dates, # a mapping from its result to a mascot mood and status line, the app itself, # and a headless check of the server logic with shiny::testServer(). # # Rscript review-demo.R # runs the headless check # shiny::runApp(review_app()) # opens the app (after sourcing this file) #' Check visit dates typed one per line #' #' A date needs a look when it cannot be read as `YYYY-MM-DD` or lies after #' `today`. #' #' @param lines Character vector, one visit date per element. #' @param today The latest date accepted. Pass it explicitly in tests. #' @return A list with `n_rows` (non-empty lines), `bad` (the lines that need #' a look) and `n_bad`. #' @export check_dates <- function(lines, today = Sys.Date()) { if (!is.character(lines)) { stop("`lines` must be a character vector.", call. = FALSE) } lines <- trimws(lines) lines <- lines[nzchar(lines)] parsed <- as.Date(lines, format = "%Y-%m-%d", optional = TRUE) needs_look <- is.na(parsed) | parsed > today list( n_rows = length(lines), bad = lines[needs_look], n_bad = sum(needs_look) ) } #' Turn a date check into a mood and a status line #' #' The status text says the same thing as the face, so the face never carries #' the meaning alone. #' #' @param check The list returned by `check_dates()`. #' @return A list with `mood` and `status`, ready for `update_mascot()`. #' @export mascot_state <- function(check) { if (check$n_rows == 0L) { return(list(mood = "idle", status = "Paste some dates to check")) } if (check$n_bad == 0L) { return(list(mood = "happy", status = "All checks passed")) } status <- if (check$n_bad == 1L) { "One date needs a look" } else { paste(check$n_bad, "dates need a look") } list(mood = "worried", status = status) } review_ui <- function() { shiny::fluidPage( mascot::mascot( "reviewer", mood = "idle", status = "Paste some dates to check", placement = "inline" ), shiny::textAreaInput("dates", "Visit dates (YYYY-MM-DD)", rows = 6), shiny::actionButton("check", "Check"), shiny::verbatimTextOutput("flagged") ) } review_server <- function(today = Sys.Date()) { function(input, output, session) { result <- shiny::eventReactive(input$check, { check_dates(strsplit(input$dates, "\n", fixed = TRUE)[[1]], today = today) }) shiny::observeEvent(result(), { state <- mascot_state(result()) mascot::update_mascot( "reviewer", mood = state$mood, status = state$status ) }) output$flagged <- shiny::renderText({ paste(result()$bad, collapse = "\n") }) } } review_app <- function(today = Sys.Date()) { shiny::shinyApp(review_ui(), review_server(today)) } #' Run headless checks on the server logic with testServer #' #' Sets the input as a user would, then checks the flagged rows and that #' `update_mascot()` runs without a warning (it warns, and leaves the mascot #' unchanged, when it is given an argument it cannot use). #' #' @return `TRUE`, invisibly, if every check passes. #' @export run_review_checks <- function() { today <- as.Date("2026-11-12") shiny::testServer(review_server(today), { session$setInputs(dates = "2026-03-02\n2026-13-45\n2027-01-05", check = 1) stopifnot(identical(output$flagged, "2026-13-45\n2027-01-05")) session$setInputs(dates = "2026-03-02\n2026-03-09", check = 2) stopifnot(identical(output$flagged, "")) }) invisible(TRUE) } if (sys.nframe() == 0L) { run_review_checks() cat("review-demo.R: checks passed\n") }