将下载功能包装为data.frame

时间:2018-02-13 14:28:49

标签: r

我有一个功能:

session$files$download(path = "/user/ExampleData/my_file.csv",filename ="my_file.csv", overwrite = TRUE)

使用RESTful api从远程系统下载文件。而不是将文件下载到我的本地机器或使用HPC时,我只想将内容发送到另一个函数,如read_csv。该函数表示它可以采取其他参数:

download( path = , filename = , ...)

我可以添加或包装的功能的任何想法?我见过stdout()函数,但我要么不理解它,要么根本不理解它。

来自开发者文件的完整功能:

download = function(destSystemId, path, filename, overwrite=FALSE, ...){
  args <- list(...)
  queryParams <- list()
  headerParams <- character()

  if (missing(filename)) {
    filename <- basename(path)
  }

  if (missing(destSystemId)) {
    urlPath <- "/files/v2/media/{filePath}"
  }
  else {
    urlPath <- "/files/v2/media/system/{systemId}/{filePath}"
    urlPath <- gsub(paste0("\\{", "systemId", "\\}"), `destSystemId`, urlPath)
  }

  if (!missing(`path`)) {
    urlPath <- gsub(paste0("\\{", "filePath", "\\}"), `path`, urlPath)
  }

  resp <- private$apiClient$callApi(url = paste0(private$apiClient$basePath, urlPath),
                             method = "GET",
                             queryParams = queryParams,
                             headerParams = headerParams,
                             body = body,
                             httr::progress(),
                             httr::write_disk(filename, overwrite=overwrite),
                             ...)

  if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
    normalizePath(filename)
  } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
    logger.warn(jsonlite::toJSON(httr::content(resp), auto_unbox=TRUE, null="null", na="null"))
    httr::content(resp)
  } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
    logger.warn(jsonlite::toJSON(httr::content(resp), auto_unbox=TRUE, null="null", na="null"))
    httr::content(resp)
  }
}

1 个答案:

答案 0 :(得分:1)

@MrFlick让我走上正轨:

myfile <- GET("https://raw.githubusercontent.com/dhbrand/rAgave/master/morphLabels.csv")

body <- content(myfile, as = "parsed", encoding = "UTF-8", type = "text/csv")

将文件作为数据框返回。