Hi I want to have a simple function for my local opencpu development server.
getLinearInterpolatedEstimateQuantil <- function(x, type, probs){
result = quantile(data, type, probs)
result #print for simpler debug
}
Example Debug input would be something like
{"type":1,"x":[1,2,3,4,5,6,7,8,9,10],"probs":[0.05,0.25,0.75,0.95]}
But there is a problem: when supplied manually with the sample data like:
debugInput = fromJSON(file("debugInput.json"))
the code compiles.
BUT when I try to access it via http like:
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"
I only receive as output:
unused arguments (type = 1, probs = probs)
In call:
getLinearInterpolatedEstimateQuantil(type = 1L, x = x, probs = probs)
So I expect that the parsing has some issues with the arrays?
I hope you can tell me what is wrong with my code.
Edit: I learnt that opencpu performs the json parsing for me. However the code still does not work. (https://www.opencpu.org/posts/scoring-engine/) Edit: still not working Edit: Strange: calling a native function works:
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"
however calling my own function results in an error:
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)
for clarification here my function again:
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)
Results in
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
And
do.call(stats::quantile, args)
Results in
5% 25% 75% 95%
1 3 8 10
Why does the first call result in a warning with different output? Why does the second call work?
For an RCP with -H "Content-Type: application/json"
the top level names in your JSON object must match the parameter names of your function. You can test this as follows:
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)
So assuming you want to stick with the JSON payload as it is, your function should look like:
getLinearInterpolatedEstimateQuantil <- function(data, quantil, type = 1){
}
For example to call the quantile
function directly:
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"
Note that the arguments of the json blob type
probs
and x
match the parameter names of quantile.