【发布时间】:2018-09-21 11:53:06
【问题描述】:
如何设置我的管道工 API 以提供可下载的文件?
例如,我想直接传递 rds 或 RData 对象,而不是将其序列化为 JSON。
【问题讨论】:
如何设置我的管道工 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)
【讨论】: