Suppose I have the following Shiny module, which doesn't work as I intend:
library(shiny)
# module
module_ui <- function(id) {
ns <- NS(id)
uiOutput(ns("choose"))
}
module_server <- function(input, output, session) {
output$choose <- renderUI({
selectInput(inputId = "option",
label = "choose your option",
choices = c("a", "b", input$entered_text))
})
}
# ui and server
ui <- fluidPage(
textInput("entered_text", label = "Enter a custom option:"),
module_ui("xyz")
)
server <- function(input, output, session) {
callModule(module_server, "xyz")
}
shinyApp(ui, server)
How can I pipe the global input, input$entered_text
(Line 10), into the module so that the text that the user entered shows up as a choice on the selectInput
UI?
I think I am supposed to use reactive()
, but I must be doing something wrong.
Indeed, you are supposed to use reactive()
and pass the result of the entered_text
to your module, as such:
library(shiny)
# module
module_ui <- function(id) {
ns <- NS(id)
uiOutput(ns("choose"))
}
module_server <- function(input, output, session, et) {
output$choose <- renderUI({
selectInput(inputId = "option",
label = "choose your option",
choices = c("a", "b", et()))
})
}
# ui and server
ui <- fluidPage(
textInput("entered_text", label = "Enter a custom option:"),
module_ui("xyz")
)
server <- function(input, output, session) {
et <- reactive(input$entered_text)
callModule(module_server, "xyz", et)
}
shinyApp(ui, server)