简体   繁体   中英

Making a dynamic CheckboxGroupInput with shiny

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 variable has data for shown in the ui. Sadly, after splitting my code into conditional panels the buttons do not filter any longer.

conditionalPanel(condition = "input.Select2 == 'AFD' | input.Select2 == 'Piraten'",
                           checkboxGroupInput("Year", label = h3("Checkbox group"), 
                                              choices =  "2013"),
                                              selected = "2013"),
         conditionalPanel(condition = "input.Select2 == 'DieLinke'",
                         checkboxGroupInput("Year", label = h3("Checkbox group"), 
                                            choices = list("2009", "2013"),
                                            selected = "2013")),
        conditionalPanel(condition = "input.Select2 == 'Gruene' | input.Select2 == 'FDP' | input.Select2 == 'SPD' | input.Select2 == 'CDU'",
                         checkboxGroupInput("Year", label = h3("Checkbox group"), 
                                            choices = list("1998", "2002", "2005","2009", "2013"),
       selected = "2013"))

Does anyone know why?

Here's an example of plot being generated in the server function:

dfParteien %>%
                                                filter(Partei %in% chosen_party) %>% 
                                                filter(Jahr %in% input$Year) %>%
                                                count(word) %>%
                                                with(wordcloud(word, n, max.words = input$Slider1, colors=pal12))

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM