davidgohel / ReporteRs

Note that ReporteRs has been removed from CRAN the 16th of July 2018 and is not maintained anymore. please migrate to officer.
244 stars 44 forks source link

addPlot() doesn't work with grid.arrange() #210

Closed DanChaltiel closed 7 years ago

DanChaltiel commented 7 years ago

Hi David,

When I use addPlotto add some ggplots gathered with grid.arrange, the resulting plot in the docx is either null or white.

Here is some code with comments :


library(ggplot)
library(gridExtra)

p1 <- ggplot(data=mtcars, aes(x=cyl)) + geom_bar()
p2 <- ggplot(data=mtcars, aes(x=gear)) + geom_bar()
mydoc <- docx(title = "Test Doc")
mydoc <- addTitle(mydoc, "plot1",1)
mydoc <- addPlot(mydoc, fun = print, x = p1, vector.graphic = TRUE)
mydoc <- addTitle(mydoc, "plot2",1)
mydoc <- addPlot(mydoc, fun = print, x = p2, vector.graphic = TRUE)
p = grid.arrange(p1, p2, ncol=2)
mydoc <- addTitle(mydoc, "plot1 + plot2",1)
mydoc <- addPlot(mydoc, fun = print, x = p, vector.graphic = TRUE)
#Error in doc_parse_file(con, encoding = encoding, as_html = as_html, options = options) : StartTag: invalid element name [68]
mydoc <- addTitle(mydoc, "plot1 + plot2 too",1)
mydoc <- addPlot(mydoc, fun = print, x = p, vector.graphic = FALSE)
#No error but the plot is white
writeDoc(mydoc, "TestDoc.docx")

Is this a bug or I'm using it wrong ?

davidgohel commented 7 years ago

Hi

The following is OK:

library(ggplot2)
library(ReporteRs)
library(gridExtra)

p1 <- ggplot(data=mtcars, aes(x=cyl)) + geom_bar()
p2 <- ggplot(data=mtcars, aes(x=gear)) + geom_bar()
mydoc <- docx(title = "Test Doc")
mydoc <- addTitle(mydoc, "plot1",1)
mydoc <- addPlot(mydoc, fun = print, x = p1, vector.graphic = TRUE)
mydoc <- addTitle(mydoc, "plot2",1)
mydoc <- addPlot(mydoc, fun = print, x = p2, vector.graphic = TRUE)
p = grid.arrange(p1, p2, ncol=2)
mydoc <- addTitle(mydoc, "plot1 + plot2",1)
mydoc <- addPlot(mydoc, fun = function() print(cowplot::plot_grid(p)), vector.graphic = TRUE)
writeDoc(mydoc, "TestDoc.docx")