Making a dynamic CheckboxGroupInput with shiny

后端 未结 1 1615
既然无缘
既然无缘 2021-01-16 00:14

I am trying to make a checkbox to be able to filter the dataset by years. However not every variable has all the data for every year so I wanted only the years where the var

相关标签:
1条回答
  • 2021-01-16 00:59

    Here is a working example using mtcars dataset which is going to help you achieve dynamic checkboxGroupInput and further filtering of data frame:

    library(shiny)
    library(DT)
    data <- mtcars
    
    shinyApp(
      ui = fluidPage(
        selectInput("select1", "select cyl:", choices = unique(data$cyl)),
        uiOutput("checkbox"),
    dataTableOutput("table")
      ),
      server = function(input, output) {
    
        output$checkbox <- renderUI({
          choice <-  unique(data[data$cyl %in% input$select1, "gear"])
          checkboxGroupInput("checkbox","Select gear", choices = choice, selected = choice[1])
    
        })
    
        output$table <- renderDataTable({
          data <-  data %>% filter(cyl %in% input$select1) %>% filter(gear %in% input$checkbox)
          datatable(data)
        })
    
       }
    )
    

    You do not really need to use conditionalPanel, it works much better in your case if you just move checkboxGroupInput to the server and use input$select to filter your choices.

    First of all: Your example code is not good, you should learn how to create reproducible example

    Second of all: Your code has a quite few mistakes such as giving three checkboxGroupInput widgets same ID (Year), which is not going to work, the ID must be unique for each widget in shiny.

    0 讨论(0)
提交回复
热议问题