How to chain two function calls in opencpu

主宰稳场 提交于 2020-06-26 04:24:10

问题


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

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"))

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

I need to calculate something like:

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

or

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

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 ?

Here is a complete example:

prepare package to test f(g(x)) here plotcars(dfcars()) in 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")

install the new package from the ubuntu terminal

 sudo R CMD INSTALL mypkg

Execute function chaining commands as in 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:


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'


来源:https://stackoverflow.com/questions/18551069/how-to-chain-two-function-calls-in-opencpu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!