我正在尝试获取以下资源的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
组件?
答案 0 :(得分:1)
两种可能的方法。第一步是使用两步OpenCPU过程,允许您将参数传递给toJSON
,以便设置force
参数。所以:
POST http://myserver/ocpu/library/stats/R/smooth.spline
这将为您提供Location
响应标头中的密钥。你抓住它,例如:
GET http://myserver/ocpu/tmp/x123456789/R/.val/json?force=true
force
参数将自动从对象中取消/删除json中不支持的字段。
另一种方法是为smooth.spline
编写一个简单的包装器并调用它。例如:
mysmooth <- function(...){
obj <- smooth.spline(...)
obj[c("x", "y", "yin")]
}
我会推荐第二种方法,因为smooth.spline
对象中似乎有很多东西对客户端来说并不是很有趣,并且会产生不必要的开销。