3

嗨,我想为我的本地 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 "内容类型:应用程序/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 

为什么第一次调用会导致输出不同的警告?为什么第二个电话有效?

4

1 回答 1

1

对于-H "Content-Type: application/json"JSON 对象中具有顶级名称的 RCP,必须与函数的参数名称匹配。您可以按如下方式进行测试:

library(jsonlite)
json <- '{"type":1,"data":[1,2,3,4,5,6,7,8,9,10],"quantil":[0.05,0.25,0.75,0.95]}'
args <- fromJSON(json)
result <- do.call(getLinearInterpolatedEstimateQuantil, args)

因此,假设您想按原样坚持使用 JSON 有效负载,您的函数应如下所示:

getLinearInterpolatedEstimateQuantil <- function(data, quantil, type = 1){

}

例如quantile直接调用函数:

curl http://public.opencpu.org/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"

请注意,json blob的参数type probsx分位数的参数名称匹配。

于 2015-06-02T20:44:16.317 回答