httr code:
library(httr)
library(magrittr)
res <-
POST(
"http://0.0.0.0:7000/test/1",
body = list(
# send the file with mime type `"application/rds"` so the RDS parser is used
rds_file = upload_file("data.rds", "application/rds")
)
) %>%
content()
str(res)
#> List of 1
#> $ : logi TRUE
plumber.R code:
# Add a rds and multipart form parser
#' @parser rds
#' @parser multi
#' @post /test/1
function(req, res, rds_file) {
str(rds_file)
# now that you have the object, save it to a file
saveRDS(rds_file, "We_Got_RDS.rds")
TRUE # placeholder return value
}
library(plumber)
pr("plumber_rds.R") %>% pr_run(port = 7000)
#> Running plumber API at http://127.0.0.1:7000
#> Running swagger Docs at http://127.0.0.1:7000/__docs__/
#> List of 1
#> $ data.rds:'data.frame': 150 obs. of 5 variables:
#> ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#> ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#> ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#> ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#> ..$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
You can also access the already processed body information via req$body in your plumber route.
Yes, this approach involves reading the data and saving it, but it is a less complicated solution.
Update: Removed comments within plumber block