从Shiny.io上托管的闪亮应用程序下载传单地图

时间:2018-09-06 19:06:07

标签: r shiny leaflet

我有一个闪亮的应用程序,可以显示传单包中的地图。 我有兴趣从闪亮的应用程序中将地图另存为PNG文件或HTML文件。在本地运行并使用以下代码在浏览器中打开闪亮的应用程序时,我设法将其保存为PNG文件:

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


ui <- fluidPage(
      leafletOutput(outputId = "eiffelmap")
       , downloadButton(outputId = "savemap")
       )

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




   name <- 'EIFFEL TOWER'
   longitude <- 2.2945
   latitude <- 48.8583
   location <- data.frame(name,longitude,latitude)

   icon.pop <- awesomeIcons(icon = 'thumb-tack',
                         markerColor = 'blue',
                         library = 'fa',
                         iconColor = 'black')
  map <- reactive({  
  name <- 'EIFFEL TOWER'
  longitude <- 2.2945
  latitude <- 48.8583
  location <- data.frame(name,longitude,latitude)

  icon.pop <- awesomeIcons(icon = 'thumb-tack',
                           markerColor = 'blue',
                           library = 'fa',
                           iconColor = 'black')

  leaflet(location) %>%
  addProviderTiles(providers$Stamen.TonerLite,
                   options = providerTileOptions(noWrap = TRUE)
  ) %>%

  addAwesomeMarkers(~longitude, ~latitude, label = ~name, icon=icon.pop) 

})

output$eiffelmap <- renderLeaflet({

          map()

       })

output$savemap <- downloadHandler(
    filename = "eiffelmap.png",
    content = function(file){


     mapshot(
      x = map()
      , file = file
    )
  }
)

}

shinyApp(ui, server)

我的问题是,当应用程序托管在 shiny.io 上时,为什么下载不起作用,而在浏览器上本地打开时却起作用?我的代码有什么问题? 还有关于如何将传单地图另存为可以放大和缩小的交互式HTML的见解吗? 任何见解或帮助都将受到高度欢迎和赞赏。

1 个答案:

答案 0 :(得分:1)

我不确定您是独立询问还是作为应用程序的一部分询问,但是如果要将地图另存为交互式HTML,只需运行这部分代码,然后在“查看器”窗格中选择“导出”选项,然后选择“保存”即可。作为网页...

library(leaflet)
name <- 'EIFFEL TOWER'
longitude <- 2.2945
latitude <- 48.8583
location <- data.frame(name,longitude,latitude)

icon.pop <- awesomeIcons(icon = 'thumb-tack',
                         markerColor = 'blue',
                         library = 'fa',
                         iconColor = 'black')

leaflet(location) %>%
  addProviderTiles(providers$Stamen.TonerLite,
                   options = providerTileOptions(noWrap = TRUE)
  ) %>%

  addAwesomeMarkers(~longitude, ~latitude, label = ~name, icon=icon.pop) 

enter image description here

更新

请尝试此操作(使用htmlwidgets::saveWidget()

library(shiny)
library(leaflet)
library(htmlwidgets)


ui <- fluidPage(
  leafletOutput(outputId = "eiffelmap")
  , downloadButton(outputId = "savemap")
)

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




  name <- 'EIFFEL TOWER'
  longitude <- 2.2945
  latitude <- 48.8583
  location <- data.frame(name,longitude,latitude)

  icon.pop <- awesomeIcons(icon = 'thumb-tack',
                           markerColor = 'blue',
                           library = 'fa',
                           iconColor = 'black')
  map <- reactive({  
    name <- 'EIFFEL TOWER'
    longitude <- 2.2945
    latitude <- 48.8583
    location <- data.frame(name,longitude,latitude)

    icon.pop <- awesomeIcons(icon = 'thumb-tack',
                             markerColor = 'blue',
                             library = 'fa',
                             iconColor = 'black')

    leaflet(location) %>%
      addProviderTiles(providers$Stamen.TonerLite,
                       options = providerTileOptions(noWrap = TRUE)
      ) %>%

      addAwesomeMarkers(~longitude, ~latitude, label = ~name, icon=icon.pop) 

  })

  output$eiffelmap <- renderLeaflet({

    map()

  })

  output$savemap <- downloadHandler(
    filename = "eiffelmap.html",
    content = function(file){


      saveWidget(
        widget = map()
        , file = file
      )
    }
  )

}

shinyApp(ui, server)