Illustrating Interaction with OpenCPU


Remotely Calling Procedures on HTTP

In [81]:
import json
import requests
In [82]:
v = json.dumps([1,2,3,4,5])
v
Out[82]:
'[1, 2, 3, 4, 5]'
In [83]:
r = requests.post("http://public.opencpu.org/ocpu/library/base/R/mean", data={"x":v})
In [84]:
print r.content
/ocpu/tmp/x0984badaa5/R/.val
/ocpu/tmp/x0984badaa5/stdout
/ocpu/tmp/x0984badaa5/source
/ocpu/tmp/x0984badaa5/console
/ocpu/tmp/x0984badaa5/info
/ocpu/tmp/x0984badaa5/files/DESCRIPTION

In [85]:
r = requests.get("http://public.opencpu.org/ocpu/tmp/x0984badaa5/R/.val")
In [86]:
print r.content
[1] 3

In [87]:
r_json = requests.post("http://public.opencpu.org/ocpu/library/base/R/mean/json", data={"x":v})
In [88]:
print r_json.content
[
    3
]


In [89]:
r = requests.post("http://public.opencpu.org/ocpu/library/base/R/mean", data={"x":v})

for path in r.content.split():
    print "\n--------------------\n" + path + " :\n\n" \
        + requests.get("http://public.opencpu.org{}".format(path)).content
--------------------
/ocpu/tmp/x0984badaa5/R/.val :

[1] 3


--------------------
/ocpu/tmp/x0984badaa5/stdout :

[1] 3


--------------------
/ocpu/tmp/x0984badaa5/source :

mean(x = x)

--------------------
/ocpu/tmp/x0984badaa5/console :

> mean(x = x)
[1] 3

--------------------
/ocpu/tmp/x0984badaa5/info :

R version 3.1.2 (2014-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8    LC_NUMERIC=C            LC_TIME=en_US.UTF-8    
 [4] LC_COLLATE=en_US.UTF-8  LC_MONETARY=en_US.UTF-8 LC_MESSAGES=C          
 [7] LC_PAPER=C              LC_NAME=C               LC_ADDRESS=C           
[10] LC_TELEPHONE=C          LC_MEASUREMENT=C        LC_IDENTIFICATION=C    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] opencpu_1.4.6

loaded via a namespace (and not attached):
 [1] base64enc_0.1-2    brew_1.0-6         colorspace_1.2-4   devtools_1.6.1    
 [5] digest_0.6.7       evaluate_0.5.5     formatR_1.0        ggplot2_1.0.0     
 [9] grid_3.1.2         gtable_0.1.2       httpuv_99.999      httr_0.6.0        
[13] jsonlite_0.9.14    knitr_1.8          lattice_0.20-29    MASS_7.3-35       
[17] Matrix_1.1-4       mgcv_1.8-4         munsell_0.4.2      nlme_3.1-118      
[21] openssl_0.2        parallel_3.1.2     plyr_1.8.1         proto_0.3-10      
[25] RAppArmor_1.0.1.99 Rcpp_0.11.3        reshape2_1.4.1     scales_0.2.4      
[29] sendmailR_1.2-1    stringr_0.6.2      tools_3.1.2        unixtools_0.1-1   


--------------------
/ocpu/tmp/x0984badaa5/files/DESCRIPTION :

Package: x0984badaa5
Type: Session
Version: 1.0
Author: OpenCPU
Date: 2015-02-15
Description: This file is automatically generated by OpenCPU.


Chained RPC with Graphical Output

In [90]:
r = requests.post("http://public.opencpu.org/ocpu/library/stats/R/lm", 
        data={"formula":"speed~dist","data":"cars"})
In [91]:
print r.content
/ocpu/tmp/x00fbf1aa4e/R/.val
/ocpu/tmp/x00fbf1aa4e/stdout
/ocpu/tmp/x00fbf1aa4e/source
/ocpu/tmp/x00fbf1aa4e/console
/ocpu/tmp/x00fbf1aa4e/info
/ocpu/tmp/x00fbf1aa4e/files/DESCRIPTION

In [92]:
res = requests.get("http://public.opencpu.org/ocpu/tmp/x00fbf1aa4e/R/.val")
In [93]:
print res.content
Call:
lm(formula = speed ~ dist, data = cars)

Coefficients:
(Intercept)         dist  
     8.2839       0.1656  


In [94]:
res = requests.post("http://public.opencpu.org/ocpu/library/base/R/get/json", 
    data={"x":"'coefficients'","pos":"x00fbf1aa4e"})
In [95]:
print res.content
[
    8.2839,
    0.1656
]


In [96]:
req = requests.post("http://public.opencpu.org/ocpu/library/graphics/R/plot", 
                    data={"x":"x00fbf1aa4e"})
In [97]:
print req.content
/ocpu/tmp/x08b1e1b4b2/R/.val
/ocpu/tmp/x08b1e1b4b2/graphics/1
/ocpu/tmp/x08b1e1b4b2/graphics/2
/ocpu/tmp/x08b1e1b4b2/graphics/3
/ocpu/tmp/x08b1e1b4b2/graphics/4
/ocpu/tmp/x08b1e1b4b2/source
/ocpu/tmp/x08b1e1b4b2/console
/ocpu/tmp/x08b1e1b4b2/info
/ocpu/tmp/x08b1e1b4b2/files/DESCRIPTION

In [98]:
from IPython.display import Image
In [99]:
Image(url='http://public.opencpu.org/ocpu/tmp/x0f64e80a9b/graphics/1')
Out[99]:
In [100]:
Image(url='http://public.opencpu.org/ocpu/tmp/x0f64e80a9b/graphics/2/png?width=300&height=300')
Out[100]: