Search code examples
rshinyleafletr-leaflet

R Shiny Leaflet Show number of overlapping markers / polygons?


I'm working with a large set of location data, and it turns out a lot of my locations share longitude and latitude values. Is there a way to show the number of markers / polygons that overlap, either through a popup or some other widget?

I can not remove sites that share longitude and latitude values from my dataset.

#############################################
# Needed Libraries & Input Files

library(shiny)
library(shinydashboard)
library(leaflet)

## The Data
Point_ID = c("A1", "B1", "C1")
Latitude = c(38.00, 38.00, 38.00)
Longitude = c(-107.00, -107.00, -107.00)
Map_DF <- data.frame(Point_ID, Latitude, Longitude)

choiseList <- c("A1", "B1", "C1")

#############################################
# UI
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(checkboxGroupInput(inputId = "IDPointInput", label = "Select Point ID", choices = choiseList, selected = choiseList)),
  dashboardBody(fluidRow(leafletOutput(outputId = 'mapA')))
)

#############################################
# SERVER
server <- function(input, output, session) {
  
  ## The Filter
  filter_df <- reactive({
    Map_DF[sapply(Map_DF$Point_ID, function(p) {any(input$IDPointInput %in% p)}), ]
  })
  
  ## Base Map Creation
  output$mapA <- renderLeaflet({
    leaflet() %>%
      addProviderTiles(
        providers$Esri.DeLorme,
        options = providerTileOptions(
          updateWhenZooming = FALSE,
          updateWhenIdle = TRUE)
      ) %>%
      setView(lng = -107.50, lat = 39.00, zoom = 7)
  })
  
  ## Update Map with Filter Selection
  observe({
    leafletProxy("mapA", session) %>%
      clearMarkers() %>%
      addCircleMarkers(
        data = filter_df(),
        radius = 10,
        color = "red",
        lat = ~Latitude,
        lng = ~Longitude,
        popupOptions(autoPan = FALSE),
        popup = ~paste("PointID: ", filter_df()$Point_ID))
        # Show number of sites that overlap oneanother
  })
}

############################################
shinyApp(ui = ui, server = server)

Solution

  • Does addAwesomeMarkers work for you?

    library(shiny)
    library(shinydashboard)
    library(leaflet)
    
    ## The Data
    Point_ID = c("A1", "B1", "C1")
    Latitude = c(38.00, 38.00, 38.00)
    Longitude = c(-107.00, -107.00, -107.00)
    Map_DF <- data.frame(Point_ID, Latitude, Longitude)
    
    choiseList <- c("A1", "B1", "C1")
    
    #############################################
    # UI
    ui <- dashboardPage(
      dashboardHeader(),
      dashboardSidebar(checkboxGroupInput(inputId = "IDPointInput", label = "Select Point ID", choices = choiseList, selected = choiseList)),
      dashboardBody(fluidRow(leafletOutput(outputId = 'mapA')))
    )
    
    #############################################
    # SERVER
    server <- function(input, output, session) {
      
      ## The Filter
      filter_df <- reactive({
        Map_DF[sapply(Map_DF$Point_ID, function(p) {any(input$IDPointInput %in% p)}), ]
      })
      
      ## Base Map Creation
      output$mapA <- renderLeaflet({
        leaflet(filter_df()) %>% 
    
          addProviderTiles(
            providers$Esri.DeLorme,
            options = providerTileOptions(
              updateWhenZooming = FALSE,
              updateWhenIdle = TRUE)
          )  %>%
          
          setView(lng = -107.50, lat = 39.00, zoom = 7) %>% 
          
         # use addAwesomeMarkers #
          addAwesomeMarkers(
            lat = ~Latitude,
            lng = ~Longitude,
            icon=~makeAwesomeIcon(
              icon = 'ios-close',
              iconColor = 'black',
              library = 'ion',
              markerColor = 'orange'),
            
            label=~as.character(Point_ID),
            
            popup = ~paste("PointID: ", Point_ID),
            
            clusterOptions = markerClusterOptions()
          )
      })
    }
    
    ############################################
    shinyApp(ui = ui, server = server)