shiny_module
shiny_module copied to clipboard
Learning about Shiny Modules
title: "Shiny Module Learning" author: "Brian S Yandell" date: "May 19, 2016" output: 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.Rsimple Shiny script using sliderInput() and textOutput()module.Rsimple.R modified to have key components in Shiny modulenested.Rexample nesting two modules*_renderUI.Rexamples using renderUI() in server and uiOutput() in ui*_observeEvent.Rexamples using observeEvent() for actionButton()*_observe.Rexamples using observe() for checkboxInput()*_renderUI_*.Rexamples 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
sessionas 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*InputupdateSliderInput(server, "slider", ...)- used in
observe()andobserveEvent()
conditionalPaneldoes not work as expected with modules- see worked examples in oldfaithful folder
conditionargument is interpreted by javascriptinputelements for a module namespace require care with JS- see Rstudio Shiny issue or TB Adams gist for helpful solutions