Patricia Bermudi
Patricia Bermudi

Reputation: 147

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

enter image description here

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

Upvotes: 1

Views: 1587

Answers (1)

ViviG
ViviG

Reputation: 1726

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

Upvotes: 1

Related Questions