I have a layer of CircleMarkers and I am trying to remove only the markers that have a certain layerId. The id's for these circle markers are in a dataframe. In reality I have millions of id's whose subset to visualize depends on a different dataset which can change over time. So I need a dynamic way to delete specific layerId's.
Below is a simple example: Suppose I have a dataframe with 3 rows with id's 1, 2 and 3. I tried to make a checkboxInput with the options to delete id's 1 and 2 or 3.
Below the inputs will trigger an ObserveEvent that use the removeMarker function. However, nothing happens. I have tried a million ways to enter the id's into the removeMarker and I have also tried several of the other ways to deletion. Either nothing happens or all disappear. I need a way to delete specific markers. Note that this in no way reflects my real life example: I have millions of points and deletion of points is not through a direct checkbox input, but based on a different dataset that can change over time.
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
checkboxInput("delete1", "Delete ID=1 and 2",value=FALSE),
checkboxInput("delete3", "Delete ID=3",value=FALSE)
),
mainPanel(
leafletOutput("map")
)
)
))
df <- data.frame(id=c(1,2,3),lng = rnorm(3, -106.1039361, 0.5) ,
lat = rnorm(3, 50.543981, 0.5))
server <- shinyServer(function(input, output, session) {
output$map <- renderLeaflet(
leaflet() %>%
addTiles() %>% addCircleMarkers(layerId=df$id,df$lng,df$lat, group='marker', radius=2, fill = TRUE,color='red')
)
observeEvent(input$delete1, {
proxy <- leafletProxy('map')
if (input$delete1){ proxy %>% removeMarker(df[1:2,1])
}
})
observeEvent(input$delete3, {
proxy <- leafletProxy('map')
if (input$delete3){ proxy %>% removeMarker(3)}
})
})
shinyApp(ui, server)