Shiny · R · cheat sheet
Where state lives decides how a Shiny app grows. Two patterns cover most modules; both are tested here with shiny::testServer, the way a module should be tested before it ships.
RPattern 1: the module returns its state. Tested with testServer.
library(shiny)
counter_server <- function(id, step = reactive(1)) {
moduleServer(id, function(input, output, session) {
total <- reactiveVal(0)
observeEvent(input$add, total(total() + step()))
total # the caller reads this reactive
})
}
testServer(counter_server, args = list(step = reactive(2)), {
session$setInputs(add = 1)
session$setInputs(add = 2)
stopifnot(session$returned() == 4)
cat("returned after two clicks with step 2:", session$returned(), "\n")
})Ran when this sheet was built. Output:
returned after two clicks with step 2: 4
RPattern 3: per-session shared state through session$userData.
library(shiny)
picks_server <- function(id) {
moduleServer(id, function(input, output, session) {
if (is.null(session$userData$picks)) session$userData$picks <- reactiveVal(character())
observeEvent(input$pick, session$userData$picks(c(session$userData$picks(), input$pick)))
})
}
testServer(picks_server, {
session$setInputs(pick = "FF")
session$setInputs(pick = "SL")
cat("shared picks:", session$userData$picks(), "\n")
})Ran when this sheet was built. Output:
shared picks: FF SL
Loading the calendar.
The calendar did not load here. Open it on Calendly.
Confirmed
Calendly has sent the invitation to your inbox, with the link for the call. Michael will see you then.
We've teamed up with AthlyticZ to supercharge how we use data science and statistics to impact our sports. More clarity in the numbers means bigger results for our athletes.
AthlyticZ has completely transformed the learning approach to data science through the use of sports-based problems. The instructors are the best of the best, and the practical projects have immediate impact to students.
The sports industry moves fast. Managing the pace of evolving technology, data, and AI isn't optional - it's imperative. Our Analytics team was looking for a partner to meet us where we are, to help us in the continued investment of our people and capabilities without pulling us away from the work that matters. AthlyticZ delivers exactly that: a structured, practitioner-led platform that fits how modern sports analytics groups actually operate.