Search code examples
rshinyr-leafletleaflet.markercluster

markerClusterGroup is undefined


I started to work with leaflet within R's Shiny and I am using RStudio. I want to create a map with markers for origins and destinations which could change through user input. Since i have a lot of markers to manage, i wanted to use the clustering plugin So i created a Shiny server:

server <- function(input, output, session) {
  
  reactiveData <- reactiveValues(
    origs = df.origins.total,
    dests = df.destinations.total
  )
  mydata <- reactive({
    mydf = data.frame(orig_lat=df.origins.total$lat,
                      orig_lon=df.origins.total$lon,
                      dest_lat=df.destinations.total$lat,
                      dest_lon=df.destinations.total$lon)
    print(str(mydf))
    return(mydf)
  })
  observe({
    leafletProxy("map",data=mydata()) %>%
      clearGroup("Destinations") %>%
      addMarkers(
        lng=~dest_lon,
        lat=~dest_lat,
        icon = uix.destMarker,
        group = "Destinations",
        clusterOptions = markerClusterOptions(
        )) 
      
  })
  observe({
    leafletProxy("map",data=mydata()) %>%
      clearGroup("Origins") %>%
      addMarkers(
        lng=~orig_lon,
        lat=~orig_lat,
        icon = uix.origMarker,
        group = "Origins",
        clusterOptions = markerClusterOptions(
         
        ))  
  })
  observe({
    leafletProxy("map",data=mydata()) %>%
      addLayersControl(overlayGroups = c("Origins","Destinations"))  
  })
  
  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles()%>%
  })
  
  
}

The weird thing is, that this code works sometimes and sometimes not, which means: It shows me either both, origins and destinations, or only the destinations. When only showing the destinations, and I am using Firefox's inspector, it tells me:

TypeError: _leaflet2.default.markerClusterGroup is undefined
leaflet.js:1273:7

which leads to the line

clusterGroup = _leaflet2.default.markerClusterGroup.layerSupport(clusterOptions);

I think I have installed the packages properly since they are working sometimes. And even if the error occurs, the clustering still works for the shown markers. My dataset has about 2400 markers each. Also combining all observer calls into one single observer did not change the behavior. Is my dataset to large? Do i have to use a different order?


Solution

  • The problem is/was that iconCreatFunction is not well supported by leafletProxy. In my case i unfortunately had to get rid of the leafletProxy for my clusters and only use leaflet instead, which is a big performance issue.

    Another solution could be to customize the corresponding files from the plugin: MarkerCluster.css, MarkerCluster.Default.css, leaflet.markercluster.js (which i didn't do because i could not find out how to distinguish between two types of markers that have to be clustered separately).

    Related questions that helped me and i hope it will help other developers with the same issue:

    Does markercluster work together with leafletProxy() and option iconCreateFunction?

    iconCreateFunction does NOT work when adding Markers with leafletProxy