【问题标题】:How to map large datasets with R shiny?如何使用 R Shiny 映射大型数据集?
【发布时间】:2020-04-23 15:55:53
【问题描述】:

有没有办法提高使用闪亮应用映射大型数据集的渲染速度/质量?

我们希望使用 shiny 来创建一个显示我们收集的一些数据的位置的应用程序,但是我们发现它可能不可行。我们的完整数据集接近 100 万行,我们发现表格越长,使用地图就越困难。到目前为止,我们一直在使用传单包进行映射,并且我们的日期集作为 .RData 文件导入。寻找关于替代库或编码实践的建议,我可能会危及提高速度和质量。

下面是一个示例,其中包含一些随机样本数据,以显示我们一直面临的问题。

################################################################################################
################################################################################################
# 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)

【问题讨论】:

    标签: r shiny leaflet


    【解决方案1】:

    您介意使用标记聚类吗? 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)
    

    【讨论】:

    • 这是我们考虑过的一个选项,它确实有助于减少渲染时间。但是,除非您放大它们不再聚集的点,否则您将失去识别“按颜色类型”的能力。
    猜你喜欢
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    相关资源
    最近更新 更多