Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Processing input data, step by step, using Shiny modules
# -------------------------------------------------------------------
# ------------------ PROCESSING DATA MODULARLY----------------------
# -------------------------------------------------------------------
# This app processes some input text through three modules, passing
# the result of each module down to the next module. We're also
# displaying to the user the (reactive) result of each module, but
# usually we'd just be interested in the last one... In those cases,
# the modules' ui functions would be empty (except for the last
# module, of course).
library(shiny)
# This module is responsible for getting the input and for the first
# step of processing (turning the input text into a header 2)
mod1UI <- function(id){
ns <- NS(id)
textInput(ns("txtIn"), "Enter text:", "Hello"),
uiOutput(ns("txtOut"))
}
mod1 <- function(input, output, session){
processedTxt <- reactive({ h2(input$txtIn) })
output$txtOut <- renderUI( processedTxt() )
return(processedTxt)
}
# This module is responsible for the second step of processing
# (italicizing the previously processed text)
mod2UI <- function(id){
ns <- NS(id)
uiOutput(ns("txtOut"))
}
mod2 <- function(input, output, session, prev){
processedTxt <- reactive({ tags$em(prev()) })
output$txtOut <- renderUI( processedTxt())
return(processedTxt)
}
# This module is responsible for the third step of processing
# (coloring the previously processed text)
mod3UI <- function(id){
ns <- NS(id)
uiOutput(ns("txtOut"))
}
mod3 <- function(input, output, session, prev){
processedTxt <- reactive({ tags$div(prev(), style = "color: red") })
output$txtOut <- renderUI( processedTxt() )
return(processedTxt)
}
# This is the main app that calls the three modules consecutively
ui <- fluidPage(
mod1UI("myMod1"),
mod2UI("myMod2"),
mod3UI("myMod3")
)
server <- function(input, output, session) {
mod1return <- callModule(mod1, "myMod1")
mod2return <- callModule(mod2, "myMod2", mod1return)
mod3return <- callModule(mod3, "myMod3", mod2return)
}
shinyApp(ui = ui, server = server)
@mgx85

This comment has been minimized.

Copy link

@mgx85 mgx85 commented May 30, 2017

Hi, in the first module the code is missing a tagList in the UI part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment