Chapter 2 Shiny Modules

2.1 Naming the functions

Call the module functions in pairs e.g. * name = func() * nameUI = func()

A module’s UI function should be given a name that is suffixed with Input, Output, or UI; for example, csvFileInput, zoomableChoroplethOutput, or tabOneUI

2.2 UI

  1. Add id argument
  2. Add ns = NS(id) Make namespace function
  3. Wrap all input IDs and output Ids in ns ns(“slider”)

2.3 Server

  1. Include input, output, session arguments
  2. Do not use ns() to refer to inputs and outputs from the module

2.4 Using module server

  1. Call callModule() from insider server()
  2. 1st argument = module function (no quotes, no brackets)
  3. 2nd argument = same id as UI

2.5 Loading the functions

  1. File sourced by global.R
  2. In a package that the app loads

2.7 To return a reactive output from a module

  1. Return reactive output as a reactive expression (or list of reactive expressions)
  2. callModule() returns all of the output returned by the server function

2.8 How to pass reactive input to a module?

  1. Wrap the input as a reactive expression foo = reactive({rv$foo}) Even is already input$num
  2. Pass the reactive expression to the module as an argument e.g. module(data = foo) [No brackets]
  3. Treat the argument as a reactive expression withint the function (e.g. data())

2.9 Effective use of Shiny Modules

2.9.1 Careful design

  • What does the module do?
  • What is it trying to accomplish?
  • What should I call my module?

2.9.2 Inputs and return values

  • Static or reactive inputs?
  • Complexity of return values
  • Which outputs serve as inputs for other modules?

  • Document your modules (roxygen2 @param @return)
  • When to ()?
    • function(input, output, foo)
    • foo()
    • return(foo)

plot1vars$xvar() NOT plot1vars2()$xvar