Created
March 8, 2017 16:21
-
-
Save bborgesr/4b5473773dc6c25474c21dc725c16240 to your computer and use it in GitHub Desktop.
Processing input data, step by step, using Shiny modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------------------------------------------------- | |
# ------------------ 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, in the first module the code is missing a tagList in the UI part.