問題描述
這個問題是問題如何保存的后續問題Shiny 中的傳單地圖,以及在 Shiny 中保存傳單地圖.
This question is a follow-up to the questions How to save a leaflet map in Shiny, and Save leaflet map in Shiny.
我添加了一個工具欄來在地圖上繪制形狀/點,它是leaflet.extras 包中的addDrawToolbar
.這讓用戶可以交互式地繪制線條、形狀…….最后,我希望能夠將帶有繪制形狀的地圖保存為 pdf 或 png.
I add a toolbar to draw shapes/points on the map that is addDrawToolbar
in the leaflet.extras package. That lets users to draw lines, shapes, ... interactively. In the end I want one to be able to save the map with the drawn shapes as a pdf or png.
我利用問題的答案編寫了以下代碼:如何在 Shiny 中保存傳單地圖.但這無助于實現我的目標.
I have coded up the following making use of the answer to the question: How to save a leaflet map in Shiny. But it does not help achieve my goal.
有沒有人可以幫助我?
library(shiny)
library(leaflet)
library(leaflet.extras)
library(mapview)
ui <- fluidPage(
leafletOutput("map"),
br(),
downloadButton("download_pdf", "Download .pdf")
)
server <- function(input, output, session) {
foundational_map <- reactive({
leaflet() %>%
addTiles()%>%
addMeasure(
primaryLengthUnit = "kilometers",
secondaryAreaUnit = FALSE
)%>%
addDrawToolbar(
targetGroup='draw',
editOptions = editToolbarOptions(selectedPathOptions =
selectedPathOptions()),
polylineOptions = filterNULL(list(shapeOptions =
drawShapeOptions(lineJoin = "round",
weight = 3))),
circleOptions = filterNULL(list(shapeOptions =
drawShapeOptions(),
repeatMode = F,
showRadius = T,
metric = T,
feet = F,
nautic = F))) %>%
setView(lat = 45, lng = 9, zoom = 3) %>%
addStyleEditor(position = "bottomleft",
openOnLeafletDraw = TRUE)
})
output$map <- renderLeaflet({
foundational_map()
})
user_created_map <- reactive({
foundational_map() %>%
setView(lng = input$map_center$lng, lat = input$map_center$lat,
zoom = input$map_zoom)
})
output$download_pdf <- downloadHandler(
filename = paste0("map_", Sys.time(), ".pdf"),
content = function(file) {
mapshot(user_created_map(), file = file)
}
)
}
shinyApp(ui = ui, server = server)
推薦答案
顯然 mapshot
函數不知道繪制的多邊形,只存儲干凈的傳單地圖,因為它啟動了一個隔離的后臺進程捕獲網絡快照.
Apparently the mapshot
function is not aware of drawn polygons and just stores the clean leaflet-map, as it launches an isolated background process which captures the webshot.
我會提出這個解決方法,它捕獲整個屏幕(使用這個 batch-file) 并將其保存為 png.(僅適用于 Windows)
I would propose this workaround, which captures the whole screen (using this batch-file) and saves it as png. (only for Windows)
這不是很漂亮,因為它還會捕獲窗口和瀏覽器菜單欄,盡管可以在批處理文件中進行調整.
This is not very beautiful as it will also capture the windows and browser menu bars, although that could be adapted in the batch-file.
批處理文件必須在同一目錄中,并且必須命名為 screenCapture.bat.
The batch-file must be in the same directory and must be named screenCapture.bat .
library(shiny)
library(leaflet)
library(leaflet.extras)
library(mapview)
ui <- fluidPage(
leafletOutput("map"),
actionButton("download_pdf", "Download .pdf")
)
server <- function(input, output, session) {
foundational_map <- reactive({
leaflet() %>%
addTiles()%>%
addMeasure(
primaryLengthUnit = "kilometers",
secondaryAreaUnit = FALSE
)%>%
addDrawToolbar(
targetGroup='draw',
editOptions = editToolbarOptions(selectedPathOptions =
selectedPathOptions()),
polylineOptions = filterNULL(list(shapeOptions =
drawShapeOptions(lineJoin = "round",
weight = 3))),
circleOptions = filterNULL(list(shapeOptions =
drawShapeOptions(),
repeatMode = F,
showRadius = T,
metric = T,
feet = F,
nautic = F))) %>%
setView(lat = 45, lng = 9, zoom = 3) %>%
addStyleEditor(position = "bottomleft",
openOnLeafletDraw = TRUE)
})
output$map <- renderLeaflet({
foundational_map()
})
user_created_map <- reactive({
foundational_map()
})
## observeEvent which makes a call to the Batch-file and saves the image as .png
observeEvent(input$download_pdf, {
img = paste0("screen", runif(1,0,1000), ".png")
str = paste('call screenCapture ', img)
shell(str)
})
}
shinyApp(ui = ui, server = server)
為了刪除瀏覽器和 Windows 工具欄,我像這樣操作 .bat 文件:
To remove the browser and Windows toolbar, I manipulated the .bat-file like this:
第 66 行:
int height = windowRect.bottom - windowRect.top - 37;
第 75 行:
GDI32.BitBlt(hdcDest, 0, -80, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
這適用于我的機器,但您必須調整這些值,甚至想出更好的解決方案,因為我不得不承認我不太擅長批處理腳本.這將隱藏工具欄,但底部會有一個黑色條帶.
This works on my machine, but you will have to adapt the values or even come up with a better solution, since I have to admit that I'm not too good at batch scripting. This will hide the toolbars, but there will be a black strip at the bottom.
這篇關于如何在 Shiny 中保存帶有繪制形狀/點的傳單地圖?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!