3

Summary: my ultimate goal is to use rCharts, and specifically Highcharts, as part of a ReporteRs PowerPoint report automation workflow. One of the charts I would like to use is rendered as html in the Viewer pane in Rstudio, and addPlot(function() print(myChart)) does not add it to the PowerPoint. As a workaround, I decided to try to save myChart to disk, from where I could just add it to the PowerPoint that way.

So my question is really, How do I get my html image into my ReporteRs workflow? Either getting it saved to a disk, or getting it to be readable by ReporteRs would solve my problem.

This question is really the same as this one, but I'm using rCharts, specifically the example found here:

#if the packages are not already installed
install.packages('devtools')
require(devtools)
install_github('rCharts', 'ramnathv')

#code creates a radar chart using Highcharts
library(rCharts)
#create dummy dataframe with number ranging from 0 to 1
df<-data.frame(id=c("a","b","c","d","e"),val1=runif(5,0,1),val2=runif(5,0,1))
#muliply number by 100 to get percentage
df[,-1]<-df[,-1]*100

myChart <- Highcharts$new()
myChart$chart(polar = TRUE, type = "line",height=500)
myChart$xAxis(categories=df$id, tickmarkPlacement= 'on', lineWidth= 0)
myChart$yAxis(gridLineInterpolation= 'circle', lineWidth= 0, min= 0,max=100,endOnTick=T,tickInterval=10)
myChart$series(data = df[,"val1"],name = "Series 1", pointPlacement="on")
myChart$series(data = df[,"val2"],name = "Series 2", pointPlacement="on")
myChart

So if I try

> png(filename="~/Documents/name.png")
> plot(myChart)
Error in as.double(y) : 
  cannot coerce type 'S4' to vector of type 'double'
> dev.off()

I get that error.

I've looked into Highcharts documentation, as well as many other potential solutions that seem to rely on Javascript and phantomjs. If your answer relies on phantomjs, please assume I have no idea how to use it. webshot is another package I found which is even so kind as to include an install_phantomjs() function, but from what I could find, it requires you to turn your output into a Shiny object first.

My question is really a duplicate of this one, which is not a duplicate of this one because that is how to embed the html output in Rmarkdown, not save it as a file on the hard drive.

I also found this unanswered question which is also basically the same.

edit: as noted by @hrbrmstr and scores of others, radar charts are not always the best visualization tools. I find myself required to make one for this report.

Community
  • 1
  • 1
BLT
  • 2,492
  • 1
  • 24
  • 33
  • Use the `webshot` package. there are examples of how to do that on SO but it's pretty straightforward: `myChart$save("/tmp/rcharts.html") ; webshot::webshot("/tmp/rcharts.html", file="/tmp/out.png", delay=2)`. But, why not just use the `radarchart` package? – hrbrmstr Oct 20 '16 at 17:11
  • `webshot` uses `phantomjs`. I didn't see any `radarchart` output that looked any good. I did just see that a bug I had with `ggradar` was addressed so I may go back and try that package. – BLT Oct 20 '16 at 17:15
  • Yes, it uses phantomjs and it _actually has_ an `install_phantomjs()` function (need to read help pages). As far as radar charts go (they're usually terrible visualization choices) `fsmb::radarchart()` [doesn't do a bad job](https://www.dropbox.com/s/agmc0peh2xywky9/Picture1.png?dl=1) and I find it infinitely better (more visually appealing) than rCharts (i.e. the ones this post generates) but there are way better vis choices IMO. – hrbrmstr Oct 20 '16 at 17:21
  • edited to clarify that I tried `webshot` and `install_phantomjs()` previously but it didn't appear to be what I was looking for as I went through that and many other readme files on github. Thank you for your suggestion that I read the help pages. If you post the code in the first comment, I will accept it - I was looking all over for something like `myChart$save(filepath)`. – BLT Oct 20 '16 at 17:34

1 Answers1

5

The answer turned out to be in the webshot package. @hrbrmstr provided the following code, which would be run at the end of the code I posted in the question:

# If necessary
install.packages("webshot")
library(webshot)
install_phantomjs()

# Main code
myChart$save("/tmp/rcharts.html")
webshot::webshot("/tmp/rcharts.html", file="/tmp/out.png", delay=2)

This saves the plot to the folder as an html, and then takes a picture of it, which is saved as a png.

I can then run the ReporteRs workflow by using addImage(mydoc, "/tmp/out.png").

BLT
  • 2,492
  • 1
  • 24
  • 33