rpowerpointreportersofficer

Mutliple formatted text on pptx by using officer package on R


Ciao,

I'm writing a code to generate an automatic report using officer package. I was wondering how and if I can write some text with different font. In my case I'd like to write some normal text and some bold words.

Let me show you what I get. I use these functions to generate fp_text objects:

fp_normal <- function(){
return( fp_text(color = "black", font.size = 16,
        bold = FALSE, italic = FALSE,
        underlined = FALSE, font.family = "Arial", 
        shading.color = "transparent") )
}


fp_bold <- function(){
return( fp_text(color = "black", font.size = 16,
        bold = TRUE, italic = FALSE,
        underlined = FALSE, font.family = "Arial", 
        shading.color = "transparent") )
}

I used to use combination of pot function by using sum operator and function textProperties:

pot("not bold ") + pot("and bold", textProperties(font.weight = "bold") )

My question is: how should I combine fp_normal and fp_bold functions with ph_with_text function?


Solution

  • I have updated the package to make that kind of operation easier. Usage of id_chr is not easy and the code below give the advantage to not use it :)

    library(magrittr)
    library(officer)
    
    fp_normal <- fp_text(font.size = 24)
    fp_bold <- update(fp_normal, bold = TRUE)
    fp_red <- update(fp_normal, color = "red")
    
    pars <- block_list(
      fpar(ftext("not bold ", fp_normal), ftext("and bold", fp_bold)),
      fpar(ftext("red text", fp_red))
    )
    my_pres <- read_pptx() %>%
      add_slide(layout = "Title and Content", master = "Office Theme") %>%
      ph_with(pars, location = ph_location_type(type = "body") ) 
    
    print(my_pres, target = "test.pptx")
    

    result