【问题标题】:Serve downloadable files with plumber使用管道工提供可下载的文件
【发布时间】:2018-09-21 11:53:06
【问题描述】:

如何设置我的管道工 API 以提供可下载的文件?

例如,我想直接传递 rdsRData 对象,而不是将其序列化为 JSON。

【问题讨论】:

    标签: r plumber


    【解决方案1】:

    使用正确的序列化器很重要:

    # 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 2019-11-20
      • 2012-06-11
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 2017-12-29
      相关资源
      最近更新 更多