Very slow render for leaflet

Hi, goodnight. I have a problem when I render a leaflet polygon map, because is very slow to load it.
Is it possible to make it work faster somehow?

The data I use is the following: datacovid - Google Drive

And the code is:

library(readxl)
library(leaflet)
library(shiny)
library(echarts4r)
library(dplyr)
library(spdplyr)
library(lubridate)
library(tmap)
library(lubridate)
data("World")
datoscovid <- read_excel("/Users/jorge_hca/Desktop/Trabajo/datacovid.xlsx")


ui <- fluidPage(
  
  dateInput("fecha", "Fecha a graficar:", value = "2021-02-12"),
  leafletOutput("map")  
  
)



server <- function(input, output){
  
  datos <- reactive({
    World |> 
      filter(continent == "North America" | continent == "South America") |> 
      select(name,  geometry) |> 
      left_join(datoscovid, by = c("name" = "location")) |> 
      filter(date == input$fecha)
    
  })
  
  output$map <- renderLeaflet({
    
    mypalette <- colorBin(palette="RdGy", domain=datos()$total_cases, na.color="transparent", bins = 4)
    
    leaflet() %>%
      addTiles("http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png",
               attribution = paste(
                 "&copy; <a href=\"http://openstreetmap.org\">OpenStreetMap</a> contributors",
                 "&copy; <a href=\"http://cartodb.com/attributions\">CartoDB</a>"
               ))  %>%
      setView( lat=23, lng=-100 , zoom=3) %>%
      addPolygons(
        data = datos(),
        fillColor = ~mypalette(total_cases),
        stroke=TRUE,
        fillOpacity = 0.85,
        color="white",
        weight=0.3,
        label = mytext,
        labelOptions = labelOptions(
          style = list("font-weight" = "normal", padding = "3px 8px"),
          textsize = "13px",
          direction = "auto"
        )
      )  %>%
      addLegend(pal = mypalette, values = ~total_cases, opacity = 0.9, title = "Ingresos en México", position = "bottomleft", data = datos())
    
  })
  
  
}

shinyApp(ui, server)

Thanks for reading me

Does your data only contain information from North and South America? You could create a filtered file and just load that instead of loading all of World. You could simplify the polygon using rmapshaper::ms_simplify().

This is actually quite speedy for me. How fast did you want to make this? Also, perhaps you can checkout leafletProxy for updating the map. That way your map doesn't redraw every input change

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.