Plumber has an input parser system based on the mime type in the request body. By default, only a handful of parsers are activated (txt, json, query, body, octet). In the solution below, I added two parsers, one for each match mime type (xls and xlsx). I also activated all parsers with the annotation #* @parser all
.
If plumber cannot find a matching activated parser for a mime type, it returns a raw vector named with the filename.
library(plumber)
parser_xlsx <- function(...) {
parser_read_file(function(tmpfile) {
readxl::read_xlsx(tmpfile, sheet = 1)
})
}
parser_xls <- function(...) {
parser_read_file(function(tmpfile) {
readxl::read_xls(tmpfile, sheet= 1)
})
}
register_parser("xlsx", parser_xlsx, fixed = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
register_parser("xls", parser_xls, fixed = "application/vnd.ms-excel")
#* @param dataset:file
#* @param a:str <param used for processing>.
#* @param b:str <param used for processing>.
#* @param c:number <param used for processing>.
#* @param d:number <param used for processing>.
#* @parser all
#* @post reconciliation/auto_match
#* @response 200 Json serialized dataset extended with reconsoliation columns
#* @tag Reconciliation
function(dataset, a, b, c, d) {
# Load the file based on its type
if (is.raw(dataset)) {
stop("Unsupported file type, please use a RDS, CSV, XLS or XLSX file.")
}
# Do some processing on dataset
...
# Return updated dataset to user
...
}
Documentation reference: