Exporting R dataframe with image in a column to PPT

I have populated a dataframe by getting data from SQL, I am using Officer Package to sent it to PPT.
It works well when only text or numbers in the columns of a dataframe but when a image is in by the side of text it not getting the image.

Example
Here is the R code:

            add_slide(layout = "Title and Content", master = "Office Theme") %>%
            ph_with_img(type = "body", src = Plot2.file,index = 1,height = 4, width = 8)
          ph_with_flextable_at(doc,value = table2,left = 1, top = 6)
          print(doc,target = ("Error.pptx",append=TRUE)

when the code executes we can see the image in table in R but the same table when sent to PPT, the image is missing.

Please any help on this would be greatly appreciated..

You won't get any image display as output format is PowerPoint. This is not a choice nor a
unimplemented feature. This is because PowerPoint is not able to embed images in a table cell.
That’s a PowerPoint limitation.

see https://davidgohel.github.io/flextable/articles/display.html#limitation-for-powerpoint

I wrote a full example for the sake of reproducibility :slight_smile:

# you need the latest version of flextable that should be on cran soon
# remotes::install_github("davidgohel/flextable")

library(flextable)
library(officer)
library(magrittr)

table2 <- flextable( head(iris, n = 10 )) %>% 
  compose( j = 1,
           value = as_paragraph(
             minibar(value = Sepal.Length, max = max(Sepal.Length))
             ), part = "body") %>% autofit()

read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  ph_with_flextable_at(value = table2, left = 1, top = 2) %>% 
  print(target = "Error.pptx")

In HTML, you will get the following flextable:

But in PowerPoint, images are not displayed:

The solution for that is to use save_as_image function:

table2 <- flextable( head(iris, n = 10 )) %>% 
  compose( j = 1,
           value = as_paragraph(
             minibar(value = Sepal.Length, max = max(Sepal.Length))
             ), part = "body") %>% autofit()

png_file <- tempfile(fileext = ".png")
save_as_image(table2, path = png_file)
dims <- flextable_dim(table2)
read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  ph_with(external_img(png_file, width = dims$widths, height = dims$heights), use_loc_size = FALSE, 
          location = ph_location_type(type = "body") ) %>% 
  print(target = "no_more_blanck_and_no_more_editing.pptx")

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.