如何在Shiny

时间:2017-05-30 10:27:53

标签: r shiny leaflet r-leaflet

this问题后,我希望保存并下载传单地图作为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
    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 个答案:

答案 0 :(得分:3)

可能会有所帮助:

  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)
        }
      )
    }

答案 1 :(得分:3)

概述

由于'leaflet' maps是交互式的,因此mapview::mapshot()函数中使用的传单对象必须是交互式的。考虑到这一点,用户可以在Shiny app内保存他们的版本的传单地图。

# install necessary packages
install.packages( c( "shiny", "leaflet", "mapview" ) )

# load necessary packages
library( shiny )
library( leaflet )
library( mapview )

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({

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

      # store the view based on UI
      setView( lng = input$map_center$lng
               ,  lat = input$map_center$lat
               , 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)

# end of script #

最终结果

运行Shiny应用程序后,在新窗口中打开应用程序。

RStudio View

进入浏览器后,请点击Download。花了大约3秒钟。

Chrome View

点击Download后,只要下载的文件存储在您的计算机上,您就会立即看到PDF文件。

Final output

参考

我的想法来自以下帖子: