繁体   English   中英

传单代理无法在闪亮的应用程序中使用 observeevent 正确更新

[英]leaflet proxy does not update correctly with observeevent in shiny app

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

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

最后一组保留在地图上,因为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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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