[英]How to save a plot as image on disk from Viewer in RStudio?
Summary: my ultimate goal is to use rCharts
, and specifically Highcharts
, as part of a ReporteRs
PowerPoint report automation workflow. 简介:我的最终目标是使用rCharts
,特别是Highcharts
,作为ReporteRs
PowerPoint报告自动化工作流程的一部分。 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. 我想使用的其中一个图表在Rstudio的Viewer窗格中呈现为html,而addPlot(function() print(myChart))
不会将其添加到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. 作为一种解决方法,我决定尝试将myChart
保存到磁盘,从那里我可以将其添加到PowerPoint中。
So my question is really, How do I get my html image into my ReporteRs
workflow? 所以我的问题是, 如何将我的html图像放入ReporteRs
工作流程? Either getting it saved to a disk, or getting it to be readable by ReporteRs
would solve my problem. 要么将其保存到磁盘,要么让它可由ReporteRs
读取, ReporteRs
将解决我的问题。
This question is really the same as this one , but I'm using rCharts
, specifically the example found here : 这个问题确实是一样的这一个 ,但我使用rCharts
,特别是例如发现这里 :
#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
. 我已经研究过Highcharts文档 ,以及许多 其他似乎依赖于Javascript和phantomjs
潜在 解决方案 。 If your answer relies on phantomjs
, please assume I have no idea how to use it. 如果你的答案依赖于phantomjs
,请假设我不知道如何使用它。 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. webshot
是我发现的另一个包,它甚至包括一个install_phantomjs()
函数,但是从我能找到的,它要求你先将输出变成一个Shiny
对象。
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. 我的问题是真正的副本这一块 ,这不是重复这一块 ,因为这是如何嵌入HTML输出在Rmarkdown,而不是将其保存为硬盘驱动器上的文件。
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. 编辑:正如@hrbrmstr和其他许多人所说,雷达图并不总是最好的可视化工具。 I find myself required to make one for this report. 我发现自己需要为此报告制作一个。
The answer turned out to be in the webshot
package. 答案结果是在webshot
包中。 @hrbrmstr provided the following code, which would be run at the end of the code I posted in the question: @hrbrmstr提供了以下代码,它将在我在问题中发布的代码的末尾运行:
# 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
. 这将绘图作为html
保存到文件夹,然后拍摄它的照片,保存为png
。
I can then run the ReporteRs
workflow by using addImage(mydoc, "/tmp/out.png")
. 然后,我可以使用addImage(mydoc, "/tmp/out.png")
运行ReporteRs
工作流程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.