Opencpu simple json parsing function not working
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 look 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 submitting manually with sample data, for example:
debugInput = fromJSON(file("debugInput.json"))
the code is compiled.
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 get output:
unused arguments (type = 1, probs = probs)
In call:
getLinearInterpolatedEstimateQuantil(type = 1L, x = x, probs = probs)
So, I expect the parsing to have some problems with arrays?
Hope you can tell me what is wrong with my code.
Edit: I found out that opencpu does json parsing for me. However, the code still doesn't work. ( https://www.opencpu.org/posts/scoring-engine/ ) Edit: Still doesn't work Edit: Weird: Calling a function of a native function:
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's my function again:
getLinearInterpolatedEstimateQuantil <- function(x){
result = quantile(data, type, probs)
return (result)
}
EDIT once again:
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 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 a different output? Why does the second call work?
source to share
For RCP with, the -H "Content-Type: application/json"
top level names in your JSON object must match the parameter names of your function. You can check it like this:
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 use the JSON payload as is, your function should look like this:
getLinearInterpolatedEstimateQuantil <- function(data, quantil, type = 1){
}
For example, to call a function directly 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"
Note that the json blob type
probs
and arguments x
match the names of the quantile parameters.
source to share