r - Opencpu simple function json parsing not working -


hi want have simple function local opencpu development server.

getlinearinterpolatedestimatequantil <- function(x, type, probs){   result = quantile(data, type, probs)   result #print simpler debug } 

example debug input

{"type":1,"x":[1,2,3,4,5,6,7,8,9,10],"probs":[0.05,0.25,0.75,0.95]} 

but there problem: when supplied manually sample data like:

debuginput = fromjson(file("debuginput.json")) 

the code compiles.

but when try access 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 receive output:

    unused arguments (type = 1, probs = probs) in call: getlinearinterpolatedestimatequantil(type = 1l, x = x, probs = probs) 

so expect parsing has issues arrays?

i hope can tell me wrong code.

edit: learnt opencpu performs json parsing me. code still not work. (https://www.opencpu.org/posts/scoring-engine/) edit: still not working edit: strange: calling 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 own function results in 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 function again:

getlinearinterpolatedestimatequantil <- function(x){   result = quantile(data, type, probs)   return (result) } 

edit 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 , nan's not allowed if 'na.rm' 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 first call result in warning different output? why second call work?

for rcp -h "content-type: application/json" top level names in json object must match parameter names of function. can test 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 want stick json payload is, function should like:

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

for example call 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 arguments of json blob type probs , x match parameter names of quantile.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -