R 与管道工一起提供可下载的文件

R 与管道工一起提供可下载的文件,r,plumber,R,Plumber,我如何设置我的管道工API,使其提供可下载的文件 例如,我希望直接传递rds或RData对象,而不是将其序列化为JSON。使用正确的序列化程序非常重要: # If the URL gets called the browser will automatically download the file. #' @serializer contentType list(type="application/octet-stream") #' @get /rds rest_rds = function(

我如何设置我的管道工API,使其提供可下载的文件


例如,我希望直接传递
rds
RData
对象,而不是将其序列化为JSON。

使用正确的序列化程序非常重要:

# If the URL gets called the browser will automatically download the file.
#' @serializer contentType list(type="application/octet-stream")
#' @get /rds
rest_rds = function() {
  tfile = tempfile()
  saveRDS(iris, file = tfile)
  readBin(tfile, "raw", n = file.info(tfile)$size)
}
提供此管道工脚本后,您可以下载此对象并在单独的R会话中导入它,如下所示:

tfile = tempfile()
download.file("http://127.0.0.1:7983/rds", destfile = tfile)
d_iris = readRDS(tfile)