How to save a plot as image on disk from Viewer in RStudio?

一世执手 提交于 2019-12-13 14:27:04

问题


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.


回答1:


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").



来源:https://stackoverflow.com/questions/40160125/how-to-save-a-plot-as-image-on-disk-from-viewer-in-rstudio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!