I am working on a Shiny app in R that creates a leaflet map and plots points.
Plotting the points by themselves works fine. The issue arises when I try to implement even the most basic clusters.
I can plot individual points with no problem. As soon as I try to implement clusterOptions=markerClusterOptions(), app runs and displays a map with no markers or clusters.
I created simplified data to troubleshoot. This code below creates a data frame with 9 points. They're intentionally created to display as three sets of three points. They display correctly.
test_data <- data.frame(
longitude = c(-90, -91, -90.5, -90.01, -91.01, -90.51, -90.02, -91.02, -90.52),
latitude = c(45, 45.5, 45.25, 45.01, 45.51, 45.26, 45.02, 45.52, 45.27)
)
leaflet(data = test_data) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircleMarkers(
lng = ~longitude, lat = ~latitude,
radius = 5 # Fixed radius for testing
)
I then try to add in basic clustering, which should give me 3 clusters on the same map. App runs, map displays the same area, but no points or clusters.
test_data <- data.frame(
longitude = c(-90, -91, -90.5, -90.01, -91.01, -90.51, -90.02, -91.02, -90.52),
latitude = c(45, 45.5, 45.25, 45.01, 45.51, 45.26, 45.02, 45.52, 45.27)
)
leaflet(data = test_data) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircleMarkers(
lng = ~longitude, lat = ~latitude,
radius = 5, # Fixed radius for testing
clusterOptions=markerClusterOptions() # This should create 3 default clusters.
)
I've updated leaflet, RStudio, etc. No change, no errors being thrown in RStudio.
Update:
I've confirmed that the issue is not my underlying Leaflet package. This barebones code correctly displays some basic clusters of points on a U.S. map.
library(leaflet)
library(leaflet.extras)
# Create a sample data frame with 9 points in the U.S.
data <- data.frame(
latitude = c(39.8283, 34.0522, 41.8781, 29.7604, 32.7157, 33.4484, 36.7783, 30.2672, 40.7128),
longitude = c(-98.5795, -118.2437, -87.6298, -95.3698, -117.1611, -112.0740, -119.4179, -97.7431, -74.0060),
group = c("Group A", "Group B", "Group A", "Group C", "Group B", "Group A", "Group C", "Group B", "Group A")
)
# Create the Leaflet map with basic marker clusters
leaflet(data) %>%
addTiles() %>%
addCircleMarkers(
lng = ~longitude, lat = ~latitude,
clusterOptions = markerClusterOptions(),
color = ~group,
radius = 8
)```
Found the issue.
I was setting a custom leaflet version, which was older than the default I have on my machine. Once I removed the custom leaflet map displayed with clusters.