I want to create an interactive leaflet map with Shiny. The creation works fine, but despite changes at selectInput() and clicking "go!" the old markers are not deleted and no new markers are set. Can you please help me?
Operating System: Windows 10 64-Bit RStudio R Version: R version 4.0.0
geocodes <- data.frame("customer" = c("John", "Ahmet", "Zara"),
"longitude" = c(8.71, 8.59, 8.98),
"latitude" = c(51.2, 51.3, 51.1))
# UI
ui <- dashboardPage(
dashboardHeader(title = "CustomerLocation Dashboard"),
dashboardSidebar(
selectInput("account", label = "Account",
choices = c(unique(geocodes$customer)),
multiple = TRUE),
actionButton("go", "Go!")
),
dashboardBody(
tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
leafletOutput("mymap", height=1000)
)
)
# server
server <- function(input, output, session){
reactiveDf <- reactive({
if(is.null(input$account)){
geocodes
} else{
geocodes[geocodes$customer %in% input$account,]
}
})
output$mymap <- renderLeaflet({
leaflet(geocodes) %>%
addProviderTiles("OpenStreetMap",
group = "OpenStreetMap"
) %>%
addAwesomeMarkers(
lng = ~longitude,
lat = ~latitude,
icon = customIcon,
clusterOptions = markerClusterOptions(),
label = ~customer,
popup = ~popup
)
})
eventReactive(input$go,{
leafletProxy("mymap", data = reactiveDf()) %>%
clearShapes() %>%
clearPopups() %>%
clearMarkers() %>%
addAwesomeMarkers(
data = reactiveDf(),
lng = ~longitude,
lat = ~latitude,
icon = customIcon,
clusterOptions = markerClusterOptions(),
label = ~customer,
popup = ~popup
)
})
}
shinyApp(ui, server)