title | author | date | output |
---|---|---|---|
Shiny Module Learning |
Brian S Yandell |
May 19, 2016 |
html_document |
This project is about learning how to use Shiny modules. I developed these as my modules were crashing, and I wanted to break down into simple steps. See links below for official resources.
- 2016 Shiny Developer Conference
- see talk by Garrett
- Conference Material
- Conference Material
- Modularizing Shiny app code (Joe Cheng article)
- Shiny Gallery
The folder has several R scripts. Latest material is in the oldfaithful folder. See also Conference Material on Modules. These were adapted from and inspired by Garrett's talk. Each file is a complete shiny app that can be run.
simple.R
simple Shiny script using sliderInput() and textOutput()module.R
simple.R modified to have key components in Shiny modulenested.R
example nesting two modules*_renderUI.R
examples using renderUI() in server and uiOutput() in ui*_observeEvent.R
examples using observeEvent() for actionButton()*_observe.R
examples using observe() for checkboxInput()*_renderUI_*.R
examples using renderUI and observe() or observeEvent()
Key things to notice about namespace use for modules:
- module UI and server must use same id
sliderTextUI("module")
# in ui functioncallModule(sliderText, "module", ...)
# in server function
- module server needs
session
as third argument - return argument from module server (if any) must be reactive()
- module UI needs two nameserver components
ns <- NS(id)
# to create namespace ID functiontagList(
# tag list to wrap UI entriessliderInput(ns("slider"), ...)
# to use namespace ID)
# ends tag list
- module server has no special arrangements except in use of renderUI()
ns <- server$ns
# to access namespace ID functionoutput$set_slider <- renderUI({
sliderInput(ns("slider"), ...)
# to use namespace Id})
- module server has no special arrangements for
update*Input
updateSliderInput(server, "slider", ...)
- used in
observe()
andobserveEvent()
conditionalPanel
does not work as expected with modules- see worked examples in oldfaithful folder
condition
argument is interpreted by javascriptinput
elements for a module namespace require care with JS- see Rstudio Shiny issue or TB Adams gist for helpful solutions