I'm currently developing a Plumber API in R that generates and sends back DOCX files. While the API appears to be working correctly, I'm facing an issue when attempting to open the downloaded DOCX file. I would appreciate any insights or suggestions to help resolve this matter.
- I have a Plumber endpoint that generates a Word document (DOCX) and serves it as a binary response.
- The generated DOCX file can be downloaded successfully, but there's an issue when attempting to open it. it can't be read even though the rmd script is able to generate a readable report docx. the report could be either docx or zipped. I'm fine with either.
as for my code :
1) plumber.R
#* @param test le deuxieme doc
#* @post /generate_report_word
function(test, res) {
tmp1 <- tempfile(tmpdir = getwd())
output_path <- paste0(getwd(), "/report_test.docx")
rmarkdown::render("report_test.Rmd", output_file = output_path,
params = list(team1 = test))
file_content <- readBin(output_path, "raw", n = file.info(output_path)$size)
res$setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
res$setHeader("Content-Disposition", sprintf("attachment; filename=%s", "report_test.docx"))
res$status(200)
res$body(file_content)
}
run_this.R
library(plumber)
library(rmarkdown)
r <- plumb("plumber.R")
r$run(port=8000)