简体   繁体   中英

How to save a highchart to PNG image in R?

How to export a highchart (type = 'organization') to PNG image in R?

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. Read more about on Export highchart widget as figure in R .

EDIT : The answer worked for the OP using webshot2 , which is meant to replace 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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