I'm trying to use officer
to make a report. However, sometimes, the plots that I want to add don't have enough data, so rather than throwing an error, I just want R to leave a blank space where that plot would go.
However, I've tried using try()
and an if statement, but neither work. For try()
, officer just skips over any line that uses it, even if the plot exists.
I've tried the is.error()
function from the BBmisc
package, but, despite the plot printing an error when you try to load it, is.error("error_plot") gives FALSE. (As an aside, if anyone familiar with BBmisc/ggplot2 knows why this is, I'm curious.)
How can I get officer to skip over these errors (and just the errors)?
#Set up
library(officer)
library(ggplot2)
library(dplyr)
library(BBmisc)
#Read in blank deck
blank_deck <- read_pptx()
#Define location for objects
my_location_1 <- ph_location(left = .43, top = 2.18, height = 2.36, width = 4.17)
my_location_2 <- ph_location(left = 5.39, top = 2.18, height = 2.36, width = 4.17)
#Make a simple plot--this plot is good
good_plot <- try(ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
geom_col())
#Make a simple plot--this plot intentionally is wrong
error_plot <- try(ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
geom_col() +
facet_wrap(~Species2))
Here's solution 1, with try()
#Note: run the code from the set up section first
sample <- blank_deck %>%
add_slide(layout = "Title Slide", master = "Office Theme") %>%
ph_with(value = "My Title", ph_location_label(ph_label = "Title 1")) %>%
add_slide(layout = "Blank", master = "Office Theme") %>%
try(ph_with(value = good_plot, location = my_location_1)) %>%
try(ph_with(value = error_plot, location = my_location_2)) %>%
add_slide(layout = "Title Slide", master = "Office Theme") %>%
ph_with(value = "End", ph_location_label(ph_label = "Title 1")) %>%
base::print(
target = '/home/define/your/pathway/mydeck.pptx')
And here's my attempt at using an if/then statement with is.error():
sample <- blank_deck %>%
add_slide(layout = "Title Slide", master = "Office Theme") %>%
ph_with(value = "My Title", ph_location_label(ph_label = "Title 1")) %>%
add_slide(layout = "Blank", master = "Office Theme") %>%
if_else(is.error("good_plot"), (ph_with(value = empty_content(), location = my_location_1)),
(ph_with(value = error_plot, location = my_location_2))) %>%
if_else(is.error("error_plot"), (ph_with(value = empty_content(), location = my_location_2)),
(ph_with(value = error_plot, location = my_location_2))) %>%
add_slide(layout = "Title Slide", master = "Office Theme") %>%
ph_with(value = "End", ph_location_label(ph_label = "Title 1")) %>%
base::print(
target = '/home/define/your/pathway/mydeck.pptx')