【发布时间】:2015-08-16 18:12:15
【问题描述】:
您好,我想为我的本地 opencpu 开发服务器提供一个简单的功能。
getLinearInterpolatedEstimateQuantil <- function(x, type, probs){
result = quantile(data, type, probs)
result #print for simpler debug
}
示例调试输入类似于
{"type":1,"x":[1,2,3,4,5,6,7,8,9,10],"probs":[0.05,0.25,0.75,0.95]}
但是有一个问题:当手动提供示例数据时:
debugInput = fromJSON(file("debugInput.json"))
代码编译。
但是当我尝试通过 http 访问它时:
opencpu$browse("library/myLibrary")
curl http://localhost:3469/ocpu/library/predictR/R/getLinearInterpolatedEstimateQuantil/Json -d '{"type":1,"x":[1,2,3,4,5,6,7,8,9,10],"probs":[0.05,0.25,0.75,0.95]}
' -H "Content-Type: application/json"
我只收到输出:
unused arguments (type = 1, probs = probs)
In call:
getLinearInterpolatedEstimateQuantil(type = 1L, x = x, probs = probs)
所以我认为解析对数组有一些问题?
希望你能告诉我我的代码有什么问题。
编辑:我了解到 opencpu 为我执行 json 解析。但是代码仍然不起作用。 (https://www.opencpu.org/posts/scoring-engine/) 编辑:仍然无法正常工作 编辑: 奇怪的: 调用本机函数有效:
curl http://localhost:5112/ocpu/library/stats/R/quantile/json -d '{"type":1,"x":[1,2,3,4,5,6,7,8,9,10],"probs":[0.05,0.25,0.75,0.95]}' -H "Content-Type: application/json"
但是调用我自己的函数会导致错误:
curl http://localhost:5112/ocpu/library/predictR/R/getLinearInterpolatedEstimateQuantil/json -d '{"type":1,"x":[1,2,3,4,5,6,7,8,9,10],"probs":[0.05,0.25,0.75,0.95]}' -H "Content-Type: application/json"
unused arguments (type = 1, probs = probs)
In call:
getLinearInterpolatedEstimateQuantil(type = 1L, x = x, probs = probs)
再次澄清我的功能:
getLinearInterpolatedEstimateQuantil <- function(x){
result = quantile(data, type, probs)
return (result)
}
再次编辑:
library(jsonlite)
myFunction <- function(x, type, probs){
result = quantile(x, type, probs)
return (result)
}
json <- '{"type":1,"x":[1,2,3,4,5,6,7,8,9,10],"probs":[0.05,0.25,0.75,0.95]}'
args <- fromJSON(json)
do.call(myFunction, args)
结果
100%
10
Warning message:
In if (na.rm) x <- x[!is.na(x)] else if (anyNA(x)) stop("missing values and NaN's not allowed if 'na.rm' is FALSE") :
Bedingung hat Länge > 1 und nur das erste Element wird benutzt
还有
do.call(stats::quantile, args)
结果
5% 25% 75% 95%
1 3 8 10
为什么第一次调用会导致输出不同的警告? 为什么第二次调用有效?
【问题讨论】:
-
一目了然:您定义了一个函数(x),但您的 JSON 也有
type和probs;与错误消息相匹配。你不应该声明这些参数吗?此外,您的函数目前忽略了x,但使用了我们以前从未见过的data。 -
差不多了:这是本机调用 [1, 3, 8, 10] 的输出,这是我的函数 [10] 的输出