简体   繁体   中英

Adding R officer ftext objects to an fpar object using a for loop

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 eg 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.

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!

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" )

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM