Search code examples
ropencpujsonlite

OpenCPU: No method asJSON S3 class


I am trying to get the JSON representation of the following resource:

POST http://myserver/ocpu/library/stats/R/smooth.spline/json

The error I get is No method asJSON S3 class: smooth.spline.

The result of a smooth.spline() call has the following structure:

    List of 15
 $ x       : num [1:11] 1 2 3 4 5 6 7 8 9 10 ...
 $ y       : num [1:11] 2.55 2.98 3.42 3.85 4.29 ...
 $ w       : num [1:11] 1 1 1 1 1 1 1 1 1 1 ...
 $ yin     : num [1:11] 1 4 3 5 3 6 8 5 3 6 ...
 $ data    :List of 3
  ..$ x: num [1:11] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ y: num [1:11] 1 4 3 5 3 6 8 5 3 6 ...
  ..$ w: num [1:11] 1 1 1 1 1 1 1 1 1 1 ...
 $ lev     : num [1:11] 0.318 0.236 0.173 0.127 0.1 ...
 $ cv.crit : num 3.7
 $ pen.crit: num 27.2
 $ crit    : num 3.7
 $ df      : num 2
 $ spar    : num 1.49
 $ lambda  : num 40679
 $ iparms  : Named int [1:3] 1 0 28
  ..- attr(*, "names")= chr [1:3] "icrit" "ispar" "iter"
 $ fit     :List of 5
  ..$ knot : num [1:17] 0 0 0 0 0.1 0.2 0.3 0.4 0.5 0.6 ...
  ..$ nk   : int 13
  ..$ min  : num 1
  ..$ range: num 10
  ..$ coef : num [1:13] 2.55 2.69 2.98 3.42 3.85 ...
  ..- attr(*, "class")= chr "smooth.spline.fit"
 $ call    : language smooth.spline(x = x)
 - attr(*, "class")= chr "smooth.spline"

Is there a way to get the y component of the list using OpenCPU?


Solution

  • Two possible approaches. The first is to use the two-step OpenCPU procedure which allows you to pass arguments to toJSON so you can set the force argument. So:

    POST http://myserver/ocpu/library/stats/R/smooth.spline
    

    This will give you the key in the Location response header. You grab that, for example:

    GET http://myserver/ocpu/tmp/x123456789/R/.val/json?force=true
    

    The force argument will automatically unclass/drop fields from the object that are not supported in json.

    The other approach is to write a simple wrapper for smooth.spline and call that. For example:

    mysmooth <- function(...){
      obj <- smooth.spline(...)
      obj[c("x", "y", "yin")]
    }
    

    I would recommend the second approach because there seems to be a lot of stuff in the smooth.spline object that is not really interesting to the client, and will create unnecessary overhead.