R language/reporteRs loop to write multiple docx -
i'm trying (as hard can) create script generate formatted word documents plain text files using r language , reporters.
to extract text 1 txt i'm using code found on thread dealing readlines() function in r :
filename <- "c:/myfolder/text_to_be_processed.txt" con <- file(filename,open="r") line <- readlines(con) close(con) then add extracted text docx :
doc <- docx(template="temp.docx") next, adding title (first line of txt file)
doc <- addparagraph( doc, value = line[1], bookmark = "titre", stylename = "titre") then body of txt file
doc <- addparagraph( doc, value = line[2:length(line)], value = line[2:55], stylename = "contenu") finally create docx
writedoc(doc, file = "output-file.docx") i want able create loop can generate multiple docx multiple txt files. appreciate
you can 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) # 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")) }) 
Comments
Post a Comment