How do I include ggplots using pander's live report generation

回眸只為那壹抹淺笑 提交于 2019-12-07 22:58:27

问题


I am using pander to create docx reports via Windows7, following the examples at http://rapporter.github.io/pander/#live-report-generation.

myReport <- Pandoc$new(author="Jerubaal",title="Plot Anything", format="docx")

I've tried the example

myReport$add(plot(1:10))

and that doesn't work on Windows, but does on Linux.

Previously I got plots appearing using brew files and <%=plot(1:10)=>, but I am trying out the Live Report Generation because that method seems most suited to me.

I've also tried saving a plot to file first and then creating an image link, which again work in Linux, but not in Windows:

myReport$add("![](plots/myplot.png)")

I want to include ggplot2 plots - the code works on its own in R but doesn't appear in the docx (although I do get a blank line).

R version 3.0.2 (2013-09-25) -- "Frisbee Sailing" pandoc.exe 1.12.2.1

What am I missing? Thanks.

Edit: I'm back home and this works on Ubuntu:

library(pander)
library(ggplot2)
setwd("/home/jerubaal/R/Projects/reports")
attach(movies)
m=movies[sample(nrow(movies), 1000),]
myReport=Pandoc$new(author="Jerubaal",title="Testing plots in reports",format="docx")
myReport$add.paragraph("There should be a plot after this")
p=ggplot(data=m, aes(x=rating,fill=mpaa)) + geom_density(alpha=0.25)
ggsave(filename=paste(getwd(),"plots/movies.png",sep="/"),plot=p,width=6,height=3)
myReport$add(paste("![](",paste(getwd(),"plots/movies.png",sep="/"),")",sep=""))
myReport$add.paragraph("There should be a plot before this")
myReport$export(tempfile())

Q: Would it cause a problem if the png took too long to create or save?

来源:https://stackoverflow.com/questions/21387298/how-do-i-include-ggplots-using-panders-live-report-generation

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