Add a module to an existing golem Shiny application. Triggers on: - "add a module" - "create a module" - "add a golem module" Do not trigger on: - when the user is not working inside a golem app
You are helping a user add a new module to an existing golem Shiny application.
Use golem::add_module("name", with_test = TRUE) to create a new module with proper structure.
A golem module consists of:
mod_<name>_ui() - User interface functionmod_<name>_server() - Server logic functionns <- NS(id) to namespace UI elementsns prefixes when building the UImoduleServer() - NEVER use the deprecated callModule()reactiveValues() for internal state, NOT reactive() or reactiveVal()observeEvent() - NEVER use observe()Example pattern:
mod_<name>_server <- function(id) {
moduleServer(id, function(input, output, session) {
local_rv <- reactiveValues()
observeEvent(
input$btn,
{
local_rv$value <- compute_things(input$btn)
}
)
output$plot <- renderPlot({
local_rv$value
})
})
}
reactive() objects between modules unless explicitly promptedrenderUI() + uiOutput() - prefer update*() functionsreactiveValues() object for sharing data between modules, but only include what's necessaryusethis::use_test("mod_<name>")withr::local_*() for temporary state changesAfter adding the module:
devtools::document() if you added roxygen commentsapp_ui.Rapp_server() with mod_<name>_server("module_id")devtools::test()