如何从RStudio中的Viewer将图另存为磁盘上的图像? [英] How to save a plot as image on disk from Viewer in RStudio?
问题描述
摘要:我的最终目标是在ReporteRs
PowerPoint报表自动化工作流程中使用rCharts
,尤其是Highcharts
.我要使用的图表之一在Rstudio的查看器"窗格中显示为html,并且addPlot(function() print(myChart))
不会将其添加到PowerPoint中.作为一种解决方法,我决定尝试将myChart
保存到磁盘,从那里我可以将myChart
添加到PowerPoint中.
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.
所以我的问题是真的,我如何将html图像放入我的ReporteRs
工作流程中?是将其保存到磁盘上还是让它被ReporteRs
读取都可以解决我的问题.
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.
此问题与此问题实际上相同,但我使用的是rCharts
,特别是
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
所以,如果我尝试
> png(filename="~/Documents/name.png")
> plot(myChart)
Error in as.double(y) :
cannot coerce type 'S4' to vector of type 'double'
> dev.off()
我得到那个错误.
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.
我的问题实际上是此的副本,该不是与此的重复将html输出嵌入Rmarkdown中,而不是将其另存为硬盘上的文件.
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.
正如@hrbrmstr和其他人所指出的那样,雷达图并非始终是最佳的可视化工具.我发现自己必须为这份报告做一份.
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.
推荐答案
答案原来在webshot
软件包中. @hrbrmstr提供了以下代码,该代码将在我在问题中发布的代码的末尾运行:
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)
这会将图以html
的形式保存到文件夹中,然后对其进行拍照,并以png
的形式保存.
This saves the plot to the folder as an html
, and then takes a picture of it, which is saved as a png
.
然后我可以使用addImage(mydoc, "/tmp/out.png")
运行ReporteRs
工作流程.
I can then run the ReporteRs
workflow by using addImage(mydoc, "/tmp/out.png")
.
这篇关于如何从RStudio中的Viewer将图另存为磁盘上的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!