One possibility is passing the output from one module to the other at construction time. This allows hierachic relationships between modules. There is also the possibility to create memory that is shared between two modules which I will not cover in this answer.
reactiveValues
Here i created an inputModule
and an outputModule
. The inputModule
recieves two textinputs by the user and the output module displays them via verbatimTextOutput
. The inputModule
passes the user submitted data to the output module as a reactiveValues
object called ImProxy
(input module proxy). The outputModule
accesses the data just like a list (ImProxy$text1
, ImProxy$text2
).
library(shiny)
inputModuleUI <- function(id){
ns <- NS(id)
wellPanel(h3("Input Module"),
textInput(ns('text1'), "First text"),
textInput(ns('text2'), "Second text"))
}
inputModule <- function(input, output, session){
vals <- reactiveValues()
observe({vals$text1 <- input$text1})
observe({vals$text2 <- input$text2})
return(vals)
}
outputModuleUI <- function(id){
wellPanel(h3("Output Module"),
verbatimTextOutput(NS(id, "txt")))
}
outputModule <- function(input, output, session, ImProxy){
output$txt <- renderPrint({
paste(ImProxy$text1, "&", ImProxy$text2)
})
}
ui <- fluidPage(
inputModuleUI('IM'),
outputModuleUI('OM')
)
server <- function(input, output, session){
MyImProxy <- callModule(inputModule, 'IM')
callModule(outputModule, 'OM', MyImProxy)
}
shinyApp(ui, server)
This approach can be used with observe
or observeEvent
as well.
list(reactive)
If you want to use reactive
rather than reactiveValues
, the following adaptiation of the above code can be used. You can leave the ui
functions as they are.
inputModule <- function(input, output, session){
list(
text1 = reactive({input$text1}),
text2 = reactive({input$text2})
)
}
outputModule <- function(input, output, session, ImProxy){
output$txt <- renderPrint({
paste(ImProxy$text1(), "&", ImProxy$text2())
})
}
shinyApp(ui, server)
reactive(list)
Again, this will give the same functionality for the app but the proxy pattern is slightly different.
inputModule <- function(input, output, session){
reactive(
list(
text1 = input$text1,
text2 = input$text2
)
)
}
outputModule <- function(input, output, session, ImProxy){
output$txt <- renderPrint({
paste(ImProxy()$text1, "&", ImProxy()$text2)
})
}
shinyApp(ui, server)