【发布时间】:2022-11-02 19:21:38
【问题描述】:
我目前正在开发一个 ShinyApp,其中包括使用 sf 对象和传单构建的地图。
我找到了将地图的初始灰色背景更改为白色背景(通过添加 css 样式)并将地图保存为 png(通过使用 webshot 或 mapshot)的解决方案。
然而,当我保存传单地图时,灰色背景又回来了!而且我没有找到用白色背景保存地图的解决方案。
这是说明我的问题的可重现代码:
# Libraries
library(shiny)
library(shinyWidgets)
library(htmlwidgets)
library(tidyr)
library(sf)
library(leaflet)
library(webshot)
library(mapview)
# Spatial data
d = matrix(c(0,0,10,0,10,10,0,10,0,0), ncol = 2, byrow = TRUE) %>%
list(.) %>% st_polygon(.) %>% st_sfc(.)
# Shiny app
shinyApp(
ui = shinyUI(fluidPage(tags$head(tags$style(HTML("#map{ background: none; }"))),
leafletOutput("map"),
downloadBttn("save", label = ".png"))),
server = function(input, output){
# Map creation
map = reactiveVal(NULL)
map(leaflet(d, options = leafletOptions()) %>% addPolygons())
# Map render
output$map = renderLeaflet(map())
# Map download
output$save = downloadHandler(
file = function(){return("Rplot.png")},
content = function(file){
# Apply the changes user made (move and zoom)
coords = input$map_center
zoom = input$map_zoom
shot = map() %>% setView(lng = coords$lng, lat = coords$lat, zoom = zoom)
# Save the map unsing webshot (I prefer using this method for "heavy" map, like > 10 000 polygons)
saveWidget(shot, "temp.html", selfcontained = FALSE)
suppressWarnings({shot = webshot("temp.html", file = file, cliprect = "viewport")})
# Save using mapshot
# suppressWarnings({shot = mapshot(shot, file = file, cliprect = "viewport")})
shot
}
)
}
)
你有什么想法吗?
我尝试了不同的 css 背景样式(“none”、“white”、“#fff”)和不同的方式来保存传单地图(mapshot、webshot),但对我没有任何效果。
【问题讨论】:
标签: r shiny r-leaflet r-mapview