Created
January 6, 2020 19:53
-
-
Save gadenbuie/7178e1a9a24f9c7009124a188a41ebd2 to your computer and use it in GitHub Desktop.
A midly convoluted example of shared inputs between 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
library(shiny) | |
# Here's a simple text module that with one text input and that shows a local result | |
textModUI <- function(id, label = "Word") { | |
ns <- NS(id) | |
tagList( | |
textInput(ns("word"), label), | |
verbatimTextOutput(ns("result")) | |
) | |
} | |
#' A module that takes two inputs: prefix and suffix and applies them to a word | |
#' Only the input$word in each module is returned and the value that you get | |
#' from a module is the same as a reactive expression, i.e. | |
textMod <- function(input, output, session, | |
prefix = reactive(NULL), | |
suffix = reactive(NULL)) { | |
ns <- session | |
# prefix() and suffix() are still reactives inside here | |
output$result <- renderPrint({ | |
paste0(prefix(), input$word, suffix()) | |
}) | |
return(reactive(input$word)) | |
} | |
ui <- fluidPage( | |
div( | |
class = "col-xs-3", | |
textModUI('prefix', "Prefix") | |
), | |
div( | |
class = "col-xs-3", | |
textModUI("word") | |
), | |
div( | |
class = "col-xs-3", | |
textModUI('suffix', "Suffix") | |
), | |
div( | |
class = "col-xs-9", | |
verbatimTextOutput("debug") | |
) | |
) | |
server <- function(input, output, session) { | |
prefix <- callModule(textMod, "prefix") | |
suffix <- callModule(textMod, "suffix") | |
# pass the reactives right into the module | |
word <- callModule(textMod, "word", prefix = prefix, suffix = suffix) | |
output$debug <- renderPrint({ | |
glue::glue( | |
"As seen in the global Shiny reactive space...\n", | |
"prefix() : {prefix()}\n", | |
"word() : {word()}\n", | |
"suffix() : {suffix()}" | |
) | |
}) | |
} | |
shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment