Saving an echarts4r image via code (again)

1.2k views Asked by At

I asked this question earlier here, but it was marked as duplicate and closed. Unfortunately the answer I was pointed to doesn't work....

So, again:

I can produce an eCharts4r gauge like

library(echarts4r) 
library(magrittr)

CA_gauge <- e_charts() %>% 
  e_gauge(4.1, 
          "INCIDENCE", 
          min=0, 
          max=20,
          axisLine = list(
            linestyle = list(
              color=list(
                c(1.5/20, "green"),
                c(3/20, "yellow"),
                c(1, "red")
              )
            ))) %>% 
  e_title("CA")

print(CA_gauge)

But I haven't found a good way to save the output to a file so that I can use it later in a gt table. The best I have been able to find is to add the "saveAsImage" to the output

e_charts() %>% 
      e_gauge(4.1, 
              "INCIDENCE", 
              min=0, 
              max=20,
              axisLine = list(
                linestyle = list(
                  color=list(
                    c(1.5/20, "green"),
                    c(3/20, "yellow"),
                    c(1, "red")
                  )
                ))) %>% 
        e_toolbox_feature(feature = c("saveAsImage"))

That adds a saveAs button in the upper right of the RStudio viewer

enter image description here

But what I'd really like to do is just save the image (obviously without animation) in code to a tiff/jpg/png image file. I have been producing about a dozen of these a week for a work project, so I have been manually saving the files. But they're kind of a hit and the boss wants a new version with 75+ of these.

I tried using the standard devices like

tiff(paste("CA_gauge.tif",sep=""), 
     res=600, compression = "lzw", height=5, width=15, units="in")
print(CA_gauge)
dev.off()

But that doesn't do it...all I get is an all white tif file. The answer that I was pointed to when my original thread was closed suggested using htmlwidgets and webshot. Following that example I came up with

CA_gauge <- e_charts() %>% 
  e_gauge(round(CA_data[[nrow(CA_data), 10]],1), 
          "CA \nINCIDENCE", 
          animation = FALSE,
          min=0, 
          max=20,
          animation = FALSE,
          axisLine = list(
            lineStyle = list(
              color=list(
                c(1.5/20, "green"),
                c(3/20, "yellow"),
                c(1, "red")
              )
            )))

htmlwidgets::saveWidget(widget = CA_gauge, file = "~/plot.html")
setwd("~")
webshot::webshot(url = "plot.html", 
                 file = "plot.png")

This produces two output files: plot.html and plot.png

Plot.html is a whole lot of JavaScript that looks like this in a browser

enter image description here

Which is all well and good, but plot.png is a 5kb .png file that is empty as far as I can tell....it shows up as a white screen when you try to open it, jsut like what I get from trying to use the standard devices.

Any suggestions?

1

There are 1 answers

0
mrhellmann On BEST ANSWER

I had the same problems using webshot, but webshot2 seems to work.

webshot2 from rstudio's github

library(echarts4r) 
library(magrittr)
library(webshot2)
library(htmlwidgets)

CA_gauge <- e_charts() %>% 
  e_gauge(4.1, 
          "INCIDENCE", 
          min=0, 
          max=20,
          axisLine = list(
            linestyle = list(
              color=list(
                c(1.5/20, "green"),
                c(3/20, "yellow"),
                c(1, "red")
              )
            ))) %>% 
  e_title("CA")

htmlwidgets::saveWidget(widget = CA_gauge, file = "~/ca_gauge.html")

# I had to set this for webshot2 to work on Ubuntu 20.04 LTS
# Sys.setenv(CHROMOTE_CHROME = '/snap/bin/chromium')
#  use 'Sys.which("chromium")' to get your path.

webshot('ca_gauge.html', file = 'ca_gauge.png')

The files in a file browser:

enter image description here

Output .png:

enter image description here