Dear all,
I am trying to build an api that allows me to post an RMD file, and which returns me a pdf.
This is what my code looks like:
#* compile pdf with parser
#* @param format The output format
#* @parser text
#* @parser multi
#* @serializer pdf
#* @post /compile3
function(req, res, format, data){ #data is my Rmd file
str(data)
rmd_path <- tempfile()
output_path <- "/srv/plumber/output_file.pdf"
write(data[[1]], rmd_path)
rmarkdown::render(rmd_path, output_path, output_format = format)
readBin(output_path, "raw", n = file.info(output_path)$size)
}
I realise writing this that I actually don't need the format
argument, since only will be getting pdfs, but this doesn't matter for my issues.
This is heavily inspired by the code found on this repo.
I can see that my markdown file gets written on my server, and that the output pdf file gets written as well. Opening the pdf file from the server shows me a nice looking pdf. However, the one I get back, client-side, is empty. Here is the query I run:
library(httr)
library(magrittr)
res <-
POST(
"http://shinybrodriguesco.duckdns.org:8000/compile3?format=pdf_document",
body = list(
data = upload_file("testmark.Rmd", "text/plain")
)
) %>%
content()
output_filename <- file("output_file.pdf", "wb")
writeBin(object = res, con = output_filename)
close(output_filename)
I have several questions actually:
- First of all, would this be the best, most elegant way to write the api?
- I actually don't really understand why I need the two parsers, #* @parser text and #* @parser multi. I found this on some stackoverflow thread, but can't find it back. I kinda get @parser text, since my Rmd file is pure text, but why @parser multi?
- Finally, what am I doing wrong?
I'm running R 4.1.0 with plumber version 1.1.0 via docker on my raspberry pi.
Thank you very much in advance for your help!