1

Is it possible to insert external images into a Powerpoint with officer without changing the image dimensions?

This is the code I have now where x is an rpptx object:

library(officer)
image_url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Instagram_logo_2016.svg/768px-Instagram_logo_2016.svg.png"
 t_file <- tempfile(pattern = "logo_", fileext = ".png")
 download.file(image_url, destfile = t_file)
 x <- x %>%
    ph_with(value = external_img(t_file), location = ph_location(left = 9.4, top = 4.6))

This will change the dimensions of the image to 4x3 inches (the default for ph_location()).

I know you can use external R packages to get image metadata then pass this to the function so the actual image dimensions are used, but I don't want to add any more external R package dependencies.

Giovanni Colitti
  • 1,982
  • 11
  • 24

1 Answers1

2

You can always specify the image dimensions within your external_img function. I'm assuming you know what dimensions you want, so say your dimensions are width = 9, height = 5 it would look something like this:

    library(officer)
image_url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Instagram_logo_2016.svg/768px-Instagram_logo_2016.svg.png"
 t_file <- tempfile(pattern = "logo_", fileext = ".png")
 download.file(image_url, destfile = t_file)
 x <- x %>%
#specify height and width
    ph_with(value = external_img(t_file, width = 9, height = 5), location = ph_location(left = 9.4, top = 4.6))

Hope that is helpful!