r语言 - 在部署闪亮的应用程序时将传单地图下载为 png



我有一个闪亮的应用程序,用户可以在其中创建地图,然后可以下载它们。在本地,这有效,但是当我将其部署到 shinyapps.io 时,下载功能不再有效,它表明它正在下载 HTML 文档而不是 png。

我认为的一件事是,它可能与运行webshot::install_phantomjs()的需要有关,我已经在本地完成了此操作,但是将其作为应用程序本身的一行似乎不正确/可取,因为那样它会将内容下载到其他人的计算机上,需要更长的时间,并且如果每次都继续下载,效率低下。

这是我必须制作/下载地图的代码的简短示例:

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

ui<-       
fluidPage(      
leafletOutput("map_output"), 
downloadButton("map_to_download")
)
server <- function(input, output) {
reactive_map <- reactive({
leaflet()%>%
addTiles()
})
output$map_output <-     renderLeaflet(
reactive_map()
)
output$map_to_download <- downloadHandler(
filename = "map.png",
content = function(file) {
mapshot(reactive_map(), file = file, 
cliprect = "viewport", 
selfcontained = FALSE)
})
}
shinyApp(ui, server)

以上适用于我的机器(我已经运行了webshot::install_phantomjs()(,但等效部署无法正确下载地图。

有没有人知道如何在 shinyapps.io 上部署时使其工作,或者有另一种方法来下载有效的传单地图?

编辑: 我也在下载处理程序中尝试过这个:

output$map_to_download <- downloadHandler(
filename = "map.png",
content = function(file) {
saveWidget(reactive_map(), "temp.html", selfcontained = FALSE)
webshot::webshot("temp.html", file = file, cliprect = "viewport")
})

这在本地也有效,但在部署时不起作用,尽管在部署时它会下载文本而不是 HTML 文档。

我的解决方案是将webshot::install_phantomjs()添加到我的全局文件中。我最初正在考虑这一点,但担心它会做什么(如我的帖子中所述(。但是,添加它似乎不会产生我担心的不利影响,因此添加此答案作为面临相同问题的人的选择。

最新更新