r - OpenCPU:没有方法 asJSON S3 类

标签 r opencpu jsonlite

我正在尝试获取以下资源的 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 组件?

最佳答案

两种可能的方法。第一种方法是使用两步 OpenCPU 过程,该过程允许您将参数传递给 toJSON,以便您可以设置 force 参数。所以:

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

这将为您提供 Location 响应 header 中的 key 。例如,您捕获它:

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 对象中似乎有很多东西对客户端来说并不真正有趣,并且会产生不必要的开销。

关于r - OpenCPU:没有方法 asJSON S3 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28372132/

相关文章:

r - 非典型数据格式从长转换为宽

c++ - R 中 %in% 运算符的 C++ 版本

r - 如何在 CentOS6 上启动/停止/刷新 OpenCPU?

opencpu - OpenCPU 中长时间运行作业的任务模型?

json - 将Json文件读取到没有嵌套列表的data.frame中

r - 从 R 中的列表创建带有括号的 Json

arrays - R 与 MATLAB 中高维数据结构化的方法论

r - 在 R 中向 `summary(aov)` 的输出添加一列?

python - 无法解析的参数 : 5PL getting this error while trying to post the file through pycurl in python

r - 使用 jsonlite 在 R 中进行 Web 抓取