按照http://plumber.trestletech.com/页面上的示例
我写了myfile.R as
#* @post /test
test <- function(){
list(speech='aa',source='bb',displayText='cc')
}
Run Code Online (Sandbox Code Playgroud)
我在其上运行了管道工代码,将int转换为API
library(plumber)
r <- plumb("~/Work/myfile.R")
r$run(port=8000)
Run Code Online (Sandbox Code Playgroud)
现在当我使用我得到它的时候对它执行POST请求
curl -XPOST 'localhost:8000/test
-> {"speech":["aa"],"source":["bb"],"displayText":["cc"]}
Run Code Online (Sandbox Code Playgroud)
但我希望删除方括号.在简单的toJSON调用中,可以使用auto_unbox = TRUE来完成,但是如何在管道工中完成.如何编写自定义序列化程序并在上面的代码中使用它?
我是R管道工的新手,这是一个REST服务器,可以将R函数公开为其余的API.
我会问以下问题:
如何设置我的管道工 API 以便它提供可下载文件?
例如,我想直接传递一个rdsorRData对象,而不是将其序列化为 JSON。
平台: - 具有 16 个内核和 128 GIG RAM 的 AWS 实例。- 红帽企业 7.5。- R. - RStudio 服务器。- Plumber API 将 R 函数公开为 Web 服务端点。- 客户端是 Excel VBA。
问题: - 具有不同类型列的数据表,包括双精度、整数和字符串数据。- 就在 R 端点函数发送响应(表)之前,当我检查数据表中的双精度数据时,所有条目的长度都在 6 到 10 位小数位之间。- 表格以 JSON 格式到达客户端后,99% 的双列将四舍五入到小数点后 4 位。
知道可能是什么问题 - 为什么双打会四舍五入,四舍五入在哪里发生,我该如何防止?- 我尝试了不同的请求标头设置,但没有奏效。- 我试图将受影响的双列作为向量或列表发送,但我得到了相同的“强制”舍入。
提前致谢
我们如何从管道工 R API 返回特定的错误代码?
我们希望返回带有消息的 400 状态代码,例如“功能 x 丢失。”。我们阅读了管道工文档,但无法弄清楚这一点。
如果我们使用stop()500 内部服务器错误,则返回且没有具体消息。
#' predict
#' @param x Input feature
#'
#' @get /predict
function(x) {
if (is.na(x)) {
# error code 400 should be returned
}
# ... # else continue with prediction
}
Run Code Online (Sandbox Code Playgroud) 我需要使用以下格式的水暖工包从R发送响应
{
"status": "SUCCESS",
"code": "200",
"output": {
"studentid": "1001",
"name": "Kevin"
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到以下格式
[
"{\n \"status\": \"SUCCESS\",\n \"code\": \"200\",\n \"output\": {\n \"studentid\": \"1001\",\n \"name\": \"Kevin\"\n }\n}"
]
Run Code Online (Sandbox Code Playgroud)
请帮助我正确格式化此json
我的密码
#* @post /sum
addTwo <- function(){
library(jsonlite)
x <- list(status = "SUCCESS", code = "200",output = list(studentid = "1001", name = "Kevin"))
output<-toJSON(x,pretty = TRUE, auto_unbox = TRUE)
return (output)
}
Run Code Online (Sandbox Code Playgroud) 如果我有一个保存的图像文件,如何通过水管工为他们提供服务?
例如,这没有问题
# save this as testPlumb.R
library(magrittr)
library(ggplot2)
#* @get /test1
#* @png
test1 = function(){
p = data.frame(x=1,y= 1) %>% ggplot(aes(x=x,y=y)) + geom_point()
print(p)
}
Run Code Online (Sandbox Code Playgroud)
并运行
plum = plumber::plumb('testPlumb.R')
plum$run(port=8000)
Run Code Online (Sandbox Code Playgroud)
如果您访问http:// localhost:8000 / test1,则会看到正在提供的地块。
但是我找不到找到图像文件的方法
#* @get /test2
#* @png
test2 = function(){
p = data.frame(x=1,y= 1) %>% ggplot(aes(x=x,y=y)) + geom_point()
ggsave('file.png',p)
# code that modifies that file a little that doesn't matter here
# code that'll help me serve the file
}
Run Code Online (Sandbox Code Playgroud)
代替code that'll …
我已经检查了GitHub Repo和doc,但仍然无法弄清楚如何在 Plumber 中获取客户端 IP。
这是我尝试的实现,我想将所有请求的 IP 地址添加到日志文件中,
#' @post /v1/rl
rl_v1 <- function(a, b, c){
request='rl'
start_time <- as.numeric(as.POSIXct(Sys.time()))
log_record <- paste(NULL, Sys.time(), request, "requested", NULL, NULL,
sep=",")
cat(paste(log_record, "\n", sep=""), file=log_file_name, append=T)
lhs <- data.frame(a=unlist(a),
b=unlist(b),
c=unlist(c))
pairs <- custom_function(lhs, rhs, m_w = 0.98,
ext_blk_field=c(12), international=T,
fasterWcoBlock=T, preprocessedData2=T)
input_records=nrow(lhs)
matches=nrow(pairs)
query_time <- as.numeric(as.POSIXct(Sys.time())) - start_time
status <- data.frame(query_time=query_time,
request=request,
type='POST',
api_version=api_version_v1)
log_record <- paste(NULL, Sys.time(), request, "responded",
round(matches/input_records*100, 2),
paste0(matches, '/', input_records, ' in …Run Code Online (Sandbox Code Playgroud)