在shiny应用程序中,瓣叶代理未使用observeevent正确更新

tcomlyy6  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(110)

我有一个闪亮的应用程序,允许用户通过checkboxGroupInput选择项目,并显示在Map上!默认情况下,所有选项都被选中。但当用户取消选择所有,最后一组将留在Map上!我不知道如何修复它!

library(shiny)
library(shinydashboard)
library(tidyverse)
library(leaflet)
library(rgdal)
library(leafgl)
library(sf)

    ui <- dashboardPage(
      dashboardHeader(title = "Basic dashboard"),
      dashboardSidebar(
        checkboxGroupInput(
          inputId="projectEX",
          label= "select project",
          choices = unique(df$Project),
          selected = unique(df$Project),
          inline = TRUE
          
        )
      ),
      dashboardBody(
        
        fluidRow(
          leafletOutput("map01", width = "100%",height= "600px")
        )
      )
    )
    
    server <- function(input, output) {
      output$map01 <- renderLeaflet({
        leaflet() %>%
          setView(lat = 55,lng = 7,zoom = 4 ) %>%
          addProviderTiles("OpenStreetMap.Mapnik",options = providerTileOptions(noWrap = TRUE))%>%
          addGlPoints(data = pts, fillColor = cols, popup = TRUE, group="External Layout")
        
      })
      
      observeEvent(input$projectEX, {
        
        map_layout <- df %>% dplyr::filter(Project %in% input$projectEX)
        
        leafletProxy("map01") %>% 
          leaflet::clearMarkers()%>%
          addBootstrapDependency() %>%
          addGlPoints(data = pts, fillColor = cols, popup = TRUE,group = "pts")%>%
          addMarkers(data = map_layout, lng = ~lon, lat = ~lat)
    
        
      })
    }
    
    shinyApp(ui, server)

并且global.r具有一些数据以再现此结果:

df1 = structure(list(lon = c(2.198533, 2.072667, 1.765167, 1.7576, 
                       1.74505, 2.0371, 2.072083, 1.968417, 2.0164, 1.972417, 2.76213322, 
                       2.76214658, 2.76215994, 2.7619378, 2.76195036, 2.7621882, 2.76174424, 
                       2.76175893, 2.76153788, 2.76155105, 7.193523, 7.192891, 7.19408, 
                       7.188541, 7.170463, 7.182107, 7.188373, 7.175124, 7.17449, 7.186017, 
                       6.08929788, 6.07133495, 6.06850258, 6.05336589, 6.05053853, 6.04769791, 
                       6.03540684, 6.03256901, 6.0297334, 6.02688322), lat = c(53.864733, 
                                                                               53.8292, 53.845083, 53.892433, 53.903783, 53.899517, 53.867583, 
                                                                               53.8865, 53.842767, 53.8452, 52.56579362, 52.57695936, 52.58812507, 
                                                                               52.59915544, 52.61045596, 52.62107368, 52.63210354, 52.64299945, 
                                                                               52.65375103, 52.6649166, 55.116223, 55.109455, 55.102346, 55.092805, 
                                                                               55.093697, 55.083025, 55.078856, 55.07562, 55.067577, 55.064098, 
                                                                               54.66623968, 54.67250461, 54.6603614, 54.67877577, 54.66663221, 
                                                                               54.65447923, 54.68504457, 54.67290028, 54.66074696, 54.64860216
                       ), Project = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
                                      "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "C", "C", "C", 
                                      "C", "C", "C", "C", "C", "C", "C", "D", "D", "D", "D", "D", "D", 
                                      "D", "D", "D", "D")), class = "data.frame", row.names = c(NA, 
                                                                                                -40L))
pts = st_as_sf(df1, coords = c("lon", "lat"), crs = 4326)
cols = topo.colors(nrow(pts))

df = structure(list(lon = c(2.198533, 2.072667, 1.765167, 1.7576, 
                            1.74505, 2.0371, 2.072083, 1.968417, 2.0164, 1.972417, 2.76213322, 
                            2.76214658, 2.76215994, 2.7619378, 2.76195036, 2.7621882, 2.76174424, 
                            2.76175893, 2.76153788, 2.76155105, 7.193523, 7.192891, 7.19408, 
                            7.188541, 7.170463, 7.182107, 7.188373, 7.175124, 7.17449, 7.186017
), lat = c(53.864733, 53.8292, 53.845083, 53.892433, 53.903783, 
           53.899517, 53.867583, 53.8865, 53.842767, 53.8452, 52.56579362, 
           52.57695936, 52.58812507, 52.59915544, 52.61045596, 52.62107368, 
           52.63210354, 52.64299945, 52.65375103, 52.6649166, 55.116223, 
           55.109455, 55.102346, 55.092805, 55.093697, 55.083025, 55.078856, 
           55.07562, 55.067577, 55.064098), Project = c("A", "A", "A", "A", 
                                                        "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", 
                                                        "B", "B", "B", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C"
           )), class = "data.frame", row.names = c(NA, -30L))
sr4lhrrt

sr4lhrrt1#

最后一个组仍保留在Map上,因为input$projectEX的值为NULL。默认情况下,observeEvent不会被NULL值触发。您只需将参数ignoreNULL=FALSE添加到observeEvent中。

observeEvent(input$projectEX, {
    
    map_layout <- df %>% dplyr::filter(Project %in% input$projectEX)
    
    leafletProxy("map01") %>% 
      leaflet::clearMarkers()%>%
      addGlPoints(data = pts, fillColor = cols, popup = TRUE,group = "pts")%>%
      addMarkers(data = map_layout, lng = ~lon, lat = ~lat)
    
    
  }, ignoreNULL = FALSE)

相关问题