Hello All,
I am working on writing an API using Plumber. I have a situation where I want to return say a results JSON object and RData together in a single API call.
library(plumber)
library(jsonlite)
#* @get /healthCheck
#* @serializer contentType list(type="text/plain; charset=UTF-8")
function(res) {
res$status <- 200
return("Ok")
}
#* @param data:file Input plain text file
#* @post /doJob
#* @serializer contentType list(type = "application/json")
function(data) {
all_data <- read.table(header = TRUE,
sep = "\t",
text = as.character(data))
avg <- mean(all_data[, 2])
sdv <- sd(all_data[, 2])
out_data <- list(avg = avg,
sdv = sdv)
# Save RData
tmp <- tempfile(fileext = ".RData")
on.exit({
if (file.exists(tmp)) {
unlink(tmp)
}
}, add = TRUE)
save(list = ls(all.names = TRUE), file = tmp, envir = environment())
bin <- readBin(tmp, "raw", n = file.info(tmp)$size)
return(jsonlite::toJSON(out_data))
}
See above for an example. An example input file will be a text file with numeric values in the second column.
df <- data.frame(x = c(10, 100, 1000),
y = c(12.8, 15.3, 22.1))
write.table(df, "test_data.txt", row.names = FALSE)
Running the following request:
curl -X POST "http://127.0.0.1:8000/doJob" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "data=@test_data.txt;type=text/plain"
Returns:
{
"avg": [
16.73333
],
"sdv": [
4.81283
]
}
However, I would like to retrieve RData in the same response, if possible. Wondering whether someone have any ideas how this can be achieved. Thanks in advance and let me know if the question isn't clear enough.
Cheers!