如何在opencpu中链接两个函数调用

时间:2013-08-31 17:45:43

标签: r opencpu

据说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或hash键作为对象参数给出时,这似乎不起作用。如何解决两个非泛型函数的链接?

这是一个完整的例子:

准备包测试f(g(x))这里的情节车(dfcars())在R

  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 docs

中的函数链接命令
 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)

1 个答案:

答案 0 :(得分:1)

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

curl http://localhost/ocpu/library/mypkg/R/plotcars -d 'df=x07a1f83f'