我在Shiny应用程序中创建了一个传单地图.现在我需要一个下载按钮,以便用户可以下载当前显示的地图,包括所有标记,多边形等作为pdf文件.
我找到了这个解决方案如何在R中保存传单地图:如何将R地图中的Leaflet保存为png或jpg文件?
但它在Shiny中如何运作?我保持示例代码简单,但想到它,好像在用户想要将地图保存为pdf之前,通过leafletProxy()对地图进行了大量更改.
这是我的尝试,但它不起作用.
server.R
library(shiny)
library(leaflet)
library(devtools)
install_github("wch/webshot") # first install phantomjs.exe in your directory
library(htmlwidgets)
library(webshot)
server <- function(input, output){
  output$map <- renderLeaflet({
    leaflet() %>% addTiles()
  })
 observe({
    if(input$returnpdf == TRUE){
      m <- leafletProxy("map")
      saveWidget(m, "temp.html", selfcontained = FALSE)
      webshot("temp.html", file = "plot.pdf", cliprect = "viewport")
    }
  })
  output$pdflink <- downloadHandler(
    filename <- "map.pdf",
    content <- function(file) {
      file.copy("plot.pdf", file)
    }
  )
}
ui.R
ui <- fluidPage(
     sidebarPanel(
     checkboxInput('returnpdf', 'output pdf?', FALSE), 
     conditionalPanel(
       condition = "input.returnpdf == …每天我需要在地图上绘制路径并添加4,5或8分钟的文本.表示汽车从起点到目的地需要多长时间(见下图).我认为使用R中的Leaflet创建一个Shiny应用会很有帮助(代码如下所示).
我使用了leaflet.extras包中的addDrawToolbar来绘制路径,如附图所示.但我不知道也无法找到如何以与绘制路径相同的方式添加文本.解决方案并不一定要在R中.我的目标是为想要做这些事情并且不知道如何编码的人创建一个应用程序.
library(shiny)
library(leaflet)
library(leaflet.extras)
ui = fluidPage(
      tags$style(type = "text/css", "#map {height: calc(100vh - 20px) 
      !important;}"),
      leafletOutput("map")
      )
server = function(input,output,session){
             output$map = renderLeaflet(
                 leaflet()%>%
         addTiles(urlTemplate = "http://mt0.google.com/vt/lyrs=m&hl=en&x= 
              {x}&y={y}&z={z}&s=Ga")%>%
         addMeasure(
              primaryLengthUnit = "kilometers",
              secondaryAreaUnit = FALSE
         )%>%
         addDrawToolbar(
              targetGroup='draw',
              editOptions = editToolbarOptions(selectedPathOptions = 
                    selectedPathOptions()),
              polylineOptions = filterNULL(list(shapeOptions = 
                    drawShapeOptions(lineJoin = "round", weight = 8))),
              circleOptions = filterNULL(list(shapeOptions = 
                    drawShapeOptions(),
                    repeatMode = F,
                    showRadius = T,
                    metric = T,
                    feet = F,
                    nautic = F))) %>%
        setView(lat = 45, lng …继此问题之后,我希望将传单地图保存和下载为 png 或 jpeg 图像。我有以下代码,但我不断收到错误消息。
ui <- fluidPage(
  leafletOutput("map"),
  downloadButton("dl")
)
server <- function(input, output, session) {
  output$map <- renderLeaflet({
    leaflet() %>% 
      addTiles()
  })
  output$dl <- downloadHandler(
    filename = "map.png",
    content = function(file) {
      mapshot(input[["map"]], file = file)
    }
  )
}
shinyApp(ui = ui, server = server)
我尝试下载(通过单击按钮)时遇到的错误是
Warning: Error in system.file: 'package' must be of length 1
Stack trace (innermost first):
    65: system.file
    64: readLines
    63: paste
    62: yaml.load
    61: yaml::yaml.load_file
    60: getDependency
    59: widget_dependencies
    58: htmltools::attachDependencies …