2

I am using the R package OfficeR to create a PowerPoint presentation of my calculation results.

Would it be possible to add ftext objects to the same fpar under program control e.g. a for loop or an if statement?

Something like:

fp1 <- ftext("Line 1", prop = fp_text())
for (c in seq(2,5)) 
  fp1 <- fpar_add(ftext(sprintf("Line %d", c) , prop = fp_text()))

Thanks for your suggestions.

Gerben
  • 61
  • 3
  • OfficeR should allow you to add text easy. Although I've only used officeR to work on word documents. If you want to dive deeper, you could likely do everything with the `RDCOMClient` package. It allows you to make microsoft applications such as powerpoint and theoretically change every aspect of them as you would with microsoft functions. The only problem is there isn't any R documentation that maps microsoft functions to the R class, so a lot of exploration on the Dev pages of microsoft is required. good luck! – Justin Landis Jan 28 '20 at 14:03
  • Would you be open to other presentation frameworks instead of PowerPoint? – GcL Jan 28 '20 at 14:13

2 Answers2

3

Thanks for learning me the do.call statement. I never used it before and therefore my question was not accurate enough explaining what I was looking for. This code solves my problem:

library(officer)

bodyTxt <- lapply(seq(1,5), function(x) fpar(ftext(sprintf("Line %d", x) , prop = prop)))
bodyTxt[[3]] <- fpar(ftext(" " , prop = prop))

bl <- do.call(block_list, bodyTxt)

doc <- read_pptx()
doc <- add_slide(doc)
doc <- ph_with(x = doc, value = bl,
               location = ph_location_type(type="body") )

print(doc, target = "test.pptx" )

Thank you very much!

Gerben
  • 61
  • 3
0

You can use do.call for that. (You are free to replace the lapply by any code that would produce a list of ftext) :

library(officer)

prop <- fp_text()
fp <- do.call( 
  fpar, 
  lapply(seq(2,5), 
         function(x) ftext(sprintf("Line %d", x) , prop = prop) )
)

doc <- read_pptx()
doc <- add_slide(doc)
doc <- ph_with(x = doc, value = fp,
               location = ph_location_type(type="body") )

print(doc, target = "test.pptx" )

enter image description here

David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • Thanks David for your quick reply. But you are calling the fpar function only once with a number of ftext statements. That results in all ftext lines to be on the same line in the PowerPoint. What I am looking for is combining several calls to fpar in a combined fpar, similar to what you do in a block_list. I tried to combine fpar objects or block_list objects but failed so far. Is something like this possible in OfficeR? – Gerben Jan 28 '20 at 15:32
  • OK, but your question is about adding ftext objects to the same fpar. I used do.call but could also use a for loop. "combining several calls to fpar in a combined fpar", I am not sure I understand. You can combine several `ftext` in a `fpar` and several `fpar` in a `block_list`. You could build that object(s) with do.cal, with package rlang (maybe other methods) or with a double for loop. – David Gohel Jan 28 '20 at 15:56