OpenCPU:没有方法作为 JSON S3 class

OpenCPU: No method asJSON S3 class

我正在尝试获取以下资源的 JSON 表示:

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

我得到的错误是 No method asJSON S3 class: smooth.spline

smooth.spline() 调用的结果具有以下结构:

    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"

有没有办法使用 OpenCPU 获取列表的 y 组件?

两种可能的方法。第一种是使用 two-step OpenCPU 程序,它允许您将参数传递给 toJSON,这样您就可以设置 force 参数。所以:

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

这将为您提供 Location 响应 header 中的密钥。你抓住那个,例如:

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

force 参数将自动 unclass/drop object 中 json 不支持的字段。

另一种方法是为 smooth.spline 编写一个简单的包装器并调用它。例如:

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

我会推荐第二种方法,因为 smooth.spline object 中似乎有很多内容对客户来说并不是很有趣,并且会产生不必要的开销。