闪亮的过滤选择输入和 ggplot 图的问题

Problem with Shiny filtered selectinput and ggplot graph

我准备了一个闪亮的脚本,我想在其中显示基于两个小部件的图表。 第一个小部件 (selectInput) 控制我想显示诊断图的区域。 第二个小部件 (checkboxGroupInput) 控制我想为第一个小部件选择的区域显示的数据量。因此,复选框的选项取决于使用 selectInput 选择的内容。

我用 UI 中的 htmlOutput("") 和服务器中相应的 renderUI 解决了这个问题。

一切正常,但是当我继续绘图时,发生了一些奇怪的事情。 我可以使用反应式过滤器来控制通过 selectInput 选择的区域,但是当我扩展过滤器以也使用 checkboxGroupInput 时,我在 运行 应用程序时收到以下错误:

警告:错误:filter() 输入 ..2 有问题。 x 输入 ..2 的大小必须为 611 或 1,而不是大小 0。 i 输入 ..2Code == input$code。 202:

这仅在所有复选框都未选中且没有图表可见时显示。我可以绘制与复选框对应的图形,但它只显示 5 个条形图(例如要绘制 10 个条形图)并给出错误。

有人可以告诉我 m 代码是否有问题吗?我如何解决错误并使用这些依赖和小部件?

下面是我的代码和数据

代码

#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")

由于每个地形都有多个代码,因此您应该使用 %in%。此外,您需要定义每个条形计数。试试这个

# 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),
                 uiOutput("mycode")
               ),
               mainPanel(plotOutput("map1"))))
  )
  
)

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

server <- function(input, output){
  
  # ceate a reactive list of PQ-codes based on previous selection for area
  output$mycode <- 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({
    dat <- filter(df, Terrein == input$terrein & Code %in% input$code)
    data <- dat %>% group_by(Code) %>% 
      dplyr::summarise(n=n())
    data
  })
  
  ## GGplot graph
  output$map1 <- renderPlot({
    
    ggplot(filtered_data(), aes( x = Code, y=n, fill = Code )) +
      geom_bar(position = position_stack(reverse = TRUE), stat = "identity")+
      theme(axis.text.x = element_text(angle = 45, size = 15))+
      scale_fill_brewer()+
      labs(fill=NULL)+
      ggtitle(paste("Aantal herhalingen PQ's op",input$terrein))
  })
}

# Run app

shinyApp(ui, server)

你会得到这个输出:

请注意,没有定义 Jaar,因此您可能需要定义它。