简体   繁体   中英

Blank lines formatting officer package R

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

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

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