I'm trying to merge all word documents in a given directory together using the officer library in R. I can't quite figure out how to do this, though. Here's my attempt
# Directory where your .docx files are located
dir <- "../../Word_Docs"
# Get a list of all .docx files in the directory
docx_files <- list.files(dir, pattern = ".docx$", full.names = T)
# Create an empty Word document to hold the merged contents
doc1 <- read_docx()
# Loop through each .docx file and append its contents to doc1
for (file in docx_files) {
print(file)
doc <- read_docx(file)
doc1 <- doc1 %>% body_add_par(doc)
}
# Print the final merged document to a new .docx file
print(doc1, target = "../../Tables/Final-Output.docx")
I'm getting the error: Error: x
must be a string of length 1
Any thoughts?