如何成功地将传单地图保存为 .png 图像?

jay*_*.sf 1 linux maps png r leaflet

在地理坐标之外,我想在城市地图上绘制简单的平原点并将输出保存为.png。我使用的是Linux Ubuntu 22.04LTS。对于第一部分,我找到了一个非常简短且直接的leaflet解决方案。

library(leaflet); library(htmlwidgets); library(webshot)

m <- leaflet() %>% 
  addTiles("https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png") %>%
  setView(-.12, 51.50, zoom=13)
m$x$options <- append(m$x$options, list("zoomControl" = FALSE))
m <- m %>%
  addCircleMarkers(c(-.11, -.12, -.13), c(51.48, 51.52, 51.50),
                   radius=1, color="red")
m
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是,第二部分保存.png失败。我尝试过这个解决方案

saveWidget(m, "temp.html", selfcontained=TRUE)
webshot("temp.html", file="Rplot.png", cliprect="viewport")
Run Code Online (Sandbox Code Playgroud)

但是 - 使用phantomjs 2.1.1- 它给了我这些错误:

library(leaflet); library(htmlwidgets); library(webshot)

m <- leaflet() %>% 
  addTiles("https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png") %>%
  setView(-.12, 51.50, zoom=13)
m$x$options <- append(m$x$options, list("zoomControl" = FALSE))
m <- m %>%
  addCircleMarkers(c(-.11, -.12, -.13), c(51.48, 51.52, 51.50),
                   radius=1, color="red")
m
Run Code Online (Sandbox Code Playgroud)

使用稍旧的版本phantomjs 1.9.8不会出现错误,但图像完全空白。phantomjs我使用这些源在 Ubuntu 上安装: 1.9.82.1.1。我研究了这个答案,根据它你必须安装开发版本,但我发现它没有用,因为我不知道phantomjs.

我不一定需要互动leaflet,但我认为黑暗主题看起来很酷,并且符合夜间犯罪的研究主题。因此,如果静态解决方案看起来相似(如果它不使用谷歌),我对静态解决方案持开放态度。

Sté*_*ent 5

使用新的包webshot2,它使用 Chrome 或 Chromium 无头浏览器而不是phantomjs