2017-05-30 35 views
2

继续从this问题,我正在寻找保存并下载一张传单地图作为PNG或JPEG图像。我有下面的代码,但我不断收到错误。如何在Shiny中保存传单地图

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 
    57: toHTML 
    56: <Anonymous> 
    55: do.call 
    54: mapshot 
    53: download$func [#11] 
    4: <Anonymous> 
    3: do.call 
    2: print.shiny.appobj 
    1: <Promise> 
Error : 'package' must be of length 1 

奖励积分,如果你能告诉我如何得到这个与leafletProxy工作。

回答

2

可能,这将有助于:

server <- function(input, output, session) { 

    map <- reactiveValues(dat = 0) 

     output$map <- renderLeaflet({ 
     map$dat <- leaflet() %>% 
      addTiles() 
     }) 

     output$dl <- downloadHandler(
     filename = "map.png", 

     content = function(file) { 
      mapshot(map$dat, file = file) 
     } 
    ) 
    } 
+0

感谢您的建议,但不适合我 – nathaneastwood

+0

工作并不适合你此代码的工作? 库(mapview) m < - 传单()%>%addTiles() mapshot(m,file =“map.png”) – SBista

+0

啊 - 它正在运行,它只是需要一段时间才能运行。有趣的是,它只是下载灰色屏幕... – nathaneastwood

0

TL; DR

由于'leaflet' maps是交互式的,在mapview::mapshot()功能正在使用的传单对象必须是互动的。对此的考虑允许用户在Shiny app内保存它们的版本的小册子地图。

必要的软件包

按照教程,您需要安装从Comprehensive R Archive Network (CRAN)安装以下软件包:

  • magrittr:管道运营商%>%

  • shiny:Web应用程序框架对于R

  • leaflet:使用JavaScript '传单' 图书馆创建交互式Web地图

  • mapview:R中

代码

我的想法从下面的帖子如雨后春笋般涌现空间数据的交互式查看:

我的代码如下:

install.packages(c("magrittr", "shiny", "leaflet", "mapview")) 

library(shiny) 
library(leaflet) 
library(mapview) 
library(magrittr) 

ui <- fluidPage(
    leafletOutput(outputId = "map"), 
    downloadButton(outputId = "dl") 
) 

server <- function(input, output, session) { 

    # Create foundational leaflet map 
    # and store it as a reactive expression 
    foundational.map <- reactive({ 

    leaflet() %>% # create a leaflet map widget 

     addTiles(urlTemplate = "https://{s}.tile.openstreetmap.se/hydda/base/{z}/{x}/{y}.png") # specify provider tile and type 

    }) # end of foundational.map() 

    # render foundational leaflet map 
    output$map <- leaflet::renderLeaflet({ 

    # call reactive map 
    foundational.map() 

    }) # end of render leaflet 

    # store the current user-created version 
    # of the Leaflet map for download in 
    # a reactive expression 
    user.created.map <- reactive({ 
    # we need to specify coordinates (and zoom level) that we are currently viewing 
    bounds <- input$map_bounds 
    latRng <- range(bounds$north, bounds$south) 
    lngRng <- range(bounds$east, bounds$west) 

    # call the foundational Leaflet map 
    foundational.map() %>% 

     # store the view based on UI 
     setView(lng = (lngRng[1] + lngRng[2])/2 
       , lat = (latRng[1] + latRng[2])/2 
       , zoom = input$map_zoom 
    ) 

    }) # end of creating user.created.map() 



    # create the output file name 
    # and specify how the download button will take 
    # a screenshot - using the mapview::mapshot() function 
    # and save as a PDF 
    output$dl <- downloadHandler(
    filename = paste0(Sys.Date() 
         , "_customLeafletmap" 
         , ".pdf" 
    ) 

    , content = function(file) { 
     mapshot(x = user.created.map() 
       , file = file 
       , cliprect = "viewport" # the clipping rectangle matches the height & width from the viewing port 
       , selfcontained = FALSE # when this was not specified, the function for produced a PDF of two pages: one of the leaflet map, the other a blank page. 
    ) 
    } # end of content() function 
) # end of downloadHandler() function 

} # end of server 

# run the Shiny app 
shinyApp(ui = ui, server = server) 

最终结果

一旦运行了闪亮的应用程序,在新打开的应用程序窗口。

RStudio View

一旦在浏览器中直接点击Download。花了约3秒。

Chrome View

一旦Download已被点击,你会及时看PDF文件,无论你下载的文件存储在您的计算机上。

Final output