Created
September 24, 2020 07:43
-
-
Save zsigmas/c166fc3e1693ae1314cce68973a47d9e to your computer and use it in GitHub Desktop.
R Shiny Nested Dynamic Modules Returning Values Example
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) | |
innerInputUI <- function(id){ | |
ns <- NS(id) | |
tagList(textInput(ns('ttt'), "First text")) | |
} | |
innerInputServer <- function(input, output, session){ | |
ns <- session$ns | |
return(input$ttt) | |
} | |
inputModuleUI <- function(id){ | |
ns <- NS(id) | |
wellPanel(h3("Input Module"), | |
uiOutput(ns('text_pan')) | |
) | |
} | |
inputModule <- function(input, output, session, num){ | |
ns <- session$ns | |
inputs <- reactive(paste0('text', seq(num()))) | |
output$text_pan <- renderUI({ | |
purrr::map(inputs(), function(x){innerInputUI(ns(x))}) | |
}) | |
vals <- reactiveValues() | |
observe({purrr::walk(inputs(), function(x){vals[[x]]<-callModule(innerInputServer, x)})}) | |
return(vals) | |
} | |
outputModuleUI <- function(id){ | |
wellPanel(h3("Output Module"), | |
verbatimTextOutput(NS(id, "txt"))) | |
} | |
outputModule <- function(input, output, session, ImProxy){ | |
output$txt <- renderPrint({ | |
paste(purrr::map(names(ImProxy),~ImProxy[[.x]])) | |
}) | |
} | |
ui <- fluidPage( | |
textInput('cnum_pan', label='Number of panels', value='1'), | |
inputModuleUI('IM'), | |
outputModuleUI('OM') | |
) | |
server <- function(input, output, session){ | |
num_pan <-reactive(as.numeric(input$cnum_pan)) | |
MyImProxy <- callModule(inputModule, 'IM', num_pan) | |
callModule(outputModule, 'OM', MyImProxy) | |
} | |
shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have hit this wall many times and I always forget how to return values from Nested Dynamic Modules. When the modules are not dynamic there are other ways but this seems to be one that works in most situtations.
The main point is to return values inside a reactValues, and return the reactValues itself. Then access the reactValues in the parent module, or pass it around.
It does not retain the state of the inputs between changes in the number of panels