Selective update of markers in leaflet map on Shiny

Hello,
I have this problem that I can't seem to solve even though I've been working on it for days and have sought help online.

I'm trying to make selective the update of markers (controlled through a checkbox) on a leaflet map on Shiny.

I was able to use leafletProxy to prevent the map from being regenerated every time. The problem is that when I select a new category from the checkbox, all markers are deleted and then added again (along with the new category). I would like to avoid this unpleasant effect in the UI by making sure that only new markers are added (and unselected ones removed).

Is it possible to do something like that?


## this is my current code


ui <- fillPage(
  
  tags$style(type = "text/css", "html, body {width:100%; height:100%}"),
  
  leafletOutput(outputId = "mappa", width = "100%", height = "100%"),
  
  absolutePanel(
    top = 10, left = "auto", right = 10, bottom = "auto",
    width = 250, height = "auto",draggable = TRUE,
    wellPanel(checkboxGroupInput(inputId = "tipo_spesa", 
                                 label = "Tipologia di spesa", 
                                 choices = unique(allegato_1$`Oggetto della spesa`),
                                 selected = unique(allegato_1$`Oggetto della spesa`)
    )
    )
  )
)



### SERVER

server <- function(input, output) {
  
  filtro <- reactive({
    allegato_1 %>% 
      filter(`Oggetto della spesa` %in% input$tipo_spesa)
  })
  
  output$mappa <- renderLeaflet({
    leaflet() %>%
      addProviderTiles(providers$Stamen.TonerLite) %>%
      setView(lng = 9.768875, lat = 45.619111, zoom = 9) %>%
      addPolygons(data = province_lonlat,
                  color = "grey",
                  weight = 1)
  })
  
  storico_input  <- reactiveValues(actual_input = allegato_1$`Oggetto della spesa`) 
  
  observeEvent(input$tipo_spesa, {storico_input$previous_input <- storico_input$actua_input
  storico_input$actual_input <- input$tipo_spesa})
  
  observe({
    leafletProxy(mapId = "mappa", data = filtro()) %>%
      clearGroup(group = "marcatori") %>%
      addCircles(group = "marcatori",
                 data = filtro, 
                 lng = filtro()$x, 
                 lat = filtro()$y,
                 stroke = FALSE,
                 color = colori(filtro()$`Oggetto della spesa`),
                 radius = 1000 + 10000 * filtro()$`FINANZIAMENTO PIANO` / max(filtro()$`FINANZIAMENTO PIANO`),
                 opacity = 0.9,
                 label = lapply(filtro()$label, HTML))  %>%
      clearGroup(group = "legenda") %>%
      addLegend(layerId = "legenda",
                position = "bottomleft", 
                values = unique(filtro()$`Oggetto della spesa`),
                labels = unique(filtro()$`Oggetto della spesa`), 
                na.label = "Non classificato", 
                colors = unique(colori(filtro()$`Oggetto della spesa`)),
                title = "Settore di investimento")
  })
  
}

shinyApp(ui = ui, server = server)

Hi, thanks for sharing code along with your question, however this is not a reprex as the data that is required to run this (even as a truncated example) is private to you
reprex

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.