rofficerformatted-text

Officer - paste text with tabulator


I am trying to integrate 2 tabulators between the string "Name:" and the actual Species name from the iris database. I want to have a formatted text in word.docx. Can someone help me?

my_prop_1 = officer::fp_text(font.size = 10, underlined=T, bold = TRUE, font.family = "Arial")
my_prop_2 = officer::fp_text(font.size = 11, underlined=T, bold = TRUE, font.family = "Arial")
my_prop_3 = officer::fp_text(font.size = 11, underlined=F, bold = TRUE, font.family = "Arial")
my_prop_4 = officer::fp_text(font.size = 10, underlined=F, bold = T, font.family = "Arial")
my_prop_5 = officer::fp_text(font.size = 10, underlined=T, bold = F, font.family = "Arial")
my_prop_6 = officer::fp_text(font.size = 10, underlined=F, bold = F, font.family = "Arial")

lapply(1:10, function(x) {
  empty_docs <- officer::read_docx()

  sample_doc <- empty_docs %>% officer::body_add_fpar(officer::fpar(officer::ftext(paste("Name:"), 
                                                                                   my_prop_5),
                                                                    officer::ftext(paste(iris$Species)[[x]], my_prop_6)))
  print(sample_doc, target = paste0("Test",x,".docx"))
  if (any(list.files() == paste0("Test",x,".docx"))) 
  {cat(paste("File is saved. \n"))}
  #return(sample_doc)
}) 

Thanks, Nadine


Solution

  • You can use \t as tabulator.

    library(officer)
    library(magrittr)
    
    read_docx() %>% 
      body_add_fpar(fpar("Name:\t\tdoudou bleu"), style = "Normal") %>% 
      print(target = "test.docx")