Get file through API call (R & plumber)

2019-02-26 11:24发布

问题:

I'm using plumber as a simple web-api service for some R functions.

I would like to provide a file 'download' (on the client side) through an R function very much like flask is doing it in python through send_file and send_from_directory.

I tried

#* @get /datafile
get_file <- function(){
  return(file('path-to-file.RData'))
}

But unfortunately it didn't work (because the return value cannot be transformed to JSON). I'm aware of static file server option in plubmer, but I really want to provide only one single file and not a directory. (Although serving files in directory through @assets seems to be a more secure option.)

回答1:

Sorry this isn't better documented, but see some examples here: https://github.com/trestletech/plumber/blob/master/tests/testthat/files/includes.R

I don't remember if I've tested with binary data or how it will handle the Rdata content type, so if things are still weird please open a ticket.