3

据说 OpenCPU 支持链接函数调用来计算例如 f(g(x), h(y))

有关参数格式的文档:https ://public.opencpu.org/api.html#api-arguments包含一个示例,该示例通过计算来说明这一点

summary(read.csv("mydata.csv"))

在此示例中, f 是通用函数摘要,它以对象为参数。

我需要计算类似的东西:

mycalc(read.csv("mydata.csv")) 

或者

myplot(read.csv("my data.csv"))

其中 f 将数据框作为参数。当将 read.csv 函数返回的 sessionid 或哈希键作为对象参数给出时,这似乎不起作用。如何解决两个非泛型函数的链接?

这是一个完整的例子:

准备包以在 R 中测试 f(g(x)) 此处 plotcars(dfcars())

  dfcars<-function(){
   data(cars);
   cars
  }

 plotcars<-function(df){
  matplot(1:nrow(df),df)
 }

 plotcars(dfcars()) # test the two chained functions are working


 package.skeleton(list = c("dfcars", "plotcars"), name = "mypkg")

从 ubuntu 终端安装新软件包

 sudo R CMD INSTALL mypkg

执行 opencpu 文档中的函数链接命令

 curl http://localhost/ocpu/library/mypkg/R/dfcars -d ""

 /ocpu/tmp/x07a1f83f/R/.val
 /ocpu/tmp/x07a1f83f/stdout
 /ocpu/tmp/x07a1f83f/source
 /ocpu/tmp/x07a1f83f/console
 /ocpu/tmp/x07a1f83f/info

'#replace session id with returned one above

  curl http://localhost/ocpu/tmp/x07a1f83f/R/.val/print

   speed dist
 1      4    2
 2      4   10
 3      7    4

'# POST chaining with the generic function summary works
 curl http://localhost/ocpu/library/base/R/summary -d 'object=x07a1f83f'
 /ocpu/tmp/x0e29fd5c/R/.val
 /ocpu/tmp/x0e29fd5c/stdout
 /ocpu/tmp/x0e29fd5c/source
 /ocpu/tmp/x0e29fd5c/console
 /ocpu/tmp/x0e29fd5c/info

# and the summary gets printed
 curl http://localhost/ocpu/tmp/x0e29fd5c/R/.val/print
  speed           dist       
 Min.   : 4.0   Min.   :  2.00  
 1st Qu.:12.0   1st Qu.: 26.00  
 Median :15.0   Median : 36.00  
 Mean   :15.4   Mean   : 42.98  
 3rd Qu.:19.0   3rd Qu.: 56.00  
 Max.   :25.0   Max.   :120.00  

# POST chaining with the nongeneric function plotcars doesn't work
curl http://localhost/ocpu/library/mypkg/R/plotcars -d 'object=x07a1f83f'
unused argument (object = object)

In call:
plotcars(object = object)
4

1 回答 1

1

从示例中,您似乎正在传递名为的参数object,而您的函数有一个名为df? 对函数执行 POST 会将 http 请求的参数映射到函数参数。因此,您当前所做的是plotcars(object=dfcars())导致您看到的错误。尝试:

curl http://localhost/ocpu/library/mypkg/R/plotcars -d 'df=x07a1f83f'
于 2013-09-02T02:44:31.297 回答