2

I would like to export multiple edible plots and tables from R to PowerPoint.

Below is a snippet of the code that I have, and the packages I'm using:

library(ggplot2)
library(dplyr)
library(tidyr)
library(gridExtra)
library(grid)
library(officer)
library(rvg)

read_pptx() %>%
   add_slide(layout = "Title and Content", master = "Office Theme") %>%
   ph_with_vg(code = grid.arrange(plot, table, col = 1), type = "body") %>%
   print(target = path)

In the output, the exported graph and table only covers the bottom 2/3 of the slide. How can I modify my code so that the content I'm trying to export could occupy the entire slide?

An additional question: If I would like to add a title to my slide, how do I specify the alignment (left preferred), size and color of the text? The default alignment is center, and I have not found a way to go around it.

Thank you.

Wyn Z.
  • 35
  • 4

1 Answers1

3

Use ph_location_fullsize() and it will work.

library(officer)
library(magrittr)
library(rvg)

read_pptx() %>%
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  ph_with(value = dml(barplot(1:5)), location = ph_location_fullsize()) %>%
  print(target = "test.pptx")
David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • The function dml does not seem to be available in R 3.4 or 3.5. – Wyn Z. Jul 01 '19 at 08:35
  • 1
    Also, I would like to insert a table into the same slide as the ggplot, which is not applicable with the function ph_with(). I get the following error: `Error in UseMethod("ph_with", value): no applicable method for 'ph_with' applied to an object of class "c('gtable', 'gTree', 'grob', 'gDesc')"` – Wyn Z. Jul 01 '19 at 09:21
  • 1
    I think you need to update packages officer and rvg to the latest versions. For the 2nd question, ask in another question and make sure to provide all necessary elements so that we can help (sessionInfo() and a reprex) – David Gohel Jul 01 '19 at 13:06