0

I am trying to get this set of markers from the mrt dataset to appear at a certain zoom level. I'm new to leaflet in r and would like some advice on this. I tried using an if statement that should have the markers appear at a certain zoom level. But it doe not work.

Here's an example of the mrt data

 stn_code   mrt_station      lat      lon
1      NS1   Jurong East 1.333131 103.7421
2      NS2   Bukit Batok 1.349064 103.7496
3      NS3  Bukit Gombak 1.359037 103.7518
4      NS4 Choa Chu Kang 1.385385 103.7443
5      NS5       Yew Tee 1.397329 103.7475
6      NS7        Kranji 1.425227 103.7620

Here's the portion of code that renders the leaflet map.

 output$mymap <- renderLeaflet({
        leaflet(data=df()) %>% 
            addTiles() %>% 
            addPolygons(data=adm, weight = 3, fillColor = "white", popup=popup)%>%
            addMarkers(clusterOptions = markerClusterOptions(),
                       label = paste(df()$address,',',df()$town))%>%
            if (input$map_zoom>6){
                addMarkers(data=mrt,lat = ~lat,lng = ~lon,label=mrt$mrt_station,icon = mrticon)

            }

    })

Thanks!

Elliot Tan
  • 149
  • 4
  • 11
  • please add some data using `dput()` https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – TobiO Jul 17 '19 at 18:02
  • Your code looks actually as it could work but its not reproducible. this should help: this should help. https://stackoverflow.com/questions/56817268/shiny-leaflet-display-labels-based-on-zoom-level/56820425#56820425 just adapt it for markers. – Tonio Liebrand Jul 17 '19 at 20:27

1 Answers1

1

Just from a high level review of your code, here's what you probably need or at least set you in the right direction. For any more help you must provide a minimum working example app. -

output$mymap <- renderLeaflet({
  leaflet(data=df()) %>% 
    addTiles() %>% 
    addPolygons(data=adm, weight = 3, fillColor = "white", popup=popup)%>%
    addMarkers(clusterOptions = markerClusterOptions(),
               label = paste(df()$address,',',df()$town))
})

observe({
    if(input$mymap_zoom > 6) {
      leafletProxy("mymap", data = mrt) %>% 
        addMarkers(
          lat = ~lat,
          lng = ~lon,
          label=mrt$mrt_station,
          icon = mrticon,
          layerID = "some_markers"
        )
    } else {
      leafletProxy("mymap") %>% 
        clearMarkers("some_markers")
    }
})
Shree
  • 10,835
  • 1
  • 14
  • 36