Problem with Shiny filtered selectinput and ggplot graph

I have a shinyscript prepared where i want to show a graph based on two widgets. The first widget(selectInput) controls for the area i want to show a diagnostic plot for. The second widget (checkboxGroupInput) controls for the amount of data i want to show for the area selected with the first widget. So, the options for the checkboxes depend on what is selected with the selectInput.

I solved this with a htmlOutput("") in the UI and a corresponding renderUI in the server.

Everything works fine, but when i proceed to the plotting, something weird happens. I can use a reactive filter to control for the area as selected with selectInput, but when i extend the filter to also work work with the checkboxGroupInputi get the following error when i run the app:

Warning: Error in : Problem with filter() input ..2. x Input ..2 must be of size 611 or 1, not size 0. i Input ..2 is Code == input$code. 202:

This only shows when all the checkboxes are unchecked and no graph is visible. I Can plot the graph that corresponds with the checkboxes, but it only shows 5 barcharts (when for example ten are to be plotted) and the error is given.

Can someone tell me if there is something wrong with m code? And how i can resolve the error and work with these dependand widgets?

Below my code and data

Code

#libraries needed
library(shiny)
library(ggplot2)
library(dplyr)

#data needed

df <- "load in data"

# user interface ----

ui <- fluidPage(
  tabsetPanel(
   tabPanel("diagnostische tabellen",fluid = TRUE,  
    titlePanel("PQ analyse"),
    sidebarLayout(
      sidebarPanel(
        helpText("selecteer terrein waar je de PQ-data van wil bekijken"),
        #make first dropdownmenu for area  
          selectInput("terrein",
                      label = "Kies een terrein",
                      choices = sort((unique(df$Terrein))),
                      selected = 1),
        htmlOutput("code")
      ),
      mainPanel(plotOutput("map1"))))
    )
  
  )


# Server logic ----------------------------

server <- function(input, output){

  # ceate a reactive list of PQ-codes based on previous selection for area
  output$code <- renderUI({
    data_available <- df[df$Terrein == input$terrein, "Code"]
    checkboxGroupInput("code", 
                label = "PQ-code", 
                choices = sort(unique(data_available)),
                selected = unique(data_available))
})
  
  
  ## filter the data for the graph
  filtered_data <- reactive({
    filter(df, Terrein == input$terrein, Code == input$code)
  })
  
   ## GGplot graph
  output$map1 <- renderPlot({
    
      ggplot(filtered_data(), aes( x = Code, fill = as.character(Jaar))) +
      geom_bar(position = position_stack(reverse = TRUE))+
      theme(axis.text.x = element_text(angle = 45, size = 15))+
      scale_fill_brewer()+
      labs(fill='Jaar')+
      ggtitle(paste("Aantal herhalingen PQ's op",input$terrein))
  })
}

# Run app

shinyApp(ui, server)

df

structure(list(Terrein = structure(c(25L, 25L, 25L, 25L, 1L, 
1L, 1L, 1L, 1L, 1L, 29L, 29L, 13L, 13L, 13L, 7L, 7L, 7L, 7L, 
7L, 7L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 8L, 8L, 8L, 13L, 8L, 8L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 15L, 15L, 15L, 15L, 
16L, 16L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 13L, 13L, 
13L, 13L, 24L, 24L, 24L, 24L, 28L, 28L, 28L, 28L, 2L, 2L, 2L, 
2L, 2L, 2L, 23L, 23L, 23L, 23L, 23L, 22L, 21L, 21L, 21L, 21L, 
21L, 7L, 7L, 7L, 7L, 7L, 7L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 
14L, 14L, 14L, 14L, 14L, 14L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
18L, 18L, 18L, 18L, 30L, 30L, 30L, 30L, 20L, 10L, 10L, 10L, 10L, 
10L, 13L, 13L, 13L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 3L, 3L, 3L, 
3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 25L, 1L, 1L, 17L, 17L, 17L, 17L, 
13L, 13L, 13L, 13L, 13L, 13L, 23L, 23L, 23L, 23L, 23L, 3L, 3L, 
3L, 13L, 3L, 10L, 10L, 25L, 25L, 25L, 25L, 14L, 14L, 14L, 14L, 
14L, 14L, 23L, 23L, 23L, 23L, 23L, 15L, 15L, 15L, 15L, 16L, 16L, 
16L, 5L, 5L, 5L, 5L, 5L, 12L, 12L, 12L, 12L, 12L, 19L, 15L, 15L, 
15L, 15L, 9L, 16L, 16L, 16L, 8L, 19L, 16L, 19L, 8L, 8L, 16L, 
16L, 16L, 8L, 8L, 8L, 8L, 8L, 19L, 16L, 19L, 8L, 16L, 16L, 16L, 
8L, 16L, 25L, 15L, 15L, 15L, 15L, 15L, 15L, 25L, 21L, 21L, 21L, 
7L, 7L, 7L, 12L, 12L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 10L, 10L, 10L, 15L, 15L, 28L, 28L, 
28L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 25L, 25L, 25L, 7L, 7L, 
7L, 22L, 23L, 23L, 23L, 23L, 23L, 1L, 1L, 1L, 1L, 1L, 23L, 23L, 
23L, 23L, 15L, 15L, 15L, 15L, 29L, 29L, 26L, 26L, 26L, 26L, 26L, 
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 
26L, 7L, 7L, 7L, 5L, 5L, 5L, 5L, 5L, 20L, 12L, 12L, 8L, 20L, 
20L, 20L, 20L, 7L, 7L, 7L, 12L, 25L, 25L, 25L, 24L, 24L, 24L, 
20L, 20L, 15L, 15L, 15L, 15L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 15L, 15L, 
15L, 15L, 14L, 14L, 14L, 14L, 14L, 14L, 12L, 8L, 8L, 8L, 8L, 
21L, 21L, 21L, 12L, 10L, 2L, 1L, 1L, 1L, 1L, 1L, 10L, 10L, 15L, 
15L, 15L, 15L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 
26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 6L, 6L, 6L, 6L, 
6L, 14L, 14L, 14L, 14L, 23L, 23L, 23L, 23L, 15L, 15L, 15L, 15L, 
15L, 15L, 15L, 15L, 21L, 21L, 21L, 26L, 26L, 26L, 25L, 25L, 23L, 
23L, 23L, 23L, 26L, 26L, 26L, 13L, 15L, 15L, 15L, 15L, 10L, 10L, 
10L, 10L, 26L, 26L, 26L, 13L, 13L, 13L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 23L, 23L, 23L, 23L, 23L, 1L, 1L, 1L, 1L, 
1L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 15L, 15L, 15L, 15L, 23L, 
23L, 23L, 23L, 23L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 
25L, 25L, 21L, 21L, 21L, 12L, 13L, 13L, 13L, 13L, 2L), .Label = c("Arnhemse Heide", 
"ASK Doornspijkse Heide", "ASK Oldenbroekse Heide", "Balloërveld", 
"Convooi AOCS Nieuw-Milligen", "De Dellen", "de Kom", "De Stompert & Vlasakkers", 
"Deelen, VB", "Eder- en Ginkelse Heide", "Ermelosche Heide", 
"Havelte", "ISK Harskamp", "Joost Dourleinkazerne", "Kruispeel en Achterbroek", 
"Leusderheide", "Luitenant-Generaal Best Kazerne (vml. VB de Peel)", 
"Olst-Welsum", "Oude Kamp", "Oude Molen", "Radiostation Noordwijk", 
"Rucphense Heide", "Schinveldse Bossen", "Stroese Zand", "Uilenbosch (Waalsdorp)", 
"Vliehors", "Vughtse Heide", "Weerter- en Bosoverheide", "Woensdrechtse Heide", 
"Zwaluwenberg"), class = "factor"), Code = structure(c(230L, 
228L, 228L, 231L, 4L, 5L, 6L, 1L, 2L, 3L, 239L, 240L, 100L, 101L, 
102L, 116L, 117L, 118L, 119L, 120L, 121L, 10L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 26L, 27L, 28L, 30L, 29L, 14L, 15L, 16L, 23L, 24L, 
25L, 17L, 18L, 19L, 20L, 21L, 22L, 44L, 45L, 46L, 47L, 48L, 49L, 
216L, 217L, 218L, 102L, 214L, 215L, 31L, 42L, 35L, 36L, 37L, 
38L, 43L, 32L, 33L, 34L, 39L, 40L, 41L, 71L, 71L, 72L, 59L, 60L, 
61L, 62L, 57L, 65L, 63L, 64L, 58L, 55L, 56L, 67L, 68L, 68L, 69L, 
70L, 70L, 91L, 92L, 78L, 79L, 80L, 73L, 74L, 75L, 76L, 77L, 103L, 
100L, 105L, 108L, 102L, 101L, 104L, 109L, 107L, 106L, 94L, 95L, 
93L, 96L, 99L, 97L, 98L, 122L, 123L, 124L, 125L, 135L, 136L, 
225L, 222L, 219L, 220L, 221L, 223L, 226L, 224L, 227L, 106L, 105L, 
107L, 104L, 188L, 189L, 186L, 187L, 236L, 235L, 237L, 238L, 55L, 
56L, 57L, 58L, 59L, 60L, 176L, 177L, 178L, 179L, 180L, 175L, 
143L, 144L, 145L, 146L, 147L, 116L, 119L, 117L, 118L, 121L, 120L, 
163L, 165L, 160L, 161L, 162L, 164L, 166L, 111L, 110L, 112L, 113L, 
114L, 115L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 167L, 168L, 169L, 
170L, 241L, 242L, 242L, 242L, 160L, 73L, 74L, 77L, 80L, 75L, 
103L, 105L, 107L, 50L, 51L, 52L, 53L, 54L, 138L, 139L, 140L, 
151L, 152L, 153L, 158L, 159L, 1L, 2L, 3L, 4L, 5L, 6L, 231L, 1L, 
2L, 171L, 172L, 173L, 174L, 100L, 102L, 108L, 101L, 109L, 104L, 
176L, 177L, 178L, 179L, 180L, 154L, 155L, 156L, 106L, 157L, 79L, 
78L, 230L, 229L, 228L, 230L, 115L, 114L, 113L, 112L, 110L, 111L, 
176L, 177L, 178L, 179L, 180L, 122L, 123L, 124L, 125L, 137L, 135L, 
136L, 141L, 142L, 138L, 139L, 140L, 97L, 95L, 96L, 99L, 98L, 
150L, 126L, 127L, 128L, 129L, 190L, 133L, 134L, 132L, 213L, 148L, 
131L, 149L, 211L, 212L, 133L, 134L, 132L, 210L, 213L, 210L, 212L, 
211L, 148L, 131L, 149L, 210L, 134L, 133L, 132L, 213L, 130L, 231L, 
125L, 128L, 129L, 127L, 126L, 124L, 231L, 145L, 144L, 143L, 118L, 
120L, 117L, 93L, 94L, 160L, 161L, 166L, 165L, 164L, 163L, 162L, 
89L, 88L, 85L, 84L, 90L, 86L, 87L, 79L, 78L, 91L, 123L, 122L, 
238L, 237L, 235L, 92L, 80L, 75L, 74L, 76L, 77L, 73L, 232L, 233L, 
234L, 119L, 121L, 116L, 175L, 176L, 177L, 179L, 180L, 178L, 2L, 
3L, 5L, 4L, 1L, 176L, 178L, 179L, 180L, 126L, 127L, 128L, 129L, 
239L, 240L, 191L, 192L, 193L, 194L, 195L, 196L, 197L, 198L, 199L, 
200L, 201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L, 209L, 116L, 
121L, 119L, 138L, 142L, 141L, 139L, 140L, 161L, 94L, 95L, 183L, 
166L, 165L, 160L, 163L, 117L, 120L, 118L, 93L, 233L, 234L, 232L, 
189L, 187L, 186L, 162L, 164L, 128L, 126L, 129L, 127L, 74L, 75L, 
80L, 76L, 77L, 73L, 79L, 78L, 91L, 92L, 100L, 103L, 108L, 101L, 
109L, 106L, 105L, 104L, 123L, 124L, 125L, 122L, 115L, 114L, 113L, 
112L, 111L, 110L, 97L, 182L, 184L, 185L, 181L, 145L, 144L, 143L, 
96L, 82L, 66L, 2L, 3L, 4L, 5L, 1L, 83L, 81L, 128L, 129L, 126L, 
127L, 209L, 206L, 207L, 208L, 191L, 192L, 193L, 194L, 203L, 204L, 
205L, 198L, 197L, 196L, 195L, 202L, 201L, 199L, 200L, 52L, 51L, 
53L, 50L, 54L, 112L, 115L, 114L, 110L, 180L, 179L, 176L, 178L, 
122L, 124L, 126L, 127L, 128L, 129L, 123L, 125L, 145L, 144L, 143L, 
192L, 195L, 195L, 233L, 234L, 178L, 176L, 180L, 179L, 191L, 194L, 
197L, 103L, 128L, 129L, 126L, 127L, 80L, 76L, 79L, 78L, 193L, 
198L, 200L, 101L, 100L, 108L, 81L, 83L, 82L, 73L, 74L, 75L, 77L, 
91L, 92L, 176L, 177L, 178L, 180L, 179L, 1L, 2L, 3L, 4L, 5L, 93L, 
94L, 95L, 96L, 99L, 98L, 97L, 128L, 129L, 126L, 127L, 176L, 178L, 
177L, 179L, 180L, 94L, 97L, 95L, 96L, 105L, 107L, 106L, 109L, 
104L, 233L, 234L, 143L, 144L, 145L, 93L, 108L, 101L, 100L, 103L, 
58L), .Label = c("AhQ001", "AhQ002", "AhQ003", "AhQ004", "AhQ005", 
"AhQ006", "BvB001", "BvB002", "BvB003", "BvB028", "BvB029", "BvB033", 
"BvB034", "BvExA1", "BvExA2", "BvExA3", "BvExB1", "BvExB2", "BvExB3", 
"BvExC1", "BvExC2", "BvExC3", "BvExD1", "BvExD2", "BvExD3", "BvQ004", 
"BvQ005", "BvQ006", "BvQ008", "BvQ009", "BvQ028", "BvQ029", "BvQ030", 
"BvQ031", "BvQ056", "BvQ057", "BvQ061", "BvQ062", "BvQ074", "BvQ075", 
"BvQ076", "BvQ077", "BvQ078", "BvQ104", "BvQ105", "BvQ120", "BvQ121", 
"BvQ182", "BvQ183", "DeQ001", "DeQ002", "DeQ003", "DeQ004", "DeQ005", 
"DsQ001", "DsQ002", "DsQ003", "DsQ004", "DsQ005", "DsQ006", "DsQ007", 
"DsQ008", "DsQ009", "DsQ010", "DsQ011", "DsQ023", "DsQB01", "DsQB02", 
"DsQB03", "DsQB04", "DsQB05", "DsQB06", "EhQ001", "EhQ002", "EhQ003", 
"EhQ004", "EhQ005", "EhQ006", "EhQ007", "EhQ008", "EhQJ01", "EhQJ02", 
"EhQJ03", "ErQ001", "ErQ002", "ErQ003", "ErQ004", "ErQ005", "ErQ006", 
"ErQ007", "GiQ001", "GiQ002", "HaQ001", "HaQ002", "HaQ003", "HaQ004", 
"HaQ005", "HaQ006", "HaQ007", "HkQ001", "HkQ002", "HkQ003", "HkQ004", 
"HkQ005", "HkQ006", "HkQ007", "HkQ008", "HkQ009", "HkQ010", "JdQ001", 
"JdQ002", "JdQ003", "JdQ004", "JdQ005", "JdQ006", "KoQ001", "KoQ002", 
"KoQ003", "KoQ004", "KoQ005", "KoQ006", "KrQ001", "KrQ002", "KrQ003", 
"KrQ004", "KrQ005", "KrQ006", "KrQ007", "KrQ008", "LhH004", "LhPro1", 
"LhPro2", "LhPro4", "LhPRro3", "LhQ001", "LhQ002", "LhX031", 
"NmQ001", "NmQ002", "NmQ003", "NmQ004", "NmQ005", "NrQ001", "NrQ002", 
"NrQ003", "NrQ004", "NrQ005", "OkPro1", "OkPro2", "OkQ001", "OlQ001", 
"OlQ002", "OlQ003", "OlQ004", "OlQ005", "OlQ006", "OlQ007", "OlR001", 
"OlR002", "OmQ001", "OmQ002", "OmQ003", "OmQ004", "OmQ005", "OmQ006", 
"OmQ007", "OwQ001", "OwQ002", "OwQ003", "OwQ004", "PeH011", "PeH012", 
"PeH013", "PeH014", "RhQ001", "SbQ001", "SbQ002", "SbQ003", "SbQ004", 
"SbQ005", "StQ001", "StQ002", "StQ003", "StQ004", "StQ005", "SzQ001", 
"SzQ002", "SzQ003", "SzQ004", "VdR070", "VhQ001", "VhQ002", "VhQ003", 
"VhQ004", "VhQ005", "VhQ006", "VhQ007", "VhQ008", "VhQ009", "VhQ010", 
"VhQ011", "VhQ012", "VhQ013", "VhQ014", "VhQ015", "VhQ016", "VhQ017", 
"VhQ018", "VhQ019", "VlPro1", "VlPro2", "VlPro3", "VlPro4", "VlQ001", 
"VlQ002", "VlQ003", "VlQ004", "VlQ005", "VuQ001", "VuQ002", "VuQ003", 
"VuQ004", "VuQ005", "VuQ006", "VuT001", "VuT002", "VuT003", "WaQ001", 
"WaQ002", "WaQ003", "WaQ004", "WaQ005", "WaQ006", "WaQ007", "WeQ001", 
"WeQ002", "WeQ003", "WeQ004", "WhQ001", "WhQ002", "ZwQ001", "ZwQ002"
), class = "factor")), row.names = c(NA, -611L), class = "data.frame")

1 answer

  • answered 2020-07-30 04:29 YBS

    Please try the following two changes:

    1. Change filter(df, Terrein == input$terrein, Code == input$code) to

      filter(df, Terrein == input$terrein & Code == input$code)

    2. Change htmlOutput("code") to uiOutput("code")