Link selectInput with sliderInput in shiny

前端 未结 1 1333
执念已碎
执念已碎 2020-11-30 15:51

Friends, I would like my selectInput to be linked to the number of clusters that appear in my output table. In other words, it appears divided into 5 clusters. In selectInpu

相关标签:
1条回答
  • 2020-11-30 16:31

    You were really close with the update expression. All you need there is:

      observeEvent(input$Slider,{
        updateSelectInput(session,'select',
                          choices=unique(1:input$Slider))
      }) 
    

    Another approach is to use uiOutput/renderUI. In the ui, instead of creating an empty selectInput, we can put a placeholder:

    uiOutput("select_clusters")
    

    Then in the server, we populate this placeholder:

    output$select_clusters <- renderUI({
      selectInput("select", label = h3("Select the cluster"), choices = 1:input$Slider)
    })
    

    Edit

    To make an observeEvent (or eventReactive) react to multiple inputs, wrap the inputs or reactives in c():

    observeEvent(c(input$SLIDER, input$FILTER),{
        updateSelectInput(session,'select',
                          choices=unique(1:input$Slider))
      }) 
    

    But if you need to do that, I think it makes more sense, and gives flexibility, to go with the renderUI approach. This might look something like:

    output$select_clusters <- renderUI({
       req(input$slider)
       req(input$filter)
    
       df2 <- df[df$something %in% input$filter, ]
    
      selectInput("select", 
                  label = h3("Select the cluster"), 
                  choices = df2$something)
    
    })
    
    

    In general, with the update*Input function, you can only update an existing widget, you can't remove it. But if the number of clusters = 1, then you do not need a select input at all. With renderUI you can use an empty HTML container (div()) to 'hide' the selectInput if the conditions require it:

    what_to_do <- reactive({
       req(input$Slider)
       if (input$Slider == 1) {
          x <- div() 
       } else {
          x <- selectInput("select", 
                           label = h3("Select the cluster"), 
                           choices = 1:input$Slider)
       }
    
    return(x)
    })
    
    output$select_clusters <- renderUI({
       what_to_do()
    })
    
    0 讨论(0)
提交回复
热议问题