简体   繁体   English

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

[英]How to save a highchart to PNG image in R?

How to export a highchart (type = 'organization') to PNG image in R?如何在 R 中将 highchart(type = 'organization')导出为 PNG 图像?

Here is an example:这是一个例子:

library(highcharter)
library(tidyverse)

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

在此处输入图像描述

I tried as follows, but the exported file is empty:我尝试如下,但导出的文件为空:

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()

You can use the package webshot to achieve that.您可以使用 package webshot来实现这一点。 Read more about on Export highchart widget as figure in R .R 中阅读更多关于导出 highchart 小部件的信息

EDIT : The answer worked for the OP using webshot2 , which is meant to replace webshot.编辑:答案适用于使用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM