Abhishek Thakurdesai Abhishek Thakurdesai - 2 months ago 19
R Question

adding plots to a ppt in R

Using reportRs pacakge, I'm trying to add several graphs(.png/.jpg) which are named as e.g. test-0,test-1,test-2 etc into a pptx file. These graphs have been extracted from a pdf named e.g. test using im.convert function.I can add them individually but not able to automate the code for graphs,title, slide number, date etc in loop which can figure out how many graphs with 'test' name are there in a folder and then import them in the pptx one by one in a new slide ata time and one final pptx file.

sample code:

library(animation)
im.convert("Test.pdf", output = "Test.png", extra.opts="-density 150")

library("ReporteRs")
doc <- pptx()
doc <- pptx(template = templateDir)
doc <- addSlide( doc, slide.layout = 'Competative Landscape' )
doc <- addTitle(doc, paste("Test-0"))
doc <- addImage(doc, "Test-0.png")
:
:

:
:

doc <- addSlide( doc, slide.layout = 'Competative Landscape' )
doc <- addTitle(doc, paste("Test-3"))`enter code here`
doc <- addImage(doc, "Test-3.png")

Answer

You could try using the list.files function to find the nummber of png files with the name Test in a folder. sample code:

list_of_files=list.files(path = "C:/output_folder", pattern = c("Test",".png"))
library("ReporteRs")
doc <- pptx()
doc <- pptx(template = templateDir)
for( i in 0:(length(list_of_files)-1))
{
  doc <- addSlide( doc, slide.layout = 'Competative Landscape' )
  doc <- addTitle(doc, paste0("Test-",i))
  doc <- addImage(doc, paste0("Test-",i,".png"))
}  
Comments