简体   繁体   中英

leaflet proxy does not update correctly with observeevent in shiny app

I have a shiny app that allow user select the project via checkboxGroupInput and show that on the map. by default all options are selected, But when User de-selct all, the last group will remain on the map ! which I don't know how to fix it !!

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)

And the global.r with some data to reproduce th result:

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

The last group remains on the map because the value of input$projectEX is NULL . And by default, observeEvent is not triggered by NULL value. You just need to add the parameter ignoreNULL=FALSE to your 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)

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