Cbac
Cbac

Reputation: 37

shiny module selectInput is not reactive

For some reason using a selectInput in a module is not reacting to changes of the input value. I added a minimum example to demonstrate my issue. If I write the same code without a module, i.e. pasting the ui and server code of the module to the main server and ui functions without the namespace function works. I don't really see the issue with my code.

require(shiny)

ui <- function(){
    dummyUI("test")
}

server <- function(input, output, session) {

    callModule(dummy, "test")
}

dummyUI <- function(id) {
    ns <-NS(id)
    uiOutput(width = 6, ns("selectMaterial"))
}

dummy <- function(input, output, session) {
    # render UI for the filters
    output$selectMaterial <- renderUI({
        selectInput(
            inputId = "selectMaterial",
            label = "Choose Materials" ,
            choices = c("a","b"),
            multiple = TRUE)
    })

    observeEvent(input$selectMaterial ,{print("hi")})
}

shinyApp(ui(), server)

Upvotes: 2

Views: 1072

Answers (1)

Gregor de Cillia
Gregor de Cillia

Reputation: 7665

As mentioned in this article (section "Using renderUI within modules "), you need to use the namespace function in renderUI.

require(shiny)

ui <- function(){
  dummyUI("test")
}

server <- function(input, output, session) {

  callModule(dummy, "test")
}

dummyUI <- function(id) {
  ns <-NS(id)
  uiOutput(width = 6, ns("selectMaterial"))
}

dummy <- function(input, output, session) {
  # render UI for the filters
  output$selectMaterial <- renderUI({
    selectInput(
      inputId = session$ns("selectMaterial"),       ## <= namespace here
      label = "Choose Materials" ,
      choices = c("a","b"),
      multiple = TRUE)
  })

  observeEvent(input$selectMaterial ,{print("hi")})
}

shinyApp(ui(), server)

Upvotes: 5

Related Questions