r - 在 Shiny 的应用程序中缓存或预渲染传单 map

标签 r shiny leaflet r-leaflet

我正在尝试使用传单映射约8000个多边形,并遇到性能问题。
当我在 Shiny 的应用程序中使用 map 时,我想知道是否有可能以某种方式缓存或预渲染 map 。
请注意,在我的情况下,我在this approach之后交换了不同的多边形层。
一个小的MWE是这样的:
可以从here下载数据

library(shiny)
library(leaflet)
library(sf)

## Download Shapefile
file <- "plz-gebiete.shp"

if (!file.exists(file)) {
  url <- "https://www.suche-postleitzahl.org/download_files/public/plz-gebiete.shp.zip"
  zipfile <- paste0(file, ".zip")
  download.file(url, zipfile)
  unzip(zipfile)
}

df <- st_read(file, options = "ENCODING=UTF-8")

# If possible: pre-render the map here!

library(shiny)

ui <- fluidPage(
  leafletOutput("mymap", width = "700px", height = "700px")
)

server <- function(input, output, session) {
  output$mymap <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>% 
      addPolygons(data = df, weight = 1, color = "black")
  })
}

shinyApp(ui, server)
在我的计算机上,用多边形渲染 map 大约需要16秒钟。
如果可能的话,我想将 map 预先渲染一次,将其另存为.rds文件,然后按需加载。请注意,我知道应用内 map 的宽度/高度(此处设置为700px)。但是类似
map <- renderLeaflet({leaflet() %>% ...})
saveRDS(map, "renderedmap.rds")

map <- readRDS("renderedmap.rds")

# within server()
output$mymap <- map
不会导致任何性能提升。
另外,我尝试异步加载传单,以便可以渲染/交互应用程序的其他部分,但无济于事。
有什么想法可以解决或解决这个问题吗?

最佳答案

以下两种方法不能完全回答您的问题,但是与leaflet::addPolygons相比,它们绝对是性能更高的替代方案。
使用Flatgeobuf格式:
根据leafem::addFgb的描述:

Flatgeobuf can stream the data chunk by chunk so that rendering of the map is more or less instantaneous. The map is responsive while data is still loading so that popup queries, zooming and panning will work even though not all data has been rendered yet.


我认为数据集是线串,这就是为什么fillColor似乎被忽略的原因。
library(leaflet)
library(leafem)
library(shiny)

# via URL (data around 13mb)
url = "https://raw.githubusercontent.com/bjornharrtell/flatgeobuf/3.0.1/test/data/UScounties.fgb"

ui <- fluidPage(
  leafletOutput("mymap", width = "700px", height = "700px")
)

server <- function(input, output, session) {
  output$mymap <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      leafem:::addFgb(
        url = url, group = "counties",
        label = "NAME", popup = TRUE,
        fillColor = "blue", fillOpacity = 0.6,
        color = "black", weight = 1) %>%
      addLayersControl(overlayGroups = c("counties")) %>%
      setView(lng = -105.644, lat = 51.618, zoom = 3)
  })
}

shinyApp(ui, server)

使用leafgl(WebGL-Renderer):
library(sf)
library(shiny)
library(leaflet)
library(leafgl)

plz <- st_read("C:/Users/user/Downloads/plz-gebiete.shp", layer = "plz-gebiete")

ui <- fluidPage(
  leafletOutput("mymap", width = "700px", height = "700px")
)

server <- function(input, output, session) {
  output$mymap <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      addGlPolygons(data = plz, color = ~plz, popup = "note", group = "plz") %>% 
      addLayersControl(overlayGroups = "plz")
  })
}

shinyApp(ui, server)

关于r - 在 Shiny 的应用程序中缓存或预渲染传单 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62662487/

相关文章:

R/Shiny 图不显示在浏览器中

r Shiny 的表不呈现html

r - 如何在 Shiny R 中动态更改传单 map 的大小?

r - 使用 R 将表写入文件会导致不需要的行名称列

r - 寻找曲线以匹配数据

r - 在 Wiki 的网球 table 上使用 Rvest 进行网页抓取

r - 在 'window' 中使用 apply

colors - Shiny 的checkboxgroupinput动态背景颜色控制

html - 传单 - map "shadowed",当大小设置为百分比时不可点击

r - R Shiny 中的传单全屏切换/按钮