0

I would like to highlight an issue I am experiencing with a Leaflet map embedded inside a shiny app. I have to display almost 1.200 markers with a custom icon. Some of these markers are grouped in clusters (to which I have also assigned a custom icon). My zoom is frozen at a value of 10 and I use the option "preferCanvas = TRUE" for rendering. The leaflet object is not that complex: there are three different tiles that could be activated by the users and every marker has a popup that displays a specific plot stored on github/Amazon.

When I activate the markers in Safari everything is fine, but when I use Google Chrome or Mozilla Firefox the zoom and the pan become less fluid, which results in a bad navigation experience for the users.

Any idea on what could be the problem and how to solve it?

Below there's a minimal reproducible example, please download the file with the coordinates here.

library(leaflet)
library(shiny)

load("~/R_surveys.rda")

ui <- fluidPage(

  leafletOutput(outputId = "world_map",
                height = "700px")

)

server <- function(input, output, session) {

  output$world_map <- renderLeaflet({

    leaflet_object <- leaflet(data = R_surveys,
                              options = leafletOptions(zoomDelta = 1,
                                                       zoomSnap = 0,
                                                       minZoom = 3,
                                                       maxZoom = 10,
                                                       preferCanvas = TRUE)) %>%

      addMapPane(name = "Minimal_layer",
                 zIndex = 400) %>%

      addMapPane(name = "PPS_layer",
                 zIndex = 500) %>%

      addProviderTiles(
        provider = providers$Esri.WorldShadedRelief,
        group = "Minimal",
        options = c(pathOptions(pane = "Minimal_layer"),
                    providerTileOptions(updateWhenZooming = FALSE))) %>%

      addMarkers(
        lng = ~XCoord,
        lat = ~YCoord,
        group = "Surveys",
        clusterOptions = markerClusterOptions(zoomToBoundsOnClick = FALSE,
                                              removeOutsideVisibleBounds = FALSE,
                                              spiderfyDistanceMultiplier = 2,
                                              freezeAtZoom = 10)
      ) %>%

      hideGroup("Surveys") %>%

      addLayersControl(overlayGroups = c("Surveys"),
                       options = layersControlOptions(title = "Maps",
                                                      collapsed = FALSE))

  })

}

shinyApp(ui, server)

Thank you!

nd091680
  • 585
  • 4
  • 15
  • 3
    Could you provide a min reprex if possible? – Eli Berkow Sep 18 '19 at 07:55
  • 1
    Hi Eli, I've just updated the question. Even though the example is really minimal, there are still differences in rendering the markers with Safari, Mozilla and Chrome. Thanks for the help! – nd091680 Sep 18 '19 at 12:29
  • Great, I'll try take a look when I get a chance. – Eli Berkow Sep 18 '19 at 12:53
  • I cannot download your data- – Orlando Sabogal Sep 18 '19 at 16:17
  • Sorry for the issue Orlando, now it should work. Thanks! – nd091680 Sep 19 '19 at 09:54
  • The only thing I can suggest is using circle markers instead of markers which seem to load quicker otherwise see https://stackoverflow.com/questions/53038090/r-leaflet-limitations-how-many-markers-does-a-leaflet-map-support/53041771#53041771 and look into leafgl https://github.com/r-spatial/leafgl – Eli Berkow Sep 20 '19 at 11:31
  • 1
    Also from here https://community.rstudio.com/t/plotting-thousands-of-points-in-leaflet-a-way-to-improve-the-speed/8196/7 they suggest also adding `updateWhenIdle = FALSE` – Eli Berkow Sep 20 '19 at 11:38
  • Thanks a lot for your answers. I already tried this option, but I think that for the markers the only option would be to cluster more, also because the other packages to display lots of markers still don't have stable functionalities for popups and labels. – nd091680 Sep 23 '19 at 09:34

0 Answers0