R Shiny and Mapping Large Datasets??

Is there a way to improve the rendering speed / quality of mapping large datasets with a shiny app?

We were hoping to use shiny to create an app that showed the location of some our collected data, however we are finding that it may not be feasible. Our full dateset is close to 1 millions rows, and we are finding that the longer the table the harder it is to use the map. So far we have been using the leaflet package to map, and our dateset is imported in as a .RData file. Looking for advice on alternative libraries or coding practices I could imperilment to improve speed and quality.

Below is an example with some random sample data to show the issue we have been facing.

################################################################################################
################################################################################################
# Sec 1a. Needed Libaries & Input Files

# Libaries
library(shiny) # How we create the app.
library(shinycssloaders) # Adds spinner icon to loading outputs.
library(shinydashboard) # The layout used for the ui page.
library(leaflet) # Map making. Leaflet is more supported for shiny.
library(dplyr) # Used to filter data for plots.


FileIn <- data.frame(SiteID = 1:160000	, Longitude = rnorm(160000, mean=-105, sd=4),  Latitude = rnorm(160000, mean=35, sd=4))

################################################################################################
################################################################################################
# Sec 2. The UI (HTML Page)

ui <- dashboardPage(
  
  dashboardHeader(
    title ="Sample point data"
  ), #enddashboardHeader
  
  dashboardSidebar(
  ), #enddashboardSidebar
  
  dashboardBody(
    tabsetPanel(
      tabPanel("Map", fluidRow(withSpinner(leafletOutput("mapA"))))
    ) #endtabsetPanel
  ) #enddashboardBody
  
) #end dashboardPage

################################################################################################
################################################################################################
# Sec 3. The Server (function)

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

  
  #### The Map ouput.
  output$mapA <- renderLeaflet({
    leaflet(data = FileIn) %>%
      addTiles("Add Map Title Here") %>%
      addProviderTiles("Esri.WorldImagery") %>%
      addCircleMarkers(
        lng = ~Longitude,
        lat = ~Latitude,
        radius = 1)
  })
  
  
} #endServer


################################################################################################
################################################################################################
# Sec 4. Run the application.

shinyApp(ui = ui, server = server)

I have no experience with leaflet, but probably want to do something with leafletproxy to modify the markers on the fly. Dependany on Zoom show more or less detail. the locations within bounds. and do some level of aggregation at the higher level.
for example if you rounded your latitudes and longditudes in this random example to 1dp and add the distinct ones as markers it would load much faster and look pretty much the same...

FileIn<- FileIn %>% mutate_all(list(~round(.,digits = 1))) %>% select(-SiteID) %>% distinct() %>% mutate(SiteID=row_number())

I have heard positive things about {mapdeck} in use cases like yours.

It requires a Mapbox token though, and I haven't tried it personally so consider it a hearsay.

We are definitely having more success with mapdeck() with these larger files.

1 Like