I am creating a Power Point presentation using Officer with tables created programmatically. The data varies all the time so my goal is to do it using a for loop or something else.
In the example below I have 3 flextable
objects stored in a list. In my real problem these will be a larger number of tables (usually >30 tables). I also need each slide to contain one table.
Then, I create a slide and use a for loop to add each table to a slide. However at this point I get the error:
Error in UseMethod("ph_with", value) :
no applicable method for 'ph_with' applied to an object of class "list"
When selecting or unlisting items from a list the object class is no longer flextable
and the function ph_with()
does not like that. I have also tried to use unlist
but get the same error.
Is there any way to run ph_with()
programmatically, adding 1 item (table and/or chart) per slide?
Thank you
library(magrittr)
library(officer)
library(flextable)
# Areate tables (in my code these are create programmatically using a for loop)
ft1 <- qflextable(head(airquality))
ft2 <- qflextable(head(ggplot2::diamonds))
ft3 <- qflextable(head(mtcars))
# Atore flextables in a list
table_list <- list(ft1, ft2, ft3)
# Create slide
my_pres <- read_pptx()
# Add 1 table per slide from the list created previously
for(i in 1:length(table_list)) {
my_pres <- my_pres %>%
add_slide(my_pres, layout = "Title and Content", master = "Office Theme") %>%
ph_with(table_list[i], location = ph_location_type(type = "body"))
}
# I get an error here!!!
# Error in UseMethod("ph_with", value) :
# no applicable method for 'ph_with' applied to an object of class "list"
print(my_pres, target = "My_presentation.pptx")