Super Simple Shiny Module Code

Shiny modules are especially nice as your app grows bigger and more complicated.  While there are some good resources on Shiny modules,

RStudio Shiny Modules

Steph Locke's Shiny Modules Design Patterns Series

I remember needing something simpler.  I wrote this code a while back as I learned.   Since I just recently had to refer back to it, I thought I would add in a quick post in hopes it might help someone.

library(shiny)

# this will be our super simple module UI and server
# simple UI will be blank to keep it simple
simple_moduleUI <- tagList()
# simple server will return a new runif random number
#   every 1000 milliseconds
simple_module <- function(input, output, session) {
  rctv <- reactive({
    invalidateLater(1000)
    runif(1)
  })
}

# now let's write our app which will use our
#   simple module to fill our textOutput with the random number
# UI will be just a textOutput
ui <- textOutput("randomnum")

server <- function(input, output, session) {
  # server will call our simple_module with "simple" namespace
  rndnum <- callModule(simple_module, "simple")
  # fill textOutput with the random number
  #   returned as a reactive from our callModule
  output$randomnum <- renderText({
    rndnum()
  })
}

# run our app
# use shiny.trace = TRUE if you want to see how namespacing works
# options(shiny.trace = TRUE)
shinyApp(ui, server)