I am currently working on a project involving a Plumber API, and I've encountered a challenge related to handling CSV files sent to the API.
I have a Plumber API where I need to receive two CSV files sent from Postman (for example) then they're used to generate a report that would be downloaded. I am unsure how to do that.
I found some helpful info about using mime package to do that in here: https://github.com/krlmlr/plumber-uploader/tree/f-multipart
I tried replicating that but it's still not working : "error 500"
Here's my code :
serializer_word <- function (..., type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
{
serializer_content_type(type, function(val) {
val
})
}
register_serializer("word", serializer_word)
#* create a report
#* @serializer word
#* @post /generate_report_from_data
upload <- function(req, res) {
multipart <- mime::parse_multipart(req)
uploaded_file1 <- multipart$upload[[1]]
uploaded_file2 <- multipart$upload[[2]]
in_file1 <- uploaded_file1$name
out_file1 <- uploaded_file1$datapath
df <- read.csv(in_file1, header = TRUE, sep = ";")
in_file2 <- uploaded_file2$name
out_file2 <- uploaded_file2$datapath
df_bbl <- read.csv(in_file2, header = TRUE, sep = ";")
df <- data_recoding(df, df_bbl, sep = ",")
team1 <- df
team2 <- df_bbl
output_path <- paste0(getwd(), "/rapport_.docx")
rmarkdown::render("rapport_.Rmd",
output_file = output_path,
output_format = word_document(reference_docx = "tmplt.docx"),
params = list(team1 = team1, team2 = team2))
file_content <- readBin(output_path, "raw", n = file.info(output_path)$size)
res$status <- 200
res$body <- as_attachment(file_content, "rapport_.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
}