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 function problem #202

Closed AndreaQ7 closed 7 years ago

AndreaQ7 commented 7 years ago

Hi everyone, I've produced two plots with ggplot2 and put them in a vector "p" and I want to write them in a docx documents but i receive always an error and i don't understand how to fix it:

` p [[1]] RStudioGD 2

[[2]] RStudioGD 2`

`mydoc = docx()

base_legend = "My first plot"

for( i in seq_along(p) ){ mydoc = addPlot(mydoc, fun = print, x = p[[i]],vector.graphic = TRUE, par.properties = parCenter(), width = 6, heigth = 7 ) } writeDoc( mydoc, "Newdocument.docx")`

But then I receive this error: Error in doc_parse_file(con, encoding = encoding, as_html = as_html, options = options) : StartTag: invalid element name [68]

What does it means? How can i solve it ? thanks a lot

davidgohel commented 7 years ago

I can't reproduce your example without the code for graphics, it's hard to help. Below an example that works for me:

library(ReporteRs)
library(magrittr)
library(ggplot2)

p1 <- ggplot(data = mtcars, aes(x = qsec, y =wt)) + geom_point()
p2 <- ggplot(data = mtcars, aes(x = qsec, y =drat)) + geom_point()
p <- list(p1, p2)

mydoc = docx()

for( i in seq_along(p) ){
  mydoc = addPlot(mydoc, fun = print, x = p[[i]],vector.graphic = TRUE, par.properties = parCenter(), width = 6, heigth = 7 )
}
writeDoc( mydoc, "Newdocument.docx")

If possible, switch to officer package:

library(officer)
library(devEMF)

mydoc = read_docx()

for( i in seq_along(p) ){
  emf_file <- tempfile(fileext = ".emf")
  emf(file = emf_file, width = 6, height = 7)
  print(p[[i]])
  dev.off()
  mydoc = body_add_img(mydoc, src = emf_file, style = "centered", width = 6, height = 7 )
}
print( mydoc, target = "Newdocument2.docx")