-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclojisr.v1.tutorials.main.html
1912 lines (1879 loc) · 176 KB
/
clojisr.v1.tutorials.main.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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.5.10">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>ClojisR Documentation – 2 Clojisr tutorial</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="site_libs/quarto-nav/quarto-nav.js"></script>
<script src="site_libs/quarto-nav/headroom.min.js"></script>
<script src="site_libs/clipboard/clipboard.min.js"></script>
<script src="site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="site_libs/quarto-search/fuse.min.js"></script>
<script src="site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="./">
<link href="./clojisr.v1.tutorials.codegen.html" rel="next">
<link href="./index.html" rel="prev">
<script src="site_libs/quarto-html/quarto.js"></script>
<script src="site_libs/quarto-html/popper.min.js"></script>
<script src="site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="site_libs/quarto-html/anchor.min.js"></script>
<link href="site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="site_libs/bootstrap/bootstrap.min.js"></script>
<link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script id="quarto-search-options" type="application/json">{
"location": "sidebar",
"copy-button": false,
"collapse-after": 3,
"panel-placement": "start",
"type": "textbox",
"limit": 50,
"keyboard-shortcut": [
"f",
"/",
"s"
],
"show-item-context": false,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-text-placeholder": "",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit",
"search-label": "Search"
}
}</script>
<meta property="og:image" content="https://scicloj.github.io/sci-cloj-logo-transparent.png">
<meta property="og:title" content="ClojisR Documentation - 2 Clojisr tutorial">
<meta property="og:description" content="">
<link rel="icon" href="data:,">
</head>
<body class="nav-sidebar floating">
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top">
<nav class="quarto-secondary-nav">
<div class="container-fluid d-flex">
<button type="button" class="quarto-btn-toggle btn" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<i class="bi bi-layout-text-sidebar-reverse"></i>
</button>
<nav class="quarto-page-breadcrumbs" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="./clojisr.v1.tutorials.main.html"><span class="chapter-number">2</span> <span class="chapter-title">Clojisr tutorial</span></a></li></ol></nav>
<a class="flex-grow-1" role="button" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
</a>
<button type="button" class="btn quarto-search-button" aria-label="" onclick="window.quartoOpenSearch();">
<i class="bi bi-search"></i>
</button>
</div>
</nav>
</header>
<!-- content -->
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article">
<!-- sidebar -->
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal quarto-sidebar-collapse-item sidebar-navigation floating overflow-auto">
<div class="pt-lg-2 mt-2 text-left sidebar-header">
<div class="sidebar-title mb-0 py-0">
<a href="./">ClojisR Documentation</a>
</div>
</div>
<div class="mt-2 flex-shrink-0 align-items-center">
<div class="sidebar-search">
<div id="quarto-search" class="" title="Search"></div>
</div>
</div>
<div class="sidebar-menu-container">
<ul class="list-unstyled mt-1">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./index.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">1</span> <span class="chapter-title">Preface</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./clojisr.v1.tutorials.main.html" class="sidebar-item-text sidebar-link active">
<span class="menu-text"><span class="chapter-number">2</span> <span class="chapter-title">Clojisr tutorial</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./clojisr.v1.tutorials.codegen.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">3</span> <span class="chapter-title">R code generation from Clojure forms</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./clojisr.v1.tutorials.dataset.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">4</span> <span class="chapter-title">Dataset transfer from R to Clojure</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./clojisr.v1.tutorials.titanic0.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">5</span> <span class="chapter-title">ClojisR example: Titanic #0</span></span></a>
</div>
</li>
</ul>
</div>
</nav>
<div id="quarto-sidebar-glass" class="quarto-sidebar-collapse-item" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item"></div>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#setup" id="toc-setup" class="nav-link active" data-scroll-target="#setup"><span class="header-section-number">2.1</span> Setup</a></li>
<li><a href="#basic-examples" id="toc-basic-examples" class="nav-link" data-scroll-target="#basic-examples"><span class="header-section-number">2.2</span> Basic examples</a></li>
<li><a href="#functions" id="toc-functions" class="nav-link" data-scroll-target="#functions"><span class="header-section-number">2.3</span> Functions</a></li>
<li><a href="#r-dataframes-and-tech.ml.dataset-datasets" id="toc-r-dataframes-and-tech.ml.dataset-datasets" class="nav-link" data-scroll-target="#r-dataframes-and-tech.ml.dataset-datasets"><span class="header-section-number">2.4</span> R dataframes and tech.ml.dataset datasets</a></li>
<li><a href="#r-objects" id="toc-r-objects" class="nav-link" data-scroll-target="#r-objects"><span class="header-section-number">2.5</span> R objects</a></li>
<li><a href="#generating-code" id="toc-generating-code" class="nav-link" data-scroll-target="#generating-code"><span class="header-section-number">2.6</span> Generating code</a></li>
<li><a href="#running-generated-code" id="toc-running-generated-code" class="nav-link" data-scroll-target="#running-generated-code"><span class="header-section-number">2.7</span> Running generated code</a></li>
<li><a href="#requiring-r-packages" id="toc-requiring-r-packages" class="nav-link" data-scroll-target="#requiring-r-packages"><span class="header-section-number">2.8</span> Requiring R packages</a></li>
<li><a href="#data-visualization" id="toc-data-visualization" class="nav-link" data-scroll-target="#data-visualization"><span class="header-section-number">2.9</span> Data visualization</a></li>
<li><a href="#intermediary-representation-as-java-objects." id="toc-intermediary-representation-as-java-objects." class="nav-link" data-scroll-target="#intermediary-representation-as-java-objects."><span class="header-section-number">2.10</span> Intermediary representation as Java objects.</a></li>
<li><a href="#more-data-conversion-examples" id="toc-more-data-conversion-examples" class="nav-link" data-scroll-target="#more-data-conversion-examples"><span class="header-section-number">2.11</span> More data conversion examples</a>
<ul>
<li><a href="#basic-types-convertion-clj-r-clj" id="toc-basic-types-convertion-clj-r-clj" class="nav-link" data-scroll-target="#basic-types-convertion-clj-r-clj"><span class="header-section-number">2.11.1</span> Basic types convertion clj->r->clj</a></li>
<li><a href="#various-r-objects" id="toc-various-r-objects" class="nav-link" data-scroll-target="#various-r-objects"><span class="header-section-number">2.11.2</span> Various R objects</a></li>
</ul></li>
<li><a href="#inspecting-r-functions" id="toc-inspecting-r-functions" class="nav-link" data-scroll-target="#inspecting-r-functions"><span class="header-section-number">2.12</span> Inspecting R functions</a></li>
<li><a href="#r-function-arglists" id="toc-r-function-arglists" class="nav-link" data-scroll-target="#r-function-arglists"><span class="header-section-number">2.13</span> R-function-arglists</a></li>
</ul>
</nav>
</div>
<!-- main -->
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title"><span class="chapter-number">2</span> <span class="chapter-title">Clojisr tutorial</span></h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<style></style>
<style>.printedClojure .sourceCode {
background-color: transparent;
border-style: none;
}
</style>
<style>.clay-limit-image-width .clay-image {max-width: 100%}
</style>
<script src="clojisr.v1.tutorials.main_files/md-default1.js" type="text/javascript"></script>
<script src="clojisr.v1.tutorials.main_files/md-default2.js" type="text/javascript"></script>
<section id="setup" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="setup"><span class="header-section-number">2.1</span> Setup</h2>
<div class="sourceClojure">
<div class="sourceCode" id="cb1"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">ns</span> clojisr.v1.tutorials.<span class="kw">main</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a> (<span class="at">:require</span> [clojisr.v1.r <span class="at">:as</span> r <span class="at">:refer</span> [r eval-r->java r->java java->r java->clj java->native-clj clj->java r->clj clj->r ->code r+ colon require-r]]</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a> [clojisr.v1.robject <span class="at">:as</span> robject]</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> [clojisr.v1.session <span class="at">:as</span> session]</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> [tech.v3.dataset <span class="at">:as</span> dataset]</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a> [scicloj.kindly.v4.kind <span class="at">:as</span> kind]</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> [scicloj.kindly.v4.api <span class="at">:as</span> kindly]))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<style>
img {max-width: 100%;}
svg {max-width: 100%;}
</style>
</section>
<section id="basic-examples" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="basic-examples"><span class="header-section-number">2.2</span> Basic examples</h2>
<p>Let us start by some basic usage examples of Clojisr.</p>
<p>First, let us make sure that we use the Rserve backend (in case we were using another engine instead earlier), and that there are no R sessions currently running. This is typically not needed if you just started working. Here, we do it just in case.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb2"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>(r/set-default-session-type! <span class="at">:rserve</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb3"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>{<span class="at">:session-type</span> <span class="at">:rserve</span>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb4"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>(r/discard-all-sessions)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb5"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>{}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Now let us run some R code, and keep a Clojure handle to the return value.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb6"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>(<span class="bu">def</span><span class="fu"> x </span>(r <span class="st">"1+2"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>The thing we created is something called an ROBject.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb7"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">class</span> x)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb8"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>clojisr.v1.robject.RObject</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>If we wish, we can convert an ROBject to Clojure:</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb9"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>(r->clj x)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb10"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>[<span class="fl">3.0</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Let us see more examples of creating ROBjects and converting them to Clojure:</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb11"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">->></span> <span class="st">"list(A=1,B=2,'#123strange<text> ()'=3)"</span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a> r</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a> r->clj)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb12"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>{<span class="at">:A</span> [<span class="fl">1.0</span>], <span class="at">:B</span> [<span class="fl">2.0</span>], <span class="st">"#123strange<text> ()"</span> [<span class="fl">3.0</span>]}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>In the other direction, we can convert Clojure data to R data. Note that <code>nil</code> is turned to <code>NA</code>.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb13"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">-></span> [<span class="dv">1</span> <span class="va">nil</span> <span class="dv">3</span>]</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> clj->r)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb14"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>[<span class="dv">1</span>] <span class="dv">1</span> NA <span class="dv">3</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>We can run code on a separate R session (specify session-args which are different than the default ones).</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb15"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">-></span> <span class="st">"1+2"</span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a> (r <span class="at">:session-args</span> {<span class="at">:session-name</span> <span class="st">"mysession"</span>})</span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a> r->clj)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb16"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>[<span class="fl">3.0</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="functions" class="level2" data-number="2.3">
<h2 data-number="2.3" class="anchored" data-anchor-id="functions"><span class="header-section-number">2.3</span> Functions</h2>
<p>An R function is also a Clojure function.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb17"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>(<span class="bu">def</span><span class="fu"> f </span>(r <span class="st">"function(x) x*10"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Let us apply it to Clojure data (implicitly converting that data to R).</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb18"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">-></span> <span class="dv">5</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a> f</span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a> r->clj)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb19"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>[<span class="fl">50.0</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>We can also apply it to R data.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb20"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">-></span> <span class="st">"5*5"</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a> r</span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a> f</span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a> r->clj)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb21"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>[<span class="fl">250.0</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Functions can get named arguments. Here we pass the <code>na.rm</code> argument, that tells R whether to remove missing values whenn computing the mean.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb22"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>(r->clj ((r <span class="st">"mean"</span>)</span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a> [<span class="dv">1</span> <span class="va">nil</span> <span class="dv">3</span>]</span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a> <span class="at">:na.rm</span> <span class="va">true</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb23"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a>[<span class="fl">2.0</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Another example:</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb24"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">let</span> [f (r <span class="st">"function(w,x,y=10,z=20) w+x+y+z"</span>)]</span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a> (<span class="kw">->></span> [(f <span class="dv">1</span> <span class="dv">2</span>)</span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a> (f <span class="dv">1</span> <span class="dv">2</span> <span class="at">:y</span> <span class="dv">100</span>)</span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a> (f <span class="dv">1</span> <span class="dv">2</span> <span class="at">:z</span> <span class="dv">100</span>)]</span>
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a> (<span class="kw">map</span> r->clj)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb25"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>([<span class="fl">33.0</span>] [<span class="fl">123.0</span>] [<span class="fl">113.0</span>])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Some functions are already created in Clojisr and given special names for convenience. Here are some examples:</p>
<p>R addition:</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb26"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">->></span> (r+ <span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>)</span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a> r->clj)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb27"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a>[<span class="dv">6</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>R colon (<code>:</code>), for creating a range of integers, like <code>0:9</code>:</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb28"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>(r->clj (colon <span class="dv">0</span> <span class="dv">9</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb29"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a>[<span class="dv">0</span> <span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span> <span class="dv">4</span> <span class="dv">5</span> <span class="dv">6</span> <span class="dv">7</span> <span class="dv">8</span> <span class="dv">9</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="r-dataframes-and-tech.ml.dataset-datasets" class="level2" data-number="2.4">
<h2 data-number="2.4" class="anchored" data-anchor-id="r-dataframes-and-tech.ml.dataset-datasets"><span class="header-section-number">2.4</span> R dataframes and tech.ml.dataset datasets</h2>
<p>At Clojure, we have a structure that is equivalent to R dataframes: a <a href="https://github.com/techascent/tech.ml.dataset">tech.ml.dataset</a> dataset.</p>
<p>Let us create such a dataset, pass it to an R function to compute the row means, and then convert the return value back to Clojure.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb30"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">let</span> [row-means (r <span class="st">"function(data) rowMeans(data)"</span>)]</span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a> (<span class="kw">-></span> {<span class="at">:x</span> [<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>]</span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a> <span class="at">:y</span> [<span class="dv">4</span> <span class="dv">5</span> <span class="dv">6</span>]}</span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a> dataset/->dataset</span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a> row-means</span>
<span id="cb30-6"><a href="#cb30-6" aria-hidden="true" tabindex="-1"></a> r->clj))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb31"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a>[<span class="fl">2.5</span> <span class="fl">3.5</span> <span class="fl">4.5</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Let us see some more dataset proccessing through R.</p>
<p>Loading the R package <a href="https://dplyr.tidyverse.org/">dplyr</a> (assuming it is installed).</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb32"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a>(r <span class="st">"library(dplyr)"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb33"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a>[<span class="dv">1</span>] <span class="st">"dplyr"</span> <span class="st">"Rserve"</span> <span class="st">"stats"</span> <span class="st">"graphics"</span> <span class="st">"grDevices"</span> <span class="st">"utils"</span> </span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a>[<span class="dv">7</span>] <span class="st">"datasets"</span> <span class="st">"methods"</span> <span class="st">"base"</span> </span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Using dplyr to process some Clojure dataset, and convert back to the resulting dataset.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb34"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">let</span> [filter-by-x (r <span class="st">"function(data) filter(data, x>=2)"</span>)</span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a> add-z-column (r <span class="st">"function(data) mutate(data, z=x+y)"</span>)]</span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a> (<span class="kw">-></span> {<span class="at">:x</span> [<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>]</span>
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a> <span class="at">:y</span> [<span class="dv">4</span> <span class="dv">5</span> <span class="dv">6</span>]}</span>
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a> dataset/->dataset</span>
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a> filter-by-x</span>
<span id="cb34-7"><a href="#cb34-7" aria-hidden="true" tabindex="-1"></a> add-z-column</span>
<span id="cb34-8"><a href="#cb34-8" aria-hidden="true" tabindex="-1"></a> r->clj))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="clay-dataset">
<p>_unnamed [2 3]:</p>
<table class="table">
<thead>
<tr class="header">
<th style="text-align: right;">:x</th>
<th style="text-align: right;">:y</th>
<th style="text-align: right;">:z</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">2</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">7</td>
</tr>
<tr class="even">
<td style="text-align: right;">3</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">9</td>
</tr>
</tbody>
</table>
</div>
<p><a href="https://tibble.tidyverse.org">Tibbles</a>, which are a more recent R dataframe notion, are also supported, as a special case of data frames.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb35"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a>(r <span class="st">"library(tibble)"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb36"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a> [<span class="dv">1</span>] <span class="st">"tibble"</span> <span class="st">"dplyr"</span> <span class="st">"Rserve"</span> <span class="st">"stats"</span> <span class="st">"graphics"</span> <span class="st">"grDevices"</span></span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a> [<span class="dv">7</span>] <span class="st">"utils"</span> <span class="st">"datasets"</span> <span class="st">"methods"</span> <span class="st">"base"</span> </span>
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb37"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">let</span> [tibble (r <span class="st">"tibble"</span>)]</span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a> (tibble</span>
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a> <span class="at">:x</span> [<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>]</span>
<span id="cb37-4"><a href="#cb37-4" aria-hidden="true" tabindex="-1"></a> <span class="at">:y</span> [<span class="dv">4</span> <span class="dv">5</span> <span class="dv">6</span>]))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb38"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a># A tibble: <span class="dv">3</span> × <span class="dv">2</span></span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a> x y</span>
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a> <int> <int></span>
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a><span class="dv">1</span> <span class="dv">1</span> <span class="dv">4</span></span>
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a><span class="dv">2</span> <span class="dv">2</span> <span class="dv">5</span></span>
<span id="cb38-6"><a href="#cb38-6" aria-hidden="true" tabindex="-1"></a><span class="dv">3</span> <span class="dv">3</span> <span class="dv">6</span></span>
<span id="cb38-7"><a href="#cb38-7" aria-hidden="true" tabindex="-1"></a></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb39"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">let</span> [tibble (r <span class="st">"tibble"</span>)]</span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a> (<span class="kw">-></span> (tibble</span>
<span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a> <span class="at">:x</span> [<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>]</span>
<span id="cb39-4"><a href="#cb39-4" aria-hidden="true" tabindex="-1"></a> <span class="at">:y</span> [<span class="dv">4</span> <span class="dv">5</span> <span class="dv">6</span>])</span>
<span id="cb39-5"><a href="#cb39-5" aria-hidden="true" tabindex="-1"></a> r->clj</span>
<span id="cb39-6"><a href="#cb39-6" aria-hidden="true" tabindex="-1"></a> dataset/mapseq-reader))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb40"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a>[{<span class="at">:x</span> <span class="dv">1</span>, <span class="at">:y</span> <span class="dv">4</span>} {<span class="at">:x</span> <span class="dv">2</span>, <span class="at">:y</span> <span class="dv">5</span>} {<span class="at">:x</span> <span class="dv">3</span>, <span class="at">:y</span> <span class="dv">6</span>}]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="r-objects" class="level2" data-number="2.5">
<h2 data-number="2.5" class="anchored" data-anchor-id="r-objects"><span class="header-section-number">2.5</span> R objects</h2>
<p>Clojisr holds handles to R objects, that are stored in memory at the R session, where they are assigned random names.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb41"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a>(<span class="bu">def</span><span class="fu"> one+two </span>(r <span class="st">"1+2"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb42"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">class</span> one+two)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb43"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>clojisr.v1.robject.RObject</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>The name of an object is the place where it is held at R (inside an R <a href="http://adv-r.had.co.nz/Environments.html">evnironment</a> called <code>.MEM</code>).</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb44"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a>(<span class="at">:object-name</span> one+two)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb45"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="st">".MEM$x7a9289ee40e04192"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="generating-code" class="level2" data-number="2.6">
<h2 data-number="2.6" class="anchored" data-anchor-id="generating-code"><span class="header-section-number">2.6</span> Generating code</h2>
<p>Let us see the mechanism by which clojisr generates R code, and the rules defining it.</p>
<p>Since we are playing a bit with the internals here, we will need a reference to the R session:</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb46"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a>(<span class="bu">def</span><span class="fu"> session</span></span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a> (session/fetch-or-make <span class="va">nil</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>For the following examples, we will use some <strong>dummy</strong> handles to R objects with given names:</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb47"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a>(<span class="bu">def</span><span class="fu"> x </span>(robject/->RObject <span class="st">"robject_x"</span> session <span class="va">nil</span> <span class="va">nil</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb48"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a>(<span class="bu">def</span><span class="fu"> y </span>(robject/->RObject <span class="st">"robject_y"</span> session <span class="va">nil</span> <span class="va">nil</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>.. and some <strong>real</strong> handles to R objects:</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb49"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a>(<span class="bu">def</span><span class="fu"> minus-eleven </span>(r <span class="st">"-11"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb50"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a>(<span class="bu">def</span><span class="fu"> abs </span>(r <span class="st">"abs"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>The function <code>->code</code> generates R code according to a certain set of rules. Here we describe some of these rules briefly. We also wrote a dedicated tutorial about the rule set more thoroughly.</p>
<p>For an ROBject, the generated code is just the ROBject name.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb51"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a>(->code x)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb52"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a><span class="st">"robject_x"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>For a clojure value, we use some form analysis and generate proper R string or values.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb53"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a>(->code <span class="st">"hello"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb54"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a><span class="st">"</span><span class="sc">\"</span><span class="st">hello</span><span class="sc">\"</span><span class="st">"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb55"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a>(->code [<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb56"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="st">"c(1L,2L,3L)"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>For a symbol, we generate the code with the corresponding R symbol.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb57"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a>(->code <span class="at">'x</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb58"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a><span class="st">"x"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>A sequential structure (list, vector, etc.) can be interpreted as a compound expression, for which code generation is defined accorting to the first list element.</p>
<p>For a list beginning with the symbol <code>'function</code>, we generate an R function definition.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb59"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a>(->code '(function [x y] x))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb60"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a><span class="st">"function(x,y) {x}"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>For a vector instead of list, we create R vector.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb61"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a>(->code '[function [x y] x])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb62"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a><span class="st">"c(function,c(x,y),x)"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>For a list beginning with the symbol <code>'formula</code>, we generate an R <code>~</code>-formula.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb63"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb63-1"><a href="#cb63-1" aria-hidden="true" tabindex="-1"></a>(->code '(formula x y))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb64"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a><span class="st">"(x~y)"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>For a list beginning with a symbol known to be a binary operator, we generate nested calls.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb65"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a>(->code '(<span class="kw">+</span> x y z))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb66"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb66-1"><a href="#cb66-1" aria-hidden="true" tabindex="-1"></a><span class="st">"((x+y)+z)"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>For a list beginning with another symbol, we generate a function call with that symbol as the function name.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb67"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a>(->code '(f x))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb68"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb68-1"><a href="#cb68-1" aria-hidden="true" tabindex="-1"></a><span class="st">"f(x)"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>For a list beginning with an R object that is a function, we generate a function call with that object as the function. If you create the list using the quote sign (<code>'</code>), don’t forget to unquote symbols refering to things you defined on the Clojure side.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb69"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb69-1"><a href="#cb69-1" aria-hidden="true" tabindex="-1"></a>(->code '(<span class="at">~abs</span> x))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb70"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb70-1"><a href="#cb70-1" aria-hidden="true" tabindex="-1"></a><span class="st">".MEM$x122c42652d2c44ea(x)"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>All other sequential things (that is, those not beginning with a symbol or R function) are intepreted as data, converted implicitly R data representation.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb71"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb71-1"><a href="#cb71-1" aria-hidden="true" tabindex="-1"></a>(->code `(<span class="at">~abs</span> (<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb72"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb72-1"><a href="#cb72-1" aria-hidden="true" tabindex="-1"></a><span class="st">".MEM$x122c42652d2c44ea(c(1L,2L,3L))"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Some more examples, showing how these rules compose:</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb73"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb73-1"><a href="#cb73-1" aria-hidden="true" tabindex="-1"></a>(->code '(function [x y] (f y)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb74"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb74-1"><a href="#cb74-1" aria-hidden="true" tabindex="-1"></a><span class="st">"function(x,y) {f(y)}"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb75"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb75-1"><a href="#cb75-1" aria-hidden="true" tabindex="-1"></a>(->code '(function [x y] (f <span class="at">~y</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb76"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb76-1"><a href="#cb76-1" aria-hidden="true" tabindex="-1"></a><span class="st">"function(x,y) {f(robject_y)}"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb77"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb77-1"><a href="#cb77-1" aria-hidden="true" tabindex="-1"></a>(->code '(function [x y] (<span class="kw">+</span> x y)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb78"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb78-1"><a href="#cb78-1" aria-hidden="true" tabindex="-1"></a><span class="st">"function(x,y) {(x+y)}"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb79"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb79-1"><a href="#cb79-1" aria-hidden="true" tabindex="-1"></a>(->code (<span class="kw">list</span> <span class="at">'function</span> '[x y] (<span class="kw">list</span> <span class="at">'+</span> <span class="at">'x</span> <span class="at">'y</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb80"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb80-1"><a href="#cb80-1" aria-hidden="true" tabindex="-1"></a><span class="st">"function(x,y) {(x+y)}"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb81"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb81-1"><a href="#cb81-1" aria-hidden="true" tabindex="-1"></a>(->code '(function [x y] (<span class="kw">print</span> x) (f x)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb82"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb82-1"><a href="#cb82-1" aria-hidden="true" tabindex="-1"></a><span class="st">"function(x,y) {print(x);f(x)}"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb83"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb83-1"><a href="#cb83-1" aria-hidden="true" tabindex="-1"></a>(->code '(function [x y] (<span class="at">~abs</span> x)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb84"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb84-1"><a href="#cb84-1" aria-hidden="true" tabindex="-1"></a><span class="st">"function(x,y) {.MEM$x122c42652d2c44ea(x)}"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb85"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb85-1"><a href="#cb85-1" aria-hidden="true" tabindex="-1"></a>(->code '(<span class="at">~abs</span> <span class="at">~minus-eleven</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb86"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb86-1"><a href="#cb86-1" aria-hidden="true" tabindex="-1"></a><span class="st">".MEM$x122c42652d2c44ea(.MEM$x28987055e359432e)"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb87"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb87-1"><a href="#cb87-1" aria-hidden="true" tabindex="-1"></a>(->code '(<span class="at">~abs</span> -<span class="dv">11</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb88"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb88-1"><a href="#cb88-1" aria-hidden="true" tabindex="-1"></a><span class="st">".MEM$x122c42652d2c44ea(-11L)"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Use syntax quote ` in case you want to use local bindings.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb89"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb89-1"><a href="#cb89-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">let</span> [minus-ten -<span class="dv">10</span>]</span>
<span id="cb89-2"><a href="#cb89-2" aria-hidden="true" tabindex="-1"></a> (->code `(<span class="at">~abs</span> <span class="at">~minus-ten</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb90"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb90-1"><a href="#cb90-1" aria-hidden="true" tabindex="-1"></a><span class="st">".MEM$x122c42652d2c44ea(-10L)"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="running-generated-code" class="level2" data-number="2.7">
<h2 data-number="2.7" class="anchored" data-anchor-id="running-generated-code"><span class="header-section-number">2.7</span> Running generated code</h2>
<p>Clojure forms can be run as R code. Behind the scences, they are turned to R code using the <code>->code</code> function described above. For example:</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb91"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb91-1"><a href="#cb91-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">-></span> '(<span class="at">~abs</span> ~(<span class="kw">range</span> -<span class="dv">3</span> <span class="dv">0</span>))</span>
<span id="cb91-2"><a href="#cb91-2" aria-hidden="true" tabindex="-1"></a> r</span>
<span id="cb91-3"><a href="#cb91-3" aria-hidden="true" tabindex="-1"></a> r->clj)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb92"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb92-1"><a href="#cb92-1" aria-hidden="true" tabindex="-1"></a>[<span class="dv">3</span> <span class="dv">2</span> <span class="dv">1</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Or, equivalently:</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb93"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb93-1"><a href="#cb93-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">-></span> '(<span class="at">~abs</span> ~(<span class="kw">range</span> -<span class="dv">3</span> <span class="dv">0</span>))</span>
<span id="cb93-2"><a href="#cb93-2" aria-hidden="true" tabindex="-1"></a> ->code</span>
<span id="cb93-3"><a href="#cb93-3" aria-hidden="true" tabindex="-1"></a> r</span>
<span id="cb93-4"><a href="#cb93-4" aria-hidden="true" tabindex="-1"></a> r->clj)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb94"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb94-1"><a href="#cb94-1" aria-hidden="true" tabindex="-1"></a>[<span class="dv">3</span> <span class="dv">2</span> <span class="dv">1</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Let us repeat the basic examples from the beginning of this tutorial, this time generating code rather than writing it as Strings.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb95"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb95-1"><a href="#cb95-1" aria-hidden="true" tabindex="-1"></a>(<span class="bu">def</span><span class="fu"> x </span>(r '(<span class="kw">+</span> <span class="dv">1</span> <span class="dv">2</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb96"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb96-1"><a href="#cb96-1" aria-hidden="true" tabindex="-1"></a>(r->clj x)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb97"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb97-1"><a href="#cb97-1" aria-hidden="true" tabindex="-1"></a>[<span class="dv">3</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb98"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb98-1"><a href="#cb98-1" aria-hidden="true" tabindex="-1"></a>(<span class="bu">def</span><span class="fu"> f </span>(r '(function [x] (<span class="kw">*</span> x <span class="dv">10</span>))))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb99"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb99-1"><a href="#cb99-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">-></span> <span class="dv">5</span></span>
<span id="cb99-2"><a href="#cb99-2" aria-hidden="true" tabindex="-1"></a> f</span>
<span id="cb99-3"><a href="#cb99-3" aria-hidden="true" tabindex="-1"></a> r->clj)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb100"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb100-1"><a href="#cb100-1" aria-hidden="true" tabindex="-1"></a>[<span class="dv">50</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb101"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb101-1"><a href="#cb101-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">-></span> <span class="st">"5*5"</span></span>
<span id="cb101-2"><a href="#cb101-2" aria-hidden="true" tabindex="-1"></a> r</span>
<span id="cb101-3"><a href="#cb101-3" aria-hidden="true" tabindex="-1"></a> f</span>
<span id="cb101-4"><a href="#cb101-4" aria-hidden="true" tabindex="-1"></a> r->clj)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb102"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb102-1"><a href="#cb102-1" aria-hidden="true" tabindex="-1"></a>[<span class="fl">250.0</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb103"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb103-1"><a href="#cb103-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">let</span> [row-means (r '(function [data] (rowMeans data)))]</span>
<span id="cb103-2"><a href="#cb103-2" aria-hidden="true" tabindex="-1"></a> (<span class="kw">-></span> {<span class="at">:x</span> [<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>]</span>
<span id="cb103-3"><a href="#cb103-3" aria-hidden="true" tabindex="-1"></a> <span class="at">:y</span> [<span class="dv">4</span> <span class="dv">5</span> <span class="dv">6</span>]}</span>
<span id="cb103-4"><a href="#cb103-4" aria-hidden="true" tabindex="-1"></a> dataset/->dataset</span>
<span id="cb103-5"><a href="#cb103-5" aria-hidden="true" tabindex="-1"></a> row-means</span>
<span id="cb103-6"><a href="#cb103-6" aria-hidden="true" tabindex="-1"></a> r->clj))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb104"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb104-1"><a href="#cb104-1" aria-hidden="true" tabindex="-1"></a>[<span class="fl">2.5</span> <span class="fl">3.5</span> <span class="fl">4.5</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb105"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb105-1"><a href="#cb105-1" aria-hidden="true" tabindex="-1"></a>(r '(library dplyr))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb106"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb106-1"><a href="#cb106-1" aria-hidden="true" tabindex="-1"></a> [<span class="dv">1</span>] <span class="st">"tibble"</span> <span class="st">"dplyr"</span> <span class="st">"Rserve"</span> <span class="st">"stats"</span> <span class="st">"graphics"</span> <span class="st">"grDevices"</span></span>
<span id="cb106-2"><a href="#cb106-2" aria-hidden="true" tabindex="-1"></a> [<span class="dv">7</span>] <span class="st">"utils"</span> <span class="st">"datasets"</span> <span class="st">"methods"</span> <span class="st">"base"</span> </span>
<span id="cb106-3"><a href="#cb106-3" aria-hidden="true" tabindex="-1"></a></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb107"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb107-1"><a href="#cb107-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">let</span> [filter-by-x (r '(function [data] (<span class="kw">filter</span> data (<span class="kw">>=</span> x <span class="dv">2</span>))))</span>
<span id="cb107-2"><a href="#cb107-2" aria-hidden="true" tabindex="-1"></a> add-z-column (r '(function [data] (mutate data (<span class="kw">=</span> z (<span class="kw">+</span> x y)))))]</span>
<span id="cb107-3"><a href="#cb107-3" aria-hidden="true" tabindex="-1"></a> (<span class="kw">->></span> {<span class="at">:x</span> [<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>]</span>
<span id="cb107-4"><a href="#cb107-4" aria-hidden="true" tabindex="-1"></a> <span class="at">:y</span> [<span class="dv">4</span> <span class="dv">5</span> <span class="dv">6</span>]}</span>
<span id="cb107-5"><a href="#cb107-5" aria-hidden="true" tabindex="-1"></a> dataset/->dataset</span>
<span id="cb107-6"><a href="#cb107-6" aria-hidden="true" tabindex="-1"></a> filter-by-x</span>
<span id="cb107-7"><a href="#cb107-7" aria-hidden="true" tabindex="-1"></a> add-z-column</span>
<span id="cb107-8"><a href="#cb107-8" aria-hidden="true" tabindex="-1"></a> r->clj))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="clay-dataset">
<p>_unnamed [2 3]:</p>
<table class="table">
<thead>
<tr class="header">
<th style="text-align: right;">:x</th>
<th style="text-align: right;">:y</th>
<th style="text-align: right;">:z</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">2</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">7</td>
</tr>
<tr class="even">
<td style="text-align: right;">3</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">9</td>
</tr>
</tbody>
</table>
</div>
</section>
<section id="requiring-r-packages" class="level2" data-number="2.8">
<h2 data-number="2.8" class="anchored" data-anchor-id="requiring-r-packages"><span class="header-section-number">2.8</span> Requiring R packages</h2>
<p>Sometimes, we want to bring to the Clojure world functions and data from R packages. Here, we try to follow the <a href="https://github.com/cnuernber/libpython-clj/blob/master/test/libpython_clj/require_python_test.clj">require-python</a> syntax of <a href="https://github.com/cnuernber/libpython-clj">libpython-clj</a> (though currently in a less sophisticated way.)</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb108"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb108-1"><a href="#cb108-1" aria-hidden="true" tabindex="-1"></a>(require-r '[stats <span class="at">:as</span> statz <span class="at">:refer</span> [median]])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb109"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb109-1"><a href="#cb109-1" aria-hidden="true" tabindex="-1"></a><span class="va">nil</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb110"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb110-1"><a href="#cb110-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">-></span> [<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>]</span>
<span id="cb110-2"><a href="#cb110-2" aria-hidden="true" tabindex="-1"></a> r.stats/median</span>
<span id="cb110-3"><a href="#cb110-3" aria-hidden="true" tabindex="-1"></a> r->clj</span>
<span id="cb110-4"><a href="#cb110-4" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb111"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb111-1"><a href="#cb111-1" aria-hidden="true" tabindex="-1"></a>[<span class="dv">2</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb112"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb112-1"><a href="#cb112-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">-></span> [<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>]</span>
<span id="cb112-2"><a href="#cb112-2" aria-hidden="true" tabindex="-1"></a> statz/median</span>
<span id="cb112-3"><a href="#cb112-3" aria-hidden="true" tabindex="-1"></a> r->clj)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb113"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb113-1"><a href="#cb113-1" aria-hidden="true" tabindex="-1"></a>[<span class="dv">2</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb114"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb114-1"><a href="#cb114-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">-></span> [<span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>]</span>
<span id="cb114-2"><a href="#cb114-2" aria-hidden="true" tabindex="-1"></a> median</span>
<span id="cb114-3"><a href="#cb114-3" aria-hidden="true" tabindex="-1"></a> r->clj)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb115"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb115-1"><a href="#cb115-1" aria-hidden="true" tabindex="-1"></a>[<span class="dv">2</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb116"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb116-1"><a href="#cb116-1" aria-hidden="true" tabindex="-1"></a>(require-r '[datasets <span class="at">:as</span> datasetz <span class="at">:refer</span> [euro]])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb117"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb117-1"><a href="#cb117-1" aria-hidden="true" tabindex="-1"></a><span class="va">nil</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb118"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb118-1"><a href="#cb118-1" aria-hidden="true" tabindex="-1"></a>[r.datasets/euro</span>
<span id="cb118-2"><a href="#cb118-2" aria-hidden="true" tabindex="-1"></a> datasetz/euro</span>
<span id="cb118-3"><a href="#cb118-3" aria-hidden="true" tabindex="-1"></a> euro]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb119"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb119-1"><a href="#cb119-1" aria-hidden="true" tabindex="-1"></a>[ ATS BEF DEM ESP FIM FRF </span>
<span id="cb119-2"><a href="#cb119-2" aria-hidden="true" tabindex="-1"></a> <span class="fl">13.760300</span> <span class="fl">40.339900</span> <span class="fl">1.955830</span> <span class="fl">166.386000</span> <span class="fl">5.945730</span> <span class="fl">6.559570</span> </span>
<span id="cb119-3"><a href="#cb119-3" aria-hidden="true" tabindex="-1"></a> IEP ITL LUF NLG PTE </span>
<span id="cb119-4"><a href="#cb119-4" aria-hidden="true" tabindex="-1"></a> <span class="fl">0.787564</span> <span class="fl">1936.270000</span> <span class="fl">40.339900</span> <span class="fl">2.203710</span> <span class="fl">200.482000</span> </span>
<span id="cb119-5"><a href="#cb119-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb119-6"><a href="#cb119-6" aria-hidden="true" tabindex="-1"></a> ATS BEF DEM ESP FIM FRF </span>
<span id="cb119-7"><a href="#cb119-7" aria-hidden="true" tabindex="-1"></a> <span class="fl">13.760300</span> <span class="fl">40.339900</span> <span class="fl">1.955830</span> <span class="fl">166.386000</span> <span class="fl">5.945730</span> <span class="fl">6.559570</span> </span>
<span id="cb119-8"><a href="#cb119-8" aria-hidden="true" tabindex="-1"></a> IEP ITL LUF NLG PTE </span>
<span id="cb119-9"><a href="#cb119-9" aria-hidden="true" tabindex="-1"></a> <span class="fl">0.787564</span> <span class="fl">1936.270000</span> <span class="fl">40.339900</span> <span class="fl">2.203710</span> <span class="fl">200.482000</span> </span>
<span id="cb119-10"><a href="#cb119-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb119-11"><a href="#cb119-11" aria-hidden="true" tabindex="-1"></a> ATS BEF DEM ESP FIM FRF </span>
<span id="cb119-12"><a href="#cb119-12" aria-hidden="true" tabindex="-1"></a> <span class="fl">13.760300</span> <span class="fl">40.339900</span> <span class="fl">1.955830</span> <span class="fl">166.386000</span> <span class="fl">5.945730</span> <span class="fl">6.559570</span> </span>
<span id="cb119-13"><a href="#cb119-13" aria-hidden="true" tabindex="-1"></a> IEP ITL LUF NLG PTE </span>
<span id="cb119-14"><a href="#cb119-14" aria-hidden="true" tabindex="-1"></a> <span class="fl">0.787564</span> <span class="fl">1936.270000</span> <span class="fl">40.339900</span> <span class="fl">2.203710</span> <span class="fl">200.482000</span> </span>
<span id="cb119-15"><a href="#cb119-15" aria-hidden="true" tabindex="-1"></a>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb120"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb120-1"><a href="#cb120-1" aria-hidden="true" tabindex="-1"></a>(require-r '[base <span class="at">:refer</span> [$]])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb121"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb121-1"><a href="#cb121-1" aria-hidden="true" tabindex="-1"></a><span class="va">nil</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb122"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb122-1"><a href="#cb122-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">-></span> {<span class="at">:a</span> <span class="dv">1</span> <span class="at">:b</span> <span class="dv">2</span>}</span>
<span id="cb122-2"><a href="#cb122-2" aria-hidden="true" tabindex="-1"></a> ($ <span class="at">'a</span>)</span>
<span id="cb122-3"><a href="#cb122-3" aria-hidden="true" tabindex="-1"></a> r->clj)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb123"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb123-1"><a href="#cb123-1" aria-hidden="true" tabindex="-1"></a>[<span class="dv">1</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="data-visualization" class="level2" data-number="2.9">
<h2 data-number="2.9" class="anchored" data-anchor-id="data-visualization"><span class="header-section-number">2.9</span> Data visualization</h2>
<p>Functions creating R plots or any plotting objects generated by various R libraries can be wrapped in a way that returns an SVG, BufferedImage or can be saved to a file. All of them accept additional parameters specified in <code>grDevices</code> R package.</p>
<p>Currently there is a bug that sometimes causes axes and labels to disappear when rendered inside a larger HTML.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb124"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb124-1"><a href="#cb124-1" aria-hidden="true" tabindex="-1"></a>(require-r '[graphics <span class="at">:refer</span> [plot hist]])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb125"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb125-1"><a href="#cb125-1" aria-hidden="true" tabindex="-1"></a><span class="va">nil</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb126"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb126-1"><a href="#cb126-1" aria-hidden="true" tabindex="-1"></a>(require-r '[ggplot2 <span class="at">:refer</span> [ggplot aes geom_point xlab ylab labs]])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="printedClojure">
<div class="sourceCode" id="cb127"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb127-1"><a href="#cb127-1" aria-hidden="true" tabindex="-1"></a><span class="va">nil</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="sourceClojure">
<div class="sourceCode" id="cb128"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb128-1"><a href="#cb128-1" aria-hidden="true" tabindex="-1"></a>(<span class="kw">require</span> '[clojisr.v1.applications.plotting <span class="at">:refer</span> [plot->svg plot->file plot->buffered-image]])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>First example, simple plotting function as SVG string.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb129"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb129-1"><a href="#cb129-1" aria-hidden="true" tabindex="-1"></a>(plot->svg</span>
<span id="cb129-2"><a href="#cb129-2" aria-hidden="true" tabindex="-1"></a> (<span class="kw">fn</span> []</span>
<span id="cb129-3"><a href="#cb129-3" aria-hidden="true" tabindex="-1"></a> (<span class="kw">->></span> <span class="kw">rand</span></span>
<span id="cb129-4"><a href="#cb129-4" aria-hidden="true" tabindex="-1"></a> (<span class="kw">repeatedly</span> <span class="dv">30</span>)</span>
<span id="cb129-5"><a href="#cb129-5" aria-hidden="true" tabindex="-1"></a> (reductions <span class="kw">+</span>)</span>
<span id="cb129-6"><a href="#cb129-6" aria-hidden="true" tabindex="-1"></a> (plot <span class="at">:xlab</span> <span class="st">"t"</span></span>
<span id="cb129-7"><a href="#cb129-7" aria-hidden="true" tabindex="-1"></a> <span class="at">:ylab</span> <span class="st">"y"</span></span>
<span id="cb129-8"><a href="#cb129-8" aria-hidden="true" tabindex="-1"></a> <span class="at">:type</span> <span class="st">"l"</span>))))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<!--?xml version='1.0' encoding='UTF-8' ?-->
<svg xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink" class="svglite" width="720.00pt" height="576.00pt" viewbox="0 0 720.00 576.00">
<defs>
<style type="text/css">
.svglite line, .svglite polyline, .svglite polygon, .svglite path, .svglite rect, .svglite circle {
fill: none;
stroke: #000000;
stroke-linecap: round;
stroke-linejoin: round;
stroke-miterlimit: 10.00;
}
.svglite text {
white-space: pre;
}
</style>
</defs>
<rect width="100%" height="100%" style="stroke: none; fill: #FFFFFF;"></rect>
<defs>
<clippath id="cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=">
<rect x="0.00" y="0.00" width="720.00" height="576.00"></rect>
</clippath>
</defs>
<g clip-path="url(#cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=)">
</g>
<defs>
<clippath id="cpNTkuMDR8Njg5Ljc2fDU5LjA0fDUwMi41Ng==">
<rect x="59.04" y="59.04" width="630.72" height="443.52"></rect>
</clippath>
</defs>
<g clip-path="url(#cpNTkuMDR8Njg5Ljc2fDU5LjA0fDUwMi41Ng==)">
<polyline points="82.40,486.13 102.54,474.46 122.68,471.57 142.81,450.06 162.95,445.56 183.09,431.02 203.23,412.26 223.37,397.54 243.50,379.30 263.64,365.81 283.78,347.02 303.92,337.13 324.06,327.09 344.19,313.56 364.33,292.32 384.47,269.71 404.61,250.23 424.74,238.44 444.88,217.15 465.02,215.11 485.16,202.36 505.30,193.19 525.43,167.72 545.57,156.90 565.71,156.48 585.85,132.04 605.99,129.55 626.12,119.73 646.26,96.96 666.40,75.47 " style="stroke-width: 0.75;"></polyline>
</g>
<g clip-path="url(#cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=)">
<line x1="62.26" y1="502.56" x2="666.40" y2="502.56" style="stroke-width: 0.75;"></line>
<line x1="62.26" y1="502.56" x2="62.26" y2="509.76" style="stroke-width: 0.75;"></line>
<line x1="162.95" y1="502.56" x2="162.95" y2="509.76" style="stroke-width: 0.75;"></line>
<line x1="263.64" y1="502.56" x2="263.64" y2="509.76" style="stroke-width: 0.75;"></line>
<line x1="364.33" y1="502.56" x2="364.33" y2="509.76" style="stroke-width: 0.75;"></line>
<line x1="465.02" y1="502.56" x2="465.02" y2="509.76" style="stroke-width: 0.75;"></line>
<line x1="565.71" y1="502.56" x2="565.71" y2="509.76" style="stroke-width: 0.75;"></line>
<line x1="666.40" y1="502.56" x2="666.40" y2="509.76" style="stroke-width: 0.75;"></line>
<text x="62.26" y="528.48" text-anchor="middle" style="font-size: 12.00px; font-family: "Liberation Sans";" textlength="7.64px" lengthadjust="spacingAndGlyphs">0</text>
<text x="162.95" y="528.48" text-anchor="middle" style="font-size: 12.00px; font-family: "Liberation Sans";" textlength="7.64px" lengthadjust="spacingAndGlyphs">5</text>
<text x="263.64" y="528.48" text-anchor="middle" style="font-size: 12.00px; font-family: "Liberation Sans";" textlength="15.28px" lengthadjust="spacingAndGlyphs">10</text>
<text x="364.33" y="528.48" text-anchor="middle" style="font-size: 12.00px; font-family: "Liberation Sans";" textlength="15.28px" lengthadjust="spacingAndGlyphs">15</text>
<text x="465.02" y="528.48" text-anchor="middle" style="font-size: 12.00px; font-family: "Liberation Sans";" textlength="15.28px" lengthadjust="spacingAndGlyphs">20</text>
<text x="565.71" y="528.48" text-anchor="middle" style="font-size: 12.00px; font-family: "Liberation Sans";" textlength="15.28px" lengthadjust="spacingAndGlyphs">25</text>
<text x="666.40" y="528.48" text-anchor="middle" style="font-size: 12.00px; font-family: "Liberation Sans";" textlength="15.28px" lengthadjust="spacingAndGlyphs">30</text>
<line x1="59.04" y1="498.25" x2="59.04" y2="108.75" style="stroke-width: 0.75;"></line>
<line x1="59.04" y1="498.25" x2="51.84" y2="498.25" style="stroke-width: 0.75;"></line>
<line x1="59.04" y1="368.42" x2="51.84" y2="368.42" style="stroke-width: 0.75;"></line>
<line x1="59.04" y1="238.58" x2="51.84" y2="238.58" style="stroke-width: 0.75;"></line>
<line x1="59.04" y1="108.75" x2="51.84" y2="108.75" style="stroke-width: 0.75;"></line>
<text transform="translate(41.76,498.25) rotate(-90)" text-anchor="middle" style="font-size: 12.00px; font-family: "Liberation Sans";" textlength="7.64px" lengthadjust="spacingAndGlyphs">0</text>
<text transform="translate(41.76,368.42) rotate(-90)" text-anchor="middle" style="font-size: 12.00px; font-family: "Liberation Sans";" textlength="7.64px" lengthadjust="spacingAndGlyphs">5</text>
<text transform="translate(41.76,238.58) rotate(-90)" text-anchor="middle" style="font-size: 12.00px; font-family: "Liberation Sans";" textlength="15.28px" lengthadjust="spacingAndGlyphs">10</text>
<text transform="translate(41.76,108.75) rotate(-90)" text-anchor="middle" style="font-size: 12.00px; font-family: "Liberation Sans";" textlength="15.28px" lengthadjust="spacingAndGlyphs">15</text>
<polygon points="59.04,502.56 689.76,502.56 689.76,59.04 59.04,59.04 " style="stroke-width: 0.75;"></polygon>
<text x="374.40" y="557.28" text-anchor="middle" style="font-size: 12.00px; font-family: "Liberation Sans";" textlength="4.71px" lengthadjust="spacingAndGlyphs">t</text>
<text transform="translate(12.96,280.80) rotate(-90)" text-anchor="middle" style="font-size: 12.00px; font-family: "Liberation Sans";" textlength="7.11px" lengthadjust="spacingAndGlyphs">y</text>
</g>
</svg>
<p>ggplot2 plots (or any other plot objects like lattice) can be also turned into SVG.</p>
<div class="sourceClojure">
<div class="sourceCode" id="cb130"><pre class="sourceCode clojure code-with-copy"><code class="sourceCode clojure"><span id="cb130-1"><a href="#cb130-1" aria-hidden="true" tabindex="-1"></a>(plot->svg</span>
<span id="cb130-2"><a href="#cb130-2" aria-hidden="true" tabindex="-1"></a> (<span class="kw">let</span> [x (<span class="kw">repeatedly</span> <span class="dv">99</span> <span class="kw">rand</span>)</span>
<span id="cb130-3"><a href="#cb130-3" aria-hidden="true" tabindex="-1"></a> y (<span class="kw">map</span> <span class="kw">+</span></span>
<span id="cb130-4"><a href="#cb130-4" aria-hidden="true" tabindex="-1"></a> x</span>
<span id="cb130-5"><a href="#cb130-5" aria-hidden="true" tabindex="-1"></a> (<span class="kw">repeatedly</span> <span class="dv">99</span> <span class="kw">rand</span>))]</span>
<span id="cb130-6"><a href="#cb130-6" aria-hidden="true" tabindex="-1"></a> (<span class="kw">-></span> {<span class="at">:x</span> x <span class="at">:y</span> y}</span>
<span id="cb130-7"><a href="#cb130-7" aria-hidden="true" tabindex="-1"></a> dataset/->dataset</span>
<span id="cb130-8"><a href="#cb130-8" aria-hidden="true" tabindex="-1"></a> (ggplot (aes <span class="at">:x</span> x</span>
<span id="cb130-9"><a href="#cb130-9" aria-hidden="true" tabindex="-1"></a> <span class="at">:y</span> y</span>
<span id="cb130-10"><a href="#cb130-10" aria-hidden="true" tabindex="-1"></a> <span class="at">:color</span> '(<span class="kw">+</span> x y)</span>
<span id="cb130-11"><a href="#cb130-11" aria-hidden="true" tabindex="-1"></a> <span class="at">:size</span> '(<span class="kw">/</span> x y)))</span>
<span id="cb130-12"><a href="#cb130-12" aria-hidden="true" tabindex="-1"></a> (r+ (geom_point)</span>
<span id="cb130-13"><a href="#cb130-13" aria-hidden="true" tabindex="-1"></a> (xlab <span class="st">"x"</span>)</span>
<span id="cb130-14"><a href="#cb130-14" aria-hidden="true" tabindex="-1"></a> (ylab <span class="st">"y"</span>)))))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<!--?xml version='1.0' encoding='UTF-8' ?-->
<svg xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink" class="svglite" width="720.00pt" height="576.00pt" viewbox="0 0 720.00 576.00">
<defs>
<style type="text/css">
.svglite line, .svglite polyline, .svglite polygon, .svglite path, .svglite rect, .svglite circle {
fill: none;
stroke: #000000;
stroke-linecap: round;
stroke-linejoin: round;
stroke-miterlimit: 10.00;
}
.svglite text {
white-space: pre;
}
</style>
</defs>
<rect width="100%" height="100%" style="stroke: none; fill: #FFFFFF;"></rect>
<defs>
<clippath id="cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=">
<rect x="0.00" y="0.00" width="720.00" height="576.00"></rect>
</clippath>
</defs>
<g clip-path="url(#cpMC4wMHw3MjAuMDB8MC4wMHw1NzYuMDA=)">
<rect x="-0.000000000000064" y="0.00" width="720.00" height="576.00" style="stroke-width: 1.07; stroke: #FFFFFF; fill: #FFFFFF;"></rect>
</g>
<defs>
<clippath id="cpMzcuNDR8NjUwLjI3fDUuNDh8NTQ0LjI5">
<rect x="37.44" y="5.48" width="612.82" height="538.82"></rect>
</clippath>
</defs>
<g clip-path="url(#cpMzcuNDR8NjUwLjI3fDUuNDh8NTQ0LjI5)">
<rect x="37.44" y="5.48" width="612.82" height="538.82" style="stroke-width: 1.07; stroke: none; fill: #EBEBEB;"></rect>
<polyline points="37.44,463.19 650.27,463.19 " style="stroke-width: 0.53; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="37.44,320.92 650.27,320.92 " style="stroke-width: 0.53; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="37.44,178.65 650.27,178.65 " style="stroke-width: 0.53; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="37.44,36.38 650.27,36.38 " style="stroke-width: 0.53; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="129.18,544.29 129.18,5.48 " style="stroke-width: 0.53; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="270.51,544.29 270.51,5.48 " style="stroke-width: 0.53; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="411.84,544.29 411.84,5.48 " style="stroke-width: 0.53; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="553.17,544.29 553.17,5.48 " style="stroke-width: 0.53; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="37.44,534.32 650.27,534.32 " style="stroke-width: 1.07; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="37.44,392.05 650.27,392.05 " style="stroke-width: 1.07; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="37.44,249.78 650.27,249.78 " style="stroke-width: 1.07; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="37.44,107.51 650.27,107.51 " style="stroke-width: 1.07; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="58.51,544.29 58.51,5.48 " style="stroke-width: 1.07; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="199.84,544.29 199.84,5.48 " style="stroke-width: 1.07; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="341.18,544.29 341.18,5.48 " style="stroke-width: 1.07; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="482.51,544.29 482.51,5.48 " style="stroke-width: 1.07; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<polyline points="623.84,544.29 623.84,5.48 " style="stroke-width: 1.07; stroke: #FFFFFF; stroke-linecap: butt;"></polyline>
<circle cx="267.21" cy="152.59" r="4.17" style="stroke-width: 0.71; stroke: #3A79AC; fill: #3A79AC;"></circle>
<circle cx="616.01" cy="33.88" r="5.41" style="stroke-width: 0.71; stroke: #56B1F7; fill: #56B1F7;"></circle>
<circle cx="186.81" cy="200.47" r="3.70" style="stroke-width: 0.71; stroke: #326997; fill: #326997;"></circle>
<circle cx="405.38" cy="349.05" r="6.62" style="stroke-width: 0.71; stroke: #2F638E; fill: #2F638E;"></circle>
<circle cx="333.79" cy="327.01" r="5.78" style="stroke-width: 0.71; stroke: #2E608B; fill: #2E608B;"></circle>
<circle cx="477.69" cy="246.90" r="6.00" style="stroke-width: 0.71; stroke: #3B7CAF; fill: #3B7CAF;"></circle>
<circle cx="371.55" cy="147.29" r="4.80" style="stroke-width: 0.71; stroke: #4084BB; fill: #4084BB;"></circle>