在管道工中读取本地 csv API
Read local csv in plumber API
我想使用 plumber 通过 R 读取和处理 csv 文件。
到目前为止,我在 GitHub (though regarding binary files) and SO 答案(转换 JSON 文件)上发现了关于管道工文件上传的持续讨论,两者都假设使用 postBody
.
因此我从这个端点开始:
library(plumber)
#* parse csv file
#* @param req the request object
#* @post /file
function(req) {
result <- req$postBody
return(result)
})
使用 httr
测试端点时,我可以将文件读取为 JSON 列表,但无法在下一步处理数据。
upload_csv <- httr::upload_file("file.csv")
resp <- httr::POST(
url = url,
path = "echo",
body = upload_csv
)
httr::content(resp)
此外,测试端点会产生两个警告
Warning in if (stri_startswith_fixed(body, "{")) { :
the condition has length > 1 and only the first element will be used
Warning in if (stri_startswith_fixed(qs, "?")) { :
the condition has length > 1 and only the first element will be used
您可以为此使用 Rook
包
library(plumber)
library(Rook)
#* parse csv file
#* @param req the request object
#* @post /file
function(req) {
file <- Rook::Multipart$parse(req)$req$tempfile
result <- read.csv(file)
result
}
使用当前 CRAN 版本的软件包时,警告仍然存在,但在 github 版本中已删除(有关详细信息,请参阅 here)。
我想使用 plumber 通过 R 读取和处理 csv 文件。
到目前为止,我在 GitHub (though regarding binary files) and postBody
.
因此我从这个端点开始:
library(plumber)
#* parse csv file
#* @param req the request object
#* @post /file
function(req) {
result <- req$postBody
return(result)
})
使用 httr
测试端点时,我可以将文件读取为 JSON 列表,但无法在下一步处理数据。
upload_csv <- httr::upload_file("file.csv")
resp <- httr::POST(
url = url,
path = "echo",
body = upload_csv
)
httr::content(resp)
此外,测试端点会产生两个警告
Warning in if (stri_startswith_fixed(body, "{")) { :
the condition has length > 1 and only the first element will be used
Warning in if (stri_startswith_fixed(qs, "?")) { :
the condition has length > 1 and only the first element will be used
您可以为此使用 Rook
包
library(plumber)
library(Rook)
#* parse csv file
#* @param req the request object
#* @post /file
function(req) {
file <- Rook::Multipart$parse(req)$req$tempfile
result <- read.csv(file)
result
}
使用当前 CRAN 版本的软件包时,警告仍然存在,但在 github 版本中已删除(有关详细信息,请参阅 here)。