I am trying to write the R Plumber code to create an API that allows users to upload a CSV, XLSX, or RDS file. The function should read the data from the file and allow me to do some processing and return the updated file to the user.
I was thinking of something along the lines of this.
#* @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>.
#* @post reconciliation/auto_match
#* @response 200 Json serialized dataset extended with reconsoliation columns
#* @tag Reconciliation
function(dataset, a, b, c, d){
# Check the file type of dataset param
file_type <- **.....?**
# Print the file type
cat("Loading", file_type, "file...\n")
# Load the file based on its type
if (file_type == "rds") {data <- readRDS(**.....?**)}
else if (file_type == "csv") {
# Check the delimiter
first_line <- readLines(**.....?**, n = 1)
if (grepl(";", first_line)) {
data <- read.csv(**.....?**, sep = ";")
} else if (grepl("\t", first_line)) {
data <- read.csv(**.....?**, sep = "\t")
} else {
data <- read.csv(**.....?**, sep = ",")
}
}
else if (file_type == "xlsx") {
data <- read.xlsx(**.....?**, sheetIndex = 1)
}
else {
stop("Unsupported file type, please use a RDS, CSV, or XLS file.")
}
# Do some processing on dataset
...
# Return updated dataset to user
...
I tried to use code such as this to read the data from the dataset parameter.
Rook::Multipart$parse(dataset)$dataset$tempfile
mime::parse_multipart(dataset)
Unfortunately, nothing seems to work. I test with the 'Try it out' button in SwaggerUI. I receive errors such as these two.
<simpleError in readRDS(file): bad 'file' argument>
<simpleError in read.table(file = file, header = header, sep = sep, quote = quote, dec = dec, fill = fill, comment.char = comment.char, ...): 'file' must be a character string or connection>
Thanks!