-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.html
972 lines (834 loc) · 33.3 KB
/
api.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
---
layout: page
title: "API Docs"
description: The official OpenCPU API documentation.
permalink: /api.html
---
<div class="row">
<div class="col-md-3">
<div id="docmenu" class="bs-sidebar hidden-print" data-spy="affix" data-offset-top="150" role="complementary">
<ul class="nav bs-sidenav">
<li><a class="active" href="#api-http">HTTP</a>
<ul class="nav">
<li><a href="#api-root">Root Path</a></li>
<li><a href="#api-info">Server Info</a></li>
<li><a href="#api-methods">Methods</a></li>
<li><a href="#api-status">Status Codes</a></li>
</ul>
</li>
<li><a href="#api-endpoints">API Endpoints</a>
<ul class="nav">
<li><a href="#api-libraries">Libraries</a></li>
<li><a href="#api-package">R Package API</a></li>
<li><a href="#api-object">R Object API</a></li>
<li><a href="#api-session">R Session API</a></li>
</ul>
</li>
<li><a href="#api-ioformats">I/O Formats</a>
<ul class="nav">
<li><a href="#api-formats">Output Formats</a></li>
<li><a href="#api-arguments">Argument Formats</a></li>
<li><a href="#api-scripts">Running Scripts</a></li>
<li><a href="#api-json">JSON RPC</a></li>
</ul>
</li>
<li><a href="#api-misc">Miscellaneous</a>
<ul class="nav">
<li><a href="#api-apps">OpenCPU Apps</a></li>
<li><a href="#api-ci">Github CI Hook</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="col-md-9" role="main">
<div class="alert alert-success">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>About this page</strong> Many of the sections below contain <code>curl</code> examples. Note that curl is used for illustration only, any HTTP client will do. Alternatively, the <a href="https://cloud.opencpu.org/ocpu/test"><code>/ocpu/test</code></a> page can be used to explore the API.
</div>
<div class="page-header" id="api-http">
<h1>HTTP in OpenCPU</h1>
</div>
<div class="panel panel-primary" id="api-root">
<div class="panel-heading"><h4>The OpenCPU root path</h4></div>
<div class="panel-body">
<p>The root of the API is dynamic. It defaults to <code>/ocpu/</code>, however system administrators can change this. Clients should make the OpenCPU server address and root path configurable. In the examples below we assume the default <code>/ocpu/</code>.</p>
</div>
</div>
<div class="panel panel-primary" id="api-info">
<div class="panel-heading"><h4>Debugging</h4></div>
<div class="panel-body">
<p>The <code>/ocpu/info</code> endpoint shows the output of <code>sessionInfo()</code> from the main (webserver) process. This can be helpful for debugging. The <code>/ocpu/test</code> URL gives you a handy testing web page to perform server requests. </p>
</div>
</div>
<div class="panel panel-primary" id="api-methods">
<div class="panel-heading"><h4>HTTP Methods</h4></div>
<div class="panel-body">
<p>OpenCPU currently only uses HTTP methods <code>GET</code> and <code>POST</code>. GET is used to retrieve a resource, and POST is used for RPC. A POST request is only valid targeting a script or function URL.</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Method</th>
<th>Target</th>
<th>Action</th>
<th>Arguments</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>GET</code></td>
<td>object</td>
<td>read object</td>
<td>control output format</td>
<td><code>GET /ocpu/library/MASS/R/cats/json</code></td>
</tr>
<tr>
<td><code>POST</code></td>
<td>object</td>
<td>call function</td>
<td>function arguments</td>
<td><code>POST /ocpu/library/stats/R/rnorm</code></td>
</tr>
<tr>
<td><code>GET</code></td>
<td>file</td>
<td>read file</td>
<td>-</td>
<td><code>GET /ocpu/library/MASS/NEWS</code> <br /> <code>GET /ocpu/library/MASS/scripts/</code></td>
</tr>
<tr>
<td><code>POST</code></td>
<td>file</td>
<td>run script</td>
<td>control interpreter </td>
<td><code>POST /ocpu/library/MASS/scripts/ch01.R</code> <br /><code>POST /ocpu/library/knitr/examples/knitr-minimal.Rmd</code></td>
</tr>
</tbody>
</table>
{% highlight bash %}
#curl uses http get method by default
curl https://cloud.opencpu.org/ocpu/library/MASS/data/Boston/json
curl https://cloud.opencpu.org/ocpu/library/MASS/NEWS
curl https://cloud.opencpu.org/ocpu/library/MASS/scripts/
#curl uses http post method for -X POST or -d "arg=value"
curl https://cloud.opencpu.org/ocpu/library/MASS/scripts/ch01.R -X POST
curl https://cloud.opencpu.org/ocpu/library/stats/R/rnorm -d "n=10&mean=5"
{% endhighlight %}
</div>
</div>
<div class="panel panel-primary" id="api-status">
<div class="panel-heading"><h4>HTTP Status Codes</h4></div>
<div class="panel-body">
<p>These are common statuscodes returned by OpenCPU that the client should be able to interpret</p>
<table class="table table-bordered">
<thead>
<tr>
<th>HTTP Code</th>
<th>When</th>
<th>Returns</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>200 OK</code></td>
<td>On successful GET request</td>
<td>Resource content</td>
</tr>
<tr>
<td><code>201 Created</code></td>
<td>On successful POST request</td>
<td>Output location</td>
</tr>
<tr>
<td><code>302 Found</code></td>
<td>Redirect</td>
<td>Redirect Location</td>
</tr>
<tr>
<td><code>400 Bad Request</code></td>
<td>R raised an error.</td>
<td>Error message in <code>text/plain</code></td>
</tr>
<tr>
<td><code>502 Bad Gateway</code></td>
<td>Nginx (opencpu-cache) can't connect to OpenCPU server.</td>
<td>(admin needs to look in error logs)</td>
</tr>
<tr>
<td><code>503 Bad Request</code></td>
<td>Serious problem with the server</td>
<td>(admin needs to look in error logs)</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="page-header" id="api-endpoints">
<h1>API Endpoints</h1>
</div>
<div class="panel panel-primary" id="api-libraries">
<div class="panel-heading"><h4>The API Libraries</h4></div>
<div class="panel-body">
<p>The following libraries hold packages and sessions that are available on the server</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Path</th>
<th>What</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>/ocpu/library/{pkgname}/</code></td>
<td>R packages installed in one of the global libraries on the server.</td>
</tr>
<tr>
<td><code>/ocpu/apps/{gituser}/{reponame}/</code></td>
<td>Interfaces to R package installed in the root of repository <code>{reponame}</code> from github user <code>{gituser}</code>.</td>
</tr>
<tr>
<td><code>/ocpu/user/{username}/library/{pkgname}/</code></td>
<td>R packages installed in the home library of Linux user <code>{username}</code>.</td>
</tr>
<tr>
<td><code>/ocpu/tmp/{key}/</code></td>
<td>Temporary sessions, which hold outputs from a function/script RPC.</td>
</tr>
</tbody>
</table>
{% highlight bash %}
#read packages
curl https://cloud.opencpu.org/ocpu/library/
curl https://cloud.opencpu.org/ocpu/apps/rwebapps/
curl https://cloud.opencpu.org/ocpu/user/jeroen/library/
#read session
curl https://cloud.opencpu.org/ocpu/tmp/x2c5ab8d4d6
{% endhighlight %}
</div>
</div>
<div class="panel panel-primary" id="api-package">
<div class="panel-heading"><h4>The R package API</h4></div>
<div class="panel-body">
<p>Any of the above <code>/{package}/</code> libraries support the following endpoints:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Path</th>
<th>What</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>../{pkgname}/info</code></td>
<td>Show information about this package.</td>
</tr>
<tr>
<td><code>../{pkgname}/www/</code></td>
<td>Apps (if any) included with the package</td>
</tr>
<tr>
<td><code>../{pkgname}/R/</code></td>
<td>R objects exported by this package. See R object API.</td>
</tr>
<tr>
<td><code>../{pkgname}/data/</code></td>
<td>Data included with this package. Datasets are objects, see R object API.</td>
</tr>
<tr>
<td><code>../{pkgname}/man/</code></td>
<td>Manuals (help pages) included in this package.</td>
</tr>
<tr>
<td><code>../{pkgname}/man/{topic}/{format}</code></td>
<td>Retrieve help page about <code>topic</code> in output format <code>format</code>. Manual format must be one of <code>text</code>, <code>html</code>, <code>pdf</code></td>
</tr>
<tr>
<td><code>../{pkgname}/html</code></td>
<td>Simulates the R-base html help pages (for backward compatibility).</td>
</tr>
<tr>
<td><code>../{pkgname}/*</code></td>
<td>For all else, interfaces to the files in the package installation directory.</td>
</tr>
</tbody>
</table>
{% highlight bash %}
#package info
curl https://cloud.opencpu.org/ocpu/library/MASS/
#package objects (mostly functions)
curl https://cloud.opencpu.org/ocpu/library/MASS/R/
curl https://cloud.opencpu.org/ocpu/library/MASS/R/rlm/print
#package data objects
curl https://cloud.opencpu.org/ocpu/library/MASS/data/
curl https://cloud.opencpu.org/ocpu/library/MASS/data/housing/json
#read manuals pages
curl https://cloud.opencpu.org/ocpu/library/MASS/man/
curl https://cloud.opencpu.org/ocpu/library/MASS/man/rlm/text
curl https://cloud.opencpu.org/ocpu/library/MASS/man/rlm/html
curl https://cloud.opencpu.org/ocpu/library/MASS/man/rlm/pdf
#read files included with this package
curl https://cloud.opencpu.org/ocpu/library/MASS/scripts/
curl https://cloud.opencpu.org/ocpu/library/MASS/scripts/ch01.R
curl https://cloud.opencpu.org/ocpu/library/MASS/NEWS
#call a function (example from 'rlm' help page)
curl https://cloud.opencpu.org/ocpu/library/MASS/R/rlm -d "formula=stack.loss ~ .&data=stackloss&psi=psi.bisquare"
#run R script
curl https://cloud.opencpu.org/ocpu/library/MASS/scripts/ch01.R -X POST
#read output (replace key with value returned from previous request)
curl https://cloud.opencpu.org/ocpu/tmp/x0648ec526b/R/.val/print
{% endhighlight %}
</div>
</div>
<div class="panel panel-primary" id="api-object">
<div class="panel-heading"><h4>The R object API</h4></div>
<div class="panel-body">
<p>The <code>/R</code> API is used to read R objects, or call R functions.</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Path</th>
<th>What</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>../R/</code></td>
<td>List R objects in this package or session.</td>
</tr>
<tr>
<td><code>../data/</code></td>
<td>List data objects in a package.</td>
</tr>
<tr>
<td><code>../{R|data}/{object}</code></td>
<td>Read object in default format. If <code>object</code> is a function, it can be called using HTTP POST.</td>
</tr>
<tr>
<td><code>../{R|data}/{object}/{format}</code></td>
<td>Retrieve an R object in a particular output format (see section on output formats).</td>
</tr>
</tbody>
</table>
{% highlight bash %}
#list objects and datasets from MASS
curl https://cloud.opencpu.org/ocpu/library/MASS/R/
curl https://cloud.opencpu.org/ocpu/library/MASS/data/
#retrieve objects
curl https://cloud.opencpu.org/ocpu/library/MASS/R/truehist/print
curl https://cloud.opencpu.org/ocpu/library/MASS/data/bacteria/print
curl https://cloud.opencpu.org/ocpu/library/MASS/data/bacteria/json
curl https://cloud.opencpu.org/ocpu/library/MASS/data/bacteria/csv
curl https://cloud.opencpu.org/ocpu/library/MASS/data/bacteria/rda
#call a function
curl https://cloud.opencpu.org/ocpu/library/MASS/R/truehist -d "data=[1,3,7,4,2,4,2,6,23,13,5,2]"
{% endhighlight %}
</div>
</div>
<div class="panel panel-primary" id="api-session">
<div class="panel-heading"><h4>The R session API</h4></div>
<div class="panel-body">
<p>A session is a container that holds resources created from a remote function/script call (RPC).</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Path</th>
<th>What</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>/ocpu/tmp/{key}/</code></td>
<td>List available output for this session.</td>
</tr>
<tr>
<td><code>/ocpu/tmp/{key}/R</code></td>
<td>R objects stored in this session. Interface using <a href="#api-object">R object API</a>, same as objects in packages.</td>
</tr>
<tr>
<td><code>/ocpu/tmp/{key}/graphics/</code></td>
<td>Graphics (plots) stored in this session.</td>
</tr>
<tr>
<td><code>/ocpu/tmp/{key}/graphics/{n}/{format}</code></td>
<td>Retrieve plot number <code>{n}</code> in output format <code>{format}</code>. Format is usually one of <code>png</code>, <code>pdf</code> or <code>svg</code>. The <code>{n}</code> is an integer or "last".</td>
</tr>
<tr>
<td><code>/ocpu/tmp/{key}/source</code></td>
<td>Reads the input source code for this session.</td>
</tr>
<tr>
<td><code>/ocpu/tmp/{key}/stdout</code></td>
<td>Shows text printed to STDOUT in this session.</td>
</tr>
<tr>
<td><code>/ocpu/tmp/{key}/console</code></td>
<td>Shows the console input/output for this session (combines source and stdout)</td>
</tr>
<tr>
<td><code>/ocpu/tmp/{key}/zip</code></td>
<td>Download the entire session as a zip archive.</td>
</tr>
<tr>
<td><code>/ocpu/tmp/{key}/tar</code></td>
<td>Download the entire session as a gzipped tarball.</td>
</tr>
<tr>
<td><code>/ocpu/tmp/{key}/files/*</code></td>
<td>Interfaces to the file API in the working dir of the session.</td>
</tr>
</tbody>
</table>
{% highlight bash %}
#A POST will create a temporary session
#Use the returned key for the subsequent calls below
curl https://cloud.opencpu.org/ocpu/library/MASS/scripts/ch01.R -X POST
#Look at console input/output
curl https://cloud.opencpu.org/ocpu/tmp/x05b85461/console/text
#We read session R objects
curl https://cloud.opencpu.org/ocpu/tmp/x05b85461/
curl https://cloud.opencpu.org/ocpu/tmp/x05b85461/R/
curl https://cloud.opencpu.org/ocpu/tmp/x05b85461/R/dd/csv
curl https://cloud.opencpu.org/ocpu/tmp/x05b85461/R/t.stat/print
#Or even call a function
curl https://cloud.opencpu.org/ocpu/tmp/x05b85461/R/t.stat -d "x=[1,0,0,1,1,1,0,1,1,0]"
#Download file from the working dir
curl https://cloud.opencpu.org/ocpu/tmp/x05b85461/files/ch01.pdf
#Check sessionInfo
curl https://cloud.opencpu.org/ocpu/tmp/x05b85461/info/print
{% endhighlight %}
</div>
</div>
<div class="page-header" id="api-ioformats">
<h1>Input, Output: Data and Formats</h1>
</div>
<div class="panel panel-primary" id="api-formats">
<div class="panel-heading"><h4>Output formats for R objects</h4></div>
<div class="panel-body">
<p>Any R object (including recorded graphics) can be retrieved in various output formats, indicated by <code>:format</code> in the API tables.</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Format</th>
<th>Content-type</th>
<th>Encoder (+args)</th>
<th>Data Frame</th>
<th>Matrix</th>
<th>List</th>
<th>Graphics</th>
<th>Other (function, S4)</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>print</code></td>
<td>text/plain</td>
<td>base::print</td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
</tr>
<tr>
<td><code>json</code></td>
<td>application/json</td>
<td>jsonlite::toJSON</td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
</tr>
<tr>
<td><code>ndjson</code></td>
<td>application/ndjson</td>
<td>jsonlite::stream_out</td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
</tr>
<tr>
<td><code>csv</code></td>
<td>text/csv</td>
<td>utils::write.csv</td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
</tr>
<tr>
<td><code>tab</code></td>
<td>text/plain</td>
<td>utils::write.table</td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
</tr>
<tr>
<td><code>md</code></td>
<td>text/markdown</td>
<td>pander::pander</td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
</tr>
<tr>
<td><code>rda</code></td>
<td>application/octet-stream</td>
<td>base::save</td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
</tr>
<tr>
<td><code>rds</code></td>
<td>application/octet-stream</td>
<td>base::saveRDS</td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
</tr>
<tr>
<td><code>pb</code></td>
<td>application/x-protobuf</td>
<td>protolite::serialize_pb</td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
</tr>
<tr>
<td><code>feather</code></td>
<td>application/feather</td>
<td>feather::write_feather</td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
</tr>
<tr>
<td><code>png</code></td>
<td>image/png</td>
<td>grDevices::png</td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
</tr>
<tr>
<td><code>pdf</code></td>
<td>application/pdf</td>
<td>grDevices::pdf</td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
</tr>
<tr>
<td><code>svg</code></td>
<td>image/svg+xml</td>
<td>grDevices::svg</td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
</tr>
</tbody>
</table>
{% highlight bash %}
#read R objects from packages.
curl https://cloud.opencpu.org/ocpu/library/datasets/R/mtcars/json?digits=0
curl https://cloud.opencpu.org/ocpu/library/datasets/R/mtcars/csv
curl https://cloud.opencpu.org/ocpu/library/datasets/R/mtcars/tab?sep="|"
curl https://cloud.opencpu.org/ocpu/library/MASS/R/loglm/print
{% endhighlight %}
{% highlight bash %}
#create a simple plot
curl https://cloud.opencpu.org/ocpu/library/graphics/R/plot -d "x=cars"
#replace session id with returned one
curl https://cloud.opencpu.org/ocpu/tmp/x0468b7ab/graphics/last/png
curl https://cloud.opencpu.org/ocpu/tmp/x0468b7ab/graphics/1/png?width=1000
curl https://cloud.opencpu.org/ocpu/tmp/x0468b7ab/graphics/last/svg
curl https://cloud.opencpu.org/ocpu/tmp/x0468b7ab/graphics/last/pdf?width=8
{% endhighlight %}
</div>
</div>
<div class="panel panel-primary" id="api-arguments">
<div class="panel-heading"><h4>Argument formats for R function calls (HTTP POST only)</h4></div>
<div class="panel-body">
<p>When calling a function, we need to pass arguments. OpenCPU accepts the following types of arguments</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Type</th>
<th>What</th>
<th>Examples</th>
</tr>
</thead>
<tbody>
<tr>
<td>Primitives</td>
<td>Numbers, strings, booleans</td>
<td><code>3.1415</code>, <code>false</code>, <code>"mytitle"</code></td>
</tr>
<tr>
<td>JSON object</td>
<td>An array or object</td>
<td><code>{"foo" : [1,2,3], "bar" : {"name" : "jerry"}}</code></td>
</tr>
<tr>
<td>R code</td>
<td>plain R code</td>
<td><code>paste("useR", "2013")</code>, <code>mtcars</code></td>
</tr>
<tr>
<td>File upload</td>
<td>File argument will be temporary file path.</td>
<td>(multipart only)</td>
</tr>
<tr>
<td>Temp key (session)</td>
<td>Session ID from previous function call.</td>
<td><code>x2ac58d69</code></td>
</tr>
</tbody>
</table>
<p>When performing a function call, the POST data (request body) can be formated using either <code>multipart/form-data</code>,
<code>application/x-www-form-urlencoded</code>, <code>application/json</code> or <code>application/x-protobuf</code>. Not every content-type supports any argument format:
</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Content-type</th>
<th>Primitives</th>
<th>Data structures</th>
<th>R code</th>
<th>File upload</th>
<th>Temp key (session)</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>application/x-www-form-urlencoded</code></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"> <small>(inline json)</small></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
</tr>
<tr>
<td><code>multipart/form-data</code></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"> <small>(inline json)</small></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
</tr>
<tr>
<td><code>application/json</code></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
</tr>
<tr>
<td><code>application/x-protobuf</code></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="success"><i class="icon icon-check"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
<td class="danger"><i class="icon icon-minus-sign"></i></td>
</tr>
</tbody>
</table>
{% highlight bash %}
#call some functions
curl https://cloud.opencpu.org/ocpu/library/stats/R/rnorm -d "n=10&mean=5"
curl https://cloud.opencpu.org/ocpu/library/graphics/R/hist -d "x=[2,3,2,3,4,3,3]&breaks=10"
curl https://cloud.opencpu.org/ocpu/library/graphics/R/plot -d "x=cars&main='test'"
curl https://cloud.opencpu.org/ocpu/library/base/R/identity -d "x=coef(lm(speed~dist, data=cars))"
{% endhighlight %}
{% highlight bash %}
#upload local file mydata.csv
curl https://cloud.opencpu.org/ocpu/library/utils/R/read.csv -F "[email protected]"
#replace session id with returned one above
curl https://cloud.opencpu.org/ocpu/tmp/x067b4172/R/.val/print
curl https://cloud.opencpu.org/ocpu/library/base/R/summary -d "object=x067b4172"
{% endhighlight %}
{% highlight bash %}
#post arguments in json
curl http://cloud.opencpu.org/ocpu/library/stats/R/rnorm \
-H "Content-Type: application/json" -d '{"n":10, "mean": 10, "sd":10}'
{% endhighlight %}
</div>
</div>
<div class="panel panel-primary" id="api-scripts">
<div class="panel-heading"><h4>Running scripts and reproducible documents</h4></div>
<div class="panel-body">
<p>We can run a script by performing HTTP POST on a file. The script is interpreted according to its (case insensitive) file extension. Any HTTP POST arguments are passed on to the interpreting function. The following types are supported:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>File extension</th>
<th>Type</th>
<th>Interpreter</th>
<th>Arguments</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>file.r</code></td>
<td>R script</td>
<td>evaluate::evaluate</td>
<td>-</td>
</tr>
<tr>
<td><code>file.tex</code></td>
<td>latex</td>
<td>tools::texi2pdf</td>
<td>-</td>
</tr>
<tr>
<td><code>file.rnw</code></td>
<td>knitr/sweave</td>
<td>knitr::knit + tools::texi2pdf</td>
<td>-</td>
</tr>
<tr>
<td><code>file.md</code></td>
<td>markdown</td>
<td>knitr::pandoc</td>
<td><code>format</code> (see ?pandoc) </td>
</tr>
<tr>
<td><code>file.rmd</code></td>
<td>knitr/markdown</td>
<td>knitr::knit + knitr::pandoc</td>
<td><code>format</code> (see ?pandoc) </td>
</tr>
<tr>
<td><code>file.brew</code></td>
<td>brew</td>
<td>brew::brew</td>
<td><code>output</code> (see ?brew)</td>
</tr>
</tbody>
</table>
{% highlight bash %}
#run scripts which exist in packages
curl https://cloud.opencpu.org/ocpu/library/MASS/scripts/ch01.R -X POST
curl https://cloud.opencpu.org/ocpu/library/brew/featurefull.brew -X POST
curl https://cloud.opencpu.org/ocpu/library/brew/brew-test-2.brew -d "output=output.html"
curl https://cloud.opencpu.org/ocpu/library/knitr/examples/knitr-minimal.Rmd -X POST
curl https://cloud.opencpu.org/ocpu/library/knitr/examples/knitr-minimal.Rmd -d "format=docx"
curl https://cloud.opencpu.org/ocpu/library/knitr/examples/knitr-minimal.Rmd -d "format=html"
curl https://cloud.opencpu.org/ocpu/library/knitr/examples/knitr-minimal.Rnw
{% endhighlight %}
</div>
</div>
<div class="panel panel-primary" id="api-json">
<div class="panel-heading"><h4>JSON I/O RPC (a.k.a. Data Processing Units)</h4></div>
<div class="panel-body">
<p>For the common special case where a client is only interested in the output data from a function call in JSON format, the HTTP POST request url can be post-fixed with <code>/json</code>. In this case, a successfull call will return status 200 (instead of 201), and the response body directly contains the returned object in JSON; no need for an additional GET request.</p>
{% highlight bash %}
curl http://cloud.opencpu.org/ocpu/library/stats/R/rnorm/json -d n=2
[
-1.2804,
-0.75013
]
{% endhighlight %}
<p>We can combine this with the <code>application/json</code> request content type for full JSON RPC on R functions</p>
{% highlight bash %}
curl http://cloud.opencpu.org/ocpu/library/stats/R/rnorm/json \
-H "Content-Type: application/json" -d '{"n":3, "mean": 10, "sd":10}'
[
4.9829,
6.3104,
11.411
]
{% endhighlight %}
<p> The above request invokes the following R function call:</p>
{% highlight r %}
library(jsonlite)
args <- fromJSON('{"n":3, "mean": 10, "sd":10}')
output <- do.call(stats::rnorm, args)
toJSON(output)
{% endhighlight r%}
<p>Which in this case is equivalent to:<p>
{% highlight r %}
rnorm(n=3, mean=10, sd=10)
{% endhighlight %}
</div>
</div>
<div class="page-header" id="api-misc">
<h1>Miscellaneous functionality</h1>
</div>
<div class="panel panel-primary" id="api-apps">
<div class="panel-heading"><h4>OpenCPU Apps</h4></div>
<div class="panel-body">
<p>OpenCPU apps are static web pages (html, css, js) which are included in an R package. They interface the R functions in this package through the OpenCPU API's. By convention, these apps are placed in the <code>/inst/www/</code> directory of the R source package. See for more information the <a href="apps.html">apps page</a>.</p>
</div>
</div>
<div class="panel panel-primary" id="api-ci">
<div class="panel-heading"><h4>Github CI Hook</h4></div>
<div class="panel-body">
<p>The OpenCPU cloud server includes support for continuous integration (CI). Thereby a Github repository can be configured to automatically install your package on an OpenCPU server, every time a commit is pushed to the master branch. To take advantage of this feature, it is required that:</p>
<ol>
<li>The R source package is in the root directory of your repository. <a href="https://github.com/rwebapps/appdemo">(example)</a></li>
<li>The Github user account has a public email address</li>
</ol>
<p>To setup CI, add the <code>/ocpu/webhook</code> URL for your server as a 'WebHook' in your Github repository. For example, to use the public demo server, add a webhook with the URL below (you can leave the <code>Content-type</code> and <code>Secret</code> field unchanged)</p>
{% highlight html %}
https://cloud.opencpu.org/ocpu/webhook
{% endhighlight %}
<p> To trigger a build, push a commit to the master branch. The build will show up under <i>Recent Deliveries</i> in your github webhook page, and you should receive an email (in your spam folder) reporting if the installation was successful. If it was, the package will directly be available for remote use at <code>/ocpu/apps/{username}/{package}/</code> on the server.</p>
<img class="img-thumbnail img-responsive" src="./images/githook.png" alt="git hook screenshot">
<p><br>
If you are using the public server, the package will also be available via <a href="https://jeroen.ocpu.io/jsonlite/"><code>https://{yourname}.ocpu.io/{package}/</code></a>. If you are running your own OpenCPU cloud server, an SMTP server must be configured for the email notifictions to work.</p>
</div>
</div>
<!--spacer-->
<div style="height:500px;"></div>
</div>
</div>
<script>
$(function(){
$('body').scrollspy({ target: '#docmenu', offset:80 })
});
//fancy scrolling animation
if(!(/iPhone|iPod|iPad|Android|BlackBerry/).test(navigator.userAgent)){
$("#docmenu li a").on('click', function(e) {
// prevent default anchor click behavior
e.preventDefault();
$(this).blur();
// store hash
var hash = this.hash;
// animate
$('html, body').animate({
scrollTop: $(this.hash).offset().top - 65
}, 500, function(){
// when done, add hash to url
// (default click behaviour)
window.location.hash = hash;
});
});
}
</script>