3

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)
Community
  • 1
  • 1
mike
  • 45
  • 5
  • It should work. Could you post an example of your `mycalc` function? Are you accessing it through github? – Jeroen Ooms Sep 01 '13 at 13:52
  • I just added an example as Jeroen asked. I didn't access through github as will be seen from the example I installed opencpu 1.0 on an ubuntu 12.04 in virtualbox on my MacAir – mike Sep 01 '13 at 17:08
  • Don't forget to mark the question as answered by clicking the checkbox next to the answer. – Jeroen Ooms Sep 03 '13 at 17:06

1 Answers1

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'
Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207
  • Thanks Jeroen, you are right. This works and proves that your opencpu is great. I took the example in the opencpu docs too literally and thought that the object argument was some kind of generic argument for the second function … – mike Sep 02 '13 at 04:45
  • Hmm I'll try to add an example to the docs that is less confusing then. – Jeroen Ooms Sep 02 '13 at 09:36