Chris Beeley
Chris Beeley

Reputation: 601

Can't download docx with R plumber API on Linux

I've produced a plumber API that looks like this:

#* @serializer contentType list(type="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
#* @get /word
function(team){
  tmp <- tempfile()

  render("test.Rmd", tmp, output_format = "word_document",
         params = list(team = team))

  readBin(tmp, "raw", n=file.info(tmp)$size)
}

Running locally it works fine on Windows, producing a docx file for download. It also seems to work okay if you run it locally on Linux and use Firefox, although it does seem to crash Firefox as it's trying to open or download the docx file.

But running locally on Linux and downloading via Chrome produces a binary file that is not recognised by the OS. If you select "Open with... Libre Office Writer" the file opens fine, but I really need my users to get a properly formatted file that will be opened automatically.

I can't figure out whether the problem is with the browser or the OS. Any help appreciated.

All the code is in this repository, as I say it all works fine in Windows so I actually put it up thinking it would be a useful reference for others, but now I can't get it to work on Linux- here

Upvotes: 1

Views: 444

Answers (1)

Chris Beeley
Chris Beeley

Reputation: 601

All thanks to Panagiotis, here's the code

#* @serializer contentType list(type="application/octet-stream")
#* @get /word
function(team, res){

  res$setHeader("Content-Disposition", "attachment; filename=report.docx")

  tmp <- tempfile()

  render("test.Rmd", tmp, output_format = "word_document",
         params = list(team = team))

  readBin(tmp, "raw", n=file.info(tmp)$size)
}

Upvotes: 3

Related Questions