r - Why can't I send a group of plots in a grid to PowerPoint with officer?
Asked Answered
U

2

3

I've created a grid of qicharts2 plots using gridextra but when I try to send it to PowerPoint using officer I get this error..

Error in doc_parse_raw(x, encoding = encoding, base_url = base_url, as_html = as_html, : StartTag: invalid element name [68]

This is my code:

library(qicharts2)
library(gridExtra)
library(officer)
library(rvg)



#24 random numbers from a normal distribution for example.
y1 <- rnorm(24)
y2 <- rnorm(24)

yC1 <- qic(y1)
yC2 <- qic(y2)

grid <- grid.arrange(yC1,yC2)


filename <- "C:\\Desktop\\MyCharts.pptx"


read_pptx(filename) %>% 

  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_vg(code = print(grid), type = "body") %>% 
  print(target = filename) %>% 
  invisible()

Huge thanks to everyone for your advice on how to improve my question so far.

Any help further help greatly received

Unrobe answered 23/1, 2018 at 17:28 Comment(6)
Could you provide necessary code to reproduce GridTot?Punctilious
Thanks David - any help hugely appreciatedUnrobe
I don't know r, but just a pro-tip for debugging stuff is try to find the smallest piece of code that will give you the error. SO calls it a minimal reproducible example.Urochrome
Your code is not reproducible and not minimal, it means you have to provide a short code that I will copy/paste and that will allow me to see and reproduce your issue. The link @cullub provided is a 'must read' if you are novice.Punctilious
Great article, thanks for the advice - hope this is better nowUnrobe
it's far better :)Punctilious
P
4

When using grid.arrange, you are plotting, the print method is not useful here (print(grid) had no effect on graphical device). The following is working with grid.arrange:

library(qicharts2)
library(gridExtra)
library(officer)
library(rvg)
library(magrittr)

#24 random numbers from a normal distribution for example.
y1 <- rnorm(24)
y2 <- rnorm(24)

yC1 <- qic(y1)
yC2 <- qic(y2)

read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_vg(code = grid.arrange(yC1,yC2), type = "body") %>% 
  print(target = "filename.pptx") %>% 
  browseURL()

Edit for rvg >= 0.2.4 : You must use the dml function :

read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(value = dml(grid.arrange(yC1,yC2)), location = ph_location_type(type = "body")) %>% 
  print(target = "filename.pptx") %>% 
  browseURL()
Punctilious answered 29/1, 2018 at 12:8 Comment(1)
can you validate the answer?Punctilious
J
0

Add plot instead of print works for me. There is some bug in grid.arrange but if it went trough plot() it works. This particular example was working as in previous answer but more complex with multiple grobs and text boxes was not.

read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with(value = dml(code=plot(grid.arrange(yC1,yC2))),
          location = ph_location_type(type = "body")) %>% 
  print(target = "filename.pptx") %>% 
  browseURL()
Jeremiahjeremias answered 3/2, 2021 at 0:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.