gpt4 book ai didi

r - 在 R 中保存传单 map

转载 作者:行者123 更新时间:2023-12-01 11:27:00 25 4
gpt4 key购买 nike

我创建了一个应用程序,用户可以在其中修改传单 map ,我想在 pdf 报告中使用此 map 。我有
1. 安装包传单、webshot 和 htmlwidget
2.安装PhantomJS

下面是代码的简化版本

服务器.R:

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

shinyServer(function(input, output, session) {

output$amap <- renderLeaflet({
leaflet() %>%
addProviderTiles("Stamen.Toner",
options = providerTileOptions(noWrap = TRUE, reuseTiles=TRUE))
})

observe({
leafletProxy("amap") %>%
clearShapes() %>%
addCircles(lng = c(22,-2), lat = c(42,65))
})



observeEvent(input$saveButton,{
themap<- leafletProxy("amap")
saveWidget(themap, file="temp.html", selfcontained = F)
webshot("temp.html", file = "Rplot.png",
cliprect = "viewport")

})
})

用户界面:
fluidPage(
leafletOutput("amap", height="600px", width="600px"),
br(),
actionButton("saveButton", "Save")
)

我收到此错误消息:

警告:system.file 中的错误:“package”的长度必须为 1
堆栈跟踪(最里面在先):
73:系统文件
72:读行
71:粘贴
70:yaml.load
69:yaml::yaml.load_file
68:获取依赖
67:widget_dependencies
66: htmltools::attachDependencies
65:到HTML
64:保存小部件
63:observeEventHandler [C:\R files\test/server.R#24]
1: Shiny ::运行应用程序

当保存按钮被激活时。
当我像这样定义保存按钮时,savewidget 工作正常:
  observeEvent(input$saveButton,{
themap<-leaflet() %>%
addProviderTiles("Stamen.Toner",
options = providerTileOptions(noWrap = TRUE, reuseTiles=TRUE))

saveWidget(themap, file="temp.html", selfcontained = F)
webshot("temp.html", file = "Rplot.png",
cliprect = "viewport")

})

但我真的想要用户在 webshot 中所做的更改。任何人都可以帮忙吗?

最佳答案

这并不完美,但这里有一个使用库 html2canvas 的解决方案。请注意归属、许可和版权。此外,这个 不会 在 RStudio 查看器中工作,但有办法让它工作。

library(leaflet)
library(htmltools)

lf <- leaflet() %>%
addProviderTiles(
"Stamen.Toner",
options = providerTileOptions(
noWrap = TRUE,
reuseTiles=TRUE
)
)


# add the mapbox leaflet-image library
# https://github.com/mapbox/leaflet-image
#lf$dependencies[[length(lf$dependencies)+1]] <- htmlDependency(
# name = "leaflet-image",
# version = "0.0.4",
# src = c(href = "http://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-image/v0.0.4/"),
# script = "leaflet-image.js"
#)

lf$dependencies[[length(lf$dependencies)+1]] <- htmlDependency(
name = "html2canvas",
version = "0.5.0",
src = c(href="https://cdn.rawgit.com/niklasvh/html2canvas/master/dist/"),
script = "html2canvas.min.js"
)



browsable(
tagList(
tags$button("snapshot",id="snap"),
lf,
tags$script(
'
document.getElementById("snap").addEventListener("click", function() {
var lf = document.querySelectorAll(".leaflet");
html2canvas(lf, {
useCORS: true,
onrendered: function(canvas) {
var url = canvas.toDataURL("image/png");
var downloadLink = document.createElement("a");
downloadLink.href = url;
downloadLink.download = "map.png"

document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
});
});
'
)
)
)

关于r - 在 R 中保存传单 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36495481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com