gpt4 book ai didi

r - Opencpu 简单函数 json 解析不起作用

转载 作者:行者123 更新时间:2023-12-04 11:35:47 25 4
gpt4 key购买 nike

您好,我想为我的本地 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/ )编辑:仍然无法正常工作编辑:奇怪的:调用 native 函数有效:

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

为什么第一次调用会产生不同输出的警告?为什么第二次调用有效?

最佳答案

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

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 与分位数的参数名称匹配。

关于r - Opencpu 简单函数 json 解析不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30604770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com