如何在 R 中将高图保存为 PNG 图像?

How to save a highchart to PNG image in R?

如何在 R 中将图表 (type = 'organization') 导出为 PNG 图像?

这是一个例子:

library(highcharter)
library(tidyverse)

highchart() %>%
  hc_chart(type = 'organization') %>%
  hc_add_series(
    data = list(
      list(from = 'A', to = 'A1'),
      list(from = 'A', to = 'A2')
    )) 

我试过如下,但是导出的文件是空的:

library(highcharter)
library(tidyverse)

png("org.png")

highchart() %>%
  hc_chart(type = 'organization') %>%
  hc_add_series(
    data = list(
      list(from = 'A', to = 'A1'),
      list(from = 'A', to = 'A2')
    ))

dev.off()

您可以使用包 webshot to achieve that. Read more about on Export highchart widget as figure in R

编辑:答案适用于使用 webshot2 的 OP,这意味着取代 webshot。

library(highcharter)
library(tidyverse)
library(webshot)


org <- highchart() %>%
  hc_chart(type = 'organization') %>%
  hc_add_series(
    data = list(
      list(from = 'A', to = 'A1'),
      list(from = 'A', to = 'A2')
    ))

htmlwidgets::saveWidget(widget = org, file = "org.html")
getwd()
webshot(url = "org.html", 
        file = "org.png",
        delay=3) # delay will ensure that the whole plot appears in the image
dev.off()