davidgohel / rvg

https://davidgohel.github.io/rvg/
132 stars 15 forks source link

body_add_vg() breaking up some tableGrobs #12

Closed muranyia closed 7 years ago

muranyia commented 7 years ago

I'm having problems with some tableGrob tables added to a document using body_add_vg(). The simplest way I can reliable reproduce this is by setting the table wider than the available width in between the margins:

myt <- tableGrob(data.frame(a=c("q","w","e"),b=c("a","d","d"),c=c("y","x","c")), rows=NULL)
myt$widths <- unit(c(2,8,8), "cm")
grid.arrange(myt)

mydoc <- read_docx() %>%
  body_add_vg(code=grid.arrange(myt))

print(mydoc, target="tablegrobtest.docx")

Or use tablecontent.csv.zip and it will break also when it fits between the margins. The top and the bottom of the table are also clipped:

myt2 <- tableGrob(read.csv("tablecontent.csv"), rows=NULL)

grid.arrange(myt2)

mydoc2 <- read_docx() %>%
  body_add_vg(code=grid.arrange(myt2))

print(mydoc2, target="tablegrobtest2.docx")
davidgohel commented 7 years ago

Thanks,

This is a known issue. https://github.com/davidgohel/rvg/blob/master/R/dml_docx.R#L26

That's why body_add_vg is not documented nor mention here: https://davidgohel.github.io/officer/articles/offcran/graphics.html#word-documents-and-graphics

So far, tests has been made with outputs from grDevices::png(), grDevices::jpeg() and devEMF::emf(). Enhanced metafile graphics produced by package devEMF seem to be the best solution to have vector graphics within Word document.

I have added an extra note here to make it more clear: https://github.com/davidgohel/rvg/blob/master/R/body_add_vg.R#L10