1

I am currently using the Officer package to produce a Word document. I have been using body_add_par to add multiple chucks of blank lines throughout the document, but this method is already becoming tedious.

Is there a way to create a function, or somehow be able to write one line of code that is able to specify how many blank lines I want to insert?

Practice_R.docx = read_docx() %>%
  body_add_par("") %>%
  body_add_par("") %>%
  body_add_par("") %>% 
  body_add_par(paste("test")) %>% 
  body_add_par("") %>%
  body_add_par("") %>%
  body_add_par("") %>%
  body_add_par("") %>%
  body_add_par("") %>%
  body_add_par("") %>%
  body_add_par(paste("test2"))%>%
  body_add_break( pos = "after")
Peter
  • 11,500
  • 5
  • 21
  • 31
Hallie
  • 13
  • 3

1 Answers1

1

Try:

# repeat the addition of an empty paragraph n-times

body_add_par_n <- function(doc, n) {
    i <- 1 # initialize counter
    while (i<=n) { # control if the counter is less then desired n
        
        doc <- body_add_par(doc, "") # add paragraph to the object
        i <- i+1 # increment the counter
    }
    
    doc # return the object
}

Practice_R.docx = read_docx() %>%
  body_add_par_n(3) %>% 
  body_add_par(paste("test")) %>% 
  body_add_par_n(6) %>% 
  body_add_par(paste("test2"))%>%
  body_add_break( pos = "after")
Radim
  • 455
  • 2
  • 11