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

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

本文介绍了如何在opencpu中链接两个函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

OpenCPU is said to support chaining of function calls to calculate e.g. f(g(x), h(y))

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

The docs about argument formats: https://public.opencpu.org/api.html#api-arguments includes an example that illustrates this by calculating

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

在此示例中,f是将对象作为参数的泛型函数摘要.

In this example f is the generic function summary that takes as an argument an object.

我需要计算如下内容:

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

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

其中f以数据帧作为参数.当提供read.csv函数返回的sessionid或hash键作为对象参数时,这似乎不起作用.如何解决这两个非泛型函数的链接?

where f takes as an argument a dataframe. This doesn't seem to work when giving as object argument the sessionid or hash key returned by the read.csv function. How can this chaining of two nongeneric functions be solved ?

  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)

推荐答案

从该示例看来,您正在传递名为object的参数,而您的函数具有名为df的参数?对函数执行POST将把http请求的参数映射到函数参数.因此,您当前正在执行的是plotcars(object=dfcars()),这会导致您看到错误.试试:

From the example it seems you are passing the argument named object whereas your function has an argument named df? Performing a POST on a function will map the arguments of the http request to the function parameters. So what you are doing currently is plotcars(object=dfcars()) which results in the error that you see. Try:

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

这篇关于如何在opencpu中链接两个函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:30