简体   繁体   中英

How to map large datasets with R shiny?

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)

Do you mind using marker clustering? clusterOptions = markerClusterOptions()

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,
        clusterOptions = markerClusterOptions())
  })


} #endServer


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

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM