I have a map in my shiny App.
I try to filters the markers on my map by a column of my dataframe.
My Data:
remorque time.stamp lat long geolocalisation maintenance temperature appairage
1 21/11/2017 10:36 48.86272 2.2875920 OnMouv noir
2 21/11/2017 10:36 43.60776 1.4421606 StartMouv rouge
3 21/11/2017 10:36 46.58619 0.3388710 OnMouv rouge
4 21/11/2017 10:36 45.76695 3.0556216 Life orange
5 21/11/2017 10:36 45.14555 1.4751652 EndMouv rouge
6 21/11/2017 10:36 46.81157 1.6936336 Life orange
7 21/11/2017 10:36 47.36223 0.6751146 alerte rouge
8 21/11/2017 10:36 47.36032 1.7441244 StartMouv
9 21/11/2017 10:36 48.85333 1.8215332 StartMouv
10 21/11/2017 10:36 48.84429 1.7913208 alerte
11 21/11/2017 10:36 48.81356 1.6759643 EndMouv
In ui.R I add :
selectInput("geolocalisation", "Géolocalisation :",
choice = list("Tous" = "tous",
"OnMouv" = "OnMouv",
"StartMouv" = "StartMouv",
"EndMouv" = "EndMouv",
"Life" = "Life",
"Perte ou Vol" = "alerte"))
And in server.R I did:
output$map <- renderLeaflet({
# Use leaflet() here, and only include aspects of the map that
# won't need to change dynamically (at least, not unless the
# entire map is being torn down and recreated).
leaflet() %>%
addTiles()
})
zerg <-reactive({
test<-ifelse(input$geolocalisation=="OnMouv", return(data_moment[data_moment$geolocalisation=="OnMouv",]),
ifelse(input$geolocalisation=="StartMouv", return(data_moment[data_moment$geolocalisation=="StartMouv",]),
ifelse(input$geolocalisation=="EndMouv", return(data_moment[data_moment$geolocalisation=="EndMouv",]),
ifelse(input$geolocalisation=="Life", return(data_moment[data_moment$geolocalisation=="Life",]),
ifelse(input$geolocalisation=="alerte", return(data_moment[data_moment$geolocalisation=="alerte",]),
return(data_moment))))))
return(test)
})
observe({
dataset<- zerg()
leafletProxy("map", data = dataset) %>%
clearMarkers() %>%
addMarkers(~long, ~lat,clusterOptions = markerClusterOptions(), icon = load_icons(dataset),
popup= ~paste("<h4><font color='#2B547E'><b>Remorque : </font></b>",remorque,"</h4><h4><font color='#2B547E'>Latitude : </font>",lat,"</h4><h4><font color='#2B547E'>Longitude : </font>",long,"</h4>"),
label = ~paste("Remorque : ",remorque,"::: Latitude : ",lat,"::: Longitude : ",long))
})
When I launch the app, All the markers are displayed (that's good). And when I make a selection, it adds markers, not selecting markers. Just adding new ones which are duplicates.
Do you know why?
So the secret sauce was to use clearClusterMarkers()
instead of clearMarkers()
since you adjust the markers to cluster markers when you define the argument clusterOptions = markerClusterOptions
.
Here is the long form of a working single file app.R
library(shiny)
library(leaflet)
data_moment <- read.table(
text =
"remorque time.stamp lat long geolocalisation
1 21/11/2017 10:36 48.86272 2.2875920 OnMouv
2 21/11/2017 10:36 43.60776 1.4421606 StartMouv
3 21/11/2017 10:36 46.58619 0.3388710 OnMouv
4 21/11/2017 10:36 45.76695 3.0556216 Life
5 21/11/2017 10:36 45.14555 1.4751652 EndMouv
6 21/11/2017 10:36 46.81157 1.6936336 Life
7 21/11/2017 10:36 47.36223 0.6751146 alerte
8 21/11/2017 10:36 47.36032 1.7441244 StartMouv
9 21/11/2017 10:36 48.85333 1.8215332 StartMouv
10 21/11/2017 10:36 48.84429 1.7913208 alerte
11 21/11/2017 10:36 48.81356 1.6759643 EndMouv", header = TRUE)
ui <- fluidPage(
titlePanel("A map"),
sidebarLayout(
sidebarPanel(
selectInput("geolocalisation", "Géolocalisation :",
choice = list("Tous" = "tous",
"OnMouv" = "OnMouv",
"StartMouv" = "StartMouv",
"EndMouv" = "EndMouv",
"Life" = "Life",
"Perte ou Vol" = "alerte"))
),
mainPanel(
leafletOutput("map")
)
)
)
server <- function(input, output) {
output$map <- renderLeaflet({
# Use leaflet() here, and only include aspects of the map that
# won't need to change dynamically (at least, not unless the
# entire map is being torn down and recreated).
leaflet() %>%
addTiles() %>%
# I added this so we don't have to zoom in from outer space each time
setView(lng = 2.3522, lat = 48.8566, zoom = 5)
})
zerg <-reactive({
test<-ifelse(input$geolocalisation=="OnMouv", return(data_moment[data_moment$geolocalisation=="OnMouv",]),
ifelse(input$geolocalisation=="StartMouv", return(data_moment[data_moment$geolocalisation=="StartMouv",]),
ifelse(input$geolocalisation=="EndMouv", return(data_moment[data_moment$geolocalisation=="EndMouv",]),
ifelse(input$geolocalisation=="Life", return(data_moment[data_moment$geolocalisation=="Life",]),
ifelse(input$geolocalisation=="alerte", return(data_moment[data_moment$geolocalisation=="alerte",]),
return(data_moment))))))
return(test)
})
observe({
data_set <- zerg
leafletProxy("map", data = zerg()) %>%
# this is the only change you really need
clearMarkerClusters() %>%
addMarkers(~long, ~lat, clusterOptions = markerClusterOptions(),
popup= ~paste("<h4><font color='#2B547E'><b>Remorque : </font></b>",remorque,"</h4><h4><font color='#2B547E'>Latitude : </font>",lat,"</h4><h4><font color='#2B547E'>Longitude : </font>",long,"</h4>"),
label = ~paste("Remorque : ",remorque,"::: Latitude : ",lat,"::: Longitude : ",long))
})
}
shinyApp(ui = ui, server = server)