Search code examples
rdocx

R language/reporteRs loop to write multiple docx


I'm trying (as hard as i can) to create a script that will generate formatted word documents from plain text files using R language and reporteRs.

To extract text from one txt i'm using this code found on this thread Dealing with readLines() function in R :

fileName <- "C:/MyFolder/TEXT_TO_BE_PROCESSED.txt"
con <- file(fileName,open="r")
line <- readLines(con)
close(con)

Then add the extracted text to docx with this :

doc <- docx(template="temp.docx")

Next, adding the title (first line of the txt file)

doc <- addParagraph( doc, value = line[1], bookmark = "titre", stylename = "Titre")

then the body of the txt file

doc <- addParagraph( doc, value = line[2:length(line)], value = line[2:55], stylename = "Contenu")

Finally I create the docx

writeDoc(doc, file = "output-file.docx")

I want to be able to create a loop so I can generate multiple docx from multiple txt files. I will really appreciate your help


Solution

  • You can do something like this with lapply

    myFiles <- c("C:/MyFolder/TEXT_TO_BE_PROCESSED.txt", "C:/MyFolder/TEXT_TO_BE_PROCESSED2.txt") # or use list.files()
    
    lapply(myFiles, function(fileName){
      con <- file(fileName,open="r")
      line <- readLines(con) # you could just call readLines(fileName)
      close(con)
      doc <- docx(template="temp.docx")
      doc <- addParagraph( doc, value = line[1], bookmark = "titre", stylename = "Titre")
      doc <- addParagraph( doc, value = line[2:length(line)], value = line[2:55], stylename = "Contenu")
      writeDoc(doc, file = paste0(fileName, "out.docx"))
    })