galaaz 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +29 -0
  3. data/bin/gknit +208 -10
  4. data/bin/gknit2 +14 -0
  5. data/bin/gknit2~ +6 -0
  6. data/bin/prepareR.rb +3 -0
  7. data/bin/prepareR.rb~ +1 -0
  8. data/bin/tmp.py +51 -0
  9. data/blogs/dev/dev.Rmd +70 -0
  10. data/blogs/dev/dev.Rmd~ +104 -0
  11. data/blogs/dev/dev.html +209 -0
  12. data/blogs/dev/dev.md +72 -0
  13. data/blogs/dev/dev_files/figure-html/bubble-1.png +0 -0
  14. data/blogs/dev/model.rb +41 -0
  15. data/blogs/galaaz_ggplot/galaaz_ggplot.Rmd +55 -27
  16. data/blogs/galaaz_ggplot/galaaz_ggplot.aux +44 -0
  17. data/blogs/galaaz_ggplot/galaaz_ggplot.dvi +0 -0
  18. data/blogs/galaaz_ggplot/galaaz_ggplot.html +17 -4
  19. data/blogs/galaaz_ggplot/galaaz_ggplot.out +10 -0
  20. data/blogs/galaaz_ggplot/galaaz_ggplot.pdf +0 -0
  21. data/blogs/galaaz_ggplot/galaaz_ggplot.tex +630 -0
  22. data/blogs/galaaz_ggplot/midwest.Rmd +1 -1
  23. data/blogs/galaaz_ggplot/midwest_external_png +13 -0
  24. data/blogs/galaaz_ggplot/midwest_external_png~ +1 -0
  25. data/blogs/gknit/gknit.Rmd +500 -0
  26. data/blogs/gknit/gknit.Rmd~ +184 -0
  27. data/blogs/gknit/gknit.Rnd~ +17 -0
  28. data/blogs/gknit/gknit.html +528 -0
  29. data/blogs/gknit/gknit.md +628 -0
  30. data/blogs/gknit/gknit.pdf +0 -0
  31. data/blogs/gknit/gknit.tex +745 -0
  32. data/blogs/gknit/gknit_files/figure-html/bubble-1.png +0 -0
  33. data/blogs/gknit/gknit_files/figure-html/diverging_bar.png +0 -0
  34. data/blogs/gknit/model.rb +41 -0
  35. data/blogs/gknit/model.rb~ +46 -0
  36. data/blogs/ruby_plot/figures/dose_len.png +0 -0
  37. data/blogs/ruby_plot/figures/facet_by_delivery.png +0 -0
  38. data/blogs/ruby_plot/figures/facet_by_dose.png +0 -0
  39. data/blogs/ruby_plot/figures/facets_by_delivery_color.png +0 -0
  40. data/blogs/ruby_plot/figures/facets_by_delivery_color2.png +0 -0
  41. data/blogs/ruby_plot/figures/facets_with_decorations.png +0 -0
  42. data/blogs/ruby_plot/figures/facets_with_jitter.png +0 -0
  43. data/blogs/ruby_plot/figures/facets_with_points.png +0 -0
  44. data/blogs/ruby_plot/figures/final_box_plot.png +0 -0
  45. data/blogs/ruby_plot/figures/final_violin_plot.png +0 -0
  46. data/blogs/ruby_plot/figures/violin_with_jitter.png +0 -0
  47. data/blogs/ruby_plot/ruby_plot.Rmd +680 -0
  48. data/blogs/ruby_plot/ruby_plot.Rmd~ +215 -0
  49. data/blogs/ruby_plot/ruby_plot.html +563 -0
  50. data/blogs/ruby_plot/ruby_plot.md +731 -0
  51. data/blogs/ruby_plot/ruby_plot.pdf +0 -0
  52. data/blogs/ruby_plot/ruby_plot.tex +458 -0
  53. data/examples/sthda_ggplot/all.rb +0 -6
  54. data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_hex.rb +1 -1
  55. data/examples/sthda_ggplot/two_variables_cont_cont/misc.rb +1 -1
  56. data/examples/sthda_ggplot/two_variables_disc_cont/geom_bar.rb +2 -2
  57. data/examples/sthda_ggplot/two_variables_disc_disc/geom_jitter.rb +0 -1
  58. data/lib/R/eng_ruby.R +62 -0
  59. data/lib/R/eng_ruby.R~ +63 -0
  60. data/lib/R_interface/capture_plot.rb~ +23 -0
  61. data/lib/{R → R_interface}/expression.rb +0 -0
  62. data/lib/{R → R_interface}/r.rb +10 -1
  63. data/lib/{R → R_interface}/r.rb~ +0 -0
  64. data/lib/{R → R_interface}/r_methods.rb +21 -5
  65. data/lib/{R → R_interface}/rbinary_operators.rb +6 -1
  66. data/lib/R_interface/rclosure.rb +38 -0
  67. data/lib/{R → R_interface}/rdata_frame.rb +0 -0
  68. data/lib/R_interface/rdevices.R +31 -0
  69. data/lib/R_interface/rdevices.rb +225 -0
  70. data/lib/{R/rclosure.rb → R_interface/rdevices.rb~} +3 -10
  71. data/lib/{R → R_interface}/renvironment.rb +0 -0
  72. data/lib/{R → R_interface}/rexpression.rb +0 -0
  73. data/lib/{R → R_interface}/rindexed_object.rb +0 -0
  74. data/lib/{R → R_interface}/rlanguage.rb +0 -0
  75. data/lib/{R → R_interface}/rlist.rb +0 -0
  76. data/lib/{R → R_interface}/rmatrix.rb +0 -0
  77. data/lib/{R → R_interface}/rmd_indexed_object.rb +0 -0
  78. data/lib/{R → R_interface}/robject.rb +5 -0
  79. data/lib/{R → R_interface}/rpkg.rb +0 -0
  80. data/lib/{R → R_interface}/rsupport.rb +49 -13
  81. data/lib/{R → R_interface}/rsupport_scope.rb +0 -0
  82. data/lib/{R → R_interface}/rsymbol.rb +1 -0
  83. data/lib/{R → R_interface}/ruby_callback.rb +0 -0
  84. data/lib/{R → R_interface}/ruby_extensions.rb +2 -1
  85. data/lib/{R → R_interface}/runary_operators.rb +0 -0
  86. data/lib/{R → R_interface}/rvector.rb +0 -0
  87. data/lib/galaaz.rb +4 -2
  88. data/lib/gknit.rb +27 -0
  89. data/lib/gknit.rb~ +26 -0
  90. data/lib/gknit/knitr_engine.rb +120 -0
  91. data/lib/gknit/knitr_engine.rb~ +102 -0
  92. data/lib/gknit/ruby_engine.rb +70 -0
  93. data/lib/gknit/ruby_engine.rb~ +72 -0
  94. data/lib/util/exec_ruby.rb +8 -7
  95. data/lib/util/inline_file.rb +70 -0
  96. data/lib/util/inline_file.rb~ +23 -0
  97. data/r_requires/ggplot.rb +1 -8
  98. data/r_requires/knitr.rb +27 -0
  99. data/r_requires/knitr.rb~ +4 -0
  100. data/specs/r_language.spec.rb +22 -0
  101. data/specs/r_plots.spec.rb +72 -0
  102. data/specs/r_plots.spec.rb~ +37 -0
  103. data/specs/tmp.rb +255 -1
  104. data/version.rb +1 -1
  105. metadata +89 -39
@@ -0,0 +1,44 @@
1
+ \relax
2
+ \providecommand\hyper@newdestlabel[2]{}
3
+ \providecommand\HyperFirstAtBeginDocument{\AtBeginDocument}
4
+ \HyperFirstAtBeginDocument{\ifx\hyper@anchor\@undefined
5
+ \global\let\oldcontentsline\contentsline
6
+ \gdef\contentsline#1#2#3#4{\oldcontentsline{#1}{#2}{#3}}
7
+ \global\let\oldnewlabel\newlabel
8
+ \gdef\newlabel#1#2{\newlabelxx{#1}#2}
9
+ \gdef\newlabelxx#1#2#3#4#5#6{\oldnewlabel{#1}{{#2}{#3}}}
10
+ \AtEndDocument{\ifx\hyper@anchor\@undefined
11
+ \let\contentsline\oldcontentsline
12
+ \let\newlabel\oldnewlabel
13
+ \fi}
14
+ \fi}
15
+ \global\let\hyper@last\relax
16
+ \gdef\HyperFirstAtBeginDocument#1{#1}
17
+ \providecommand\HyField@AuxAddToFields[1]{}
18
+ \providecommand\HyField@AuxAddToCoFields[2]{}
19
+ \providecommand*\new@tpo@label[2]{}
20
+ \@writefile{toc}{\contentsline {section}{\numberline {1}Introduction}{1}{section.1}}
21
+ \newlabel{introduction}{{1}{1}{Introduction}{section.1}{}}
22
+ \@writefile{toc}{\contentsline {subsection}{\numberline {1.1}What does Galaaz mean}{1}{subsection.1.1}}
23
+ \newlabel{what-does-galaaz-mean}{{1.1}{1}{What does Galaaz mean}{subsection.1.1}{}}
24
+ \@writefile{toc}{\contentsline {section}{\numberline {2}Galaaz Demo}{2}{section.2}}
25
+ \newlabel{galaaz-demo}{{2}{2}{Galaaz Demo}{section.2}{}}
26
+ \@writefile{toc}{\contentsline {subsection}{\numberline {2.1}Prerequisites}{2}{subsection.2.1}}
27
+ \newlabel{prerequisites}{{2.1}{2}{Prerequisites}{subsection.2.1}{}}
28
+ \@writefile{toc}{\contentsline {subsection}{\numberline {2.2}Preparation}{2}{subsection.2.2}}
29
+ \newlabel{preparation}{{2.2}{2}{Preparation}{subsection.2.2}{}}
30
+ \@writefile{toc}{\contentsline {subsection}{\numberline {2.3}Running the demo}{2}{subsection.2.3}}
31
+ \newlabel{running-the-demo}{{2.3}{2}{Running the demo}{subsection.2.3}{}}
32
+ \@writefile{toc}{\contentsline {subsection}{\numberline {2.4}Running other demos}{2}{subsection.2.4}}
33
+ \newlabel{running-other-demos}{{2.4}{2}{Running other demos}{subsection.2.4}{}}
34
+ \@writefile{toc}{\contentsline {section}{\numberline {3}The demo code}{3}{section.3}}
35
+ \newlabel{the-demo-code}{{3}{3}{The demo code}{section.3}{}}
36
+ \@writefile{lof}{\contentsline {figure}{\numberline {1}{\ignorespaces Midwest Plot}}{4}{figure.1}}
37
+ \@writefile{toc}{\contentsline {section}{\numberline {4}An extension to the example}{4}{section.4}}
38
+ \newlabel{an-extension-to-the-example}{{4}{4}{An extension to the example}{section.4}{}}
39
+ \@writefile{toc}{\contentsline {section}{\numberline {5}Conclusion}{7}{section.5}}
40
+ \newlabel{conclusion}{{5}{7}{Conclusion}{section.5}{}}
41
+ \@writefile{lof}{\contentsline {figure}{\numberline {2}{\ignorespaces Midwest Plot with `glm' function and modified theme}}{8}{figure.2}}
42
+ \newlabel{LastPage}{{}{8}{}{page.8}{}}
43
+ \xdef\lastpage@lastpage{8}
44
+ \xdef\lastpage@lastpageHy{8}
@@ -133,7 +133,7 @@ $(document).ready(function () {
133
133
  <div id="introduction" class="section level1">
134
134
  <h1>Introduction</h1>
135
135
  <p>Galaaz is a system for tightly coupling Ruby and R. Ruby is a powerful language, with a large community, a very large set of libraries and great for web development. However, it lacks libraries for data science, statistics, scientific plotting and machine learning. On the other hand, R is considered one of the most powerful languages for solving all of the above problems. Maybe the strongest competitor to R is Python with libraries such as NumPy, Panda, SciPy, SciKit-Learn and a couple more.</p>
136
- <p>With Galaaz we do not intend to re-implement any of the scientific libraries in R, we allow for very tight coupling between the two languages to the point that the Ruby developer does not need to know that there is an R engine running. For this to happen we use new technologies provided by Oracle, with GraalVM, TruffleRuby and FastR:</p>
136
+ <p>With Galaaz we do not intend to re-implement any of the scientific libraries in R, we allow for very tight coupling between the two languages to the point that the Ruby developer does not need to know that there is an R engine running. For this to happen we use new technologies provided by Oracle: GraalVM, TruffleRuby and FastR:</p>
137
137
  <pre><code> GraalVM is a universal virtual machine for running applications written in JavaScript,
138
138
  Python 3, Ruby, R, JVM-based languages like Java, Scala, Kotlin, and LLVM-based languages
139
139
  such as C and C++.
@@ -143,7 +143,7 @@ $(document).ready(function () {
143
143
  Oracle Database, or MySQL.
144
144
 
145
145
  GraalVM allows you to write polyglot applications with a seamless way to pass values from one
146
- language to another. With GraalVM there is no copying or marshalling necessary as it is with
146
+ language to another. With GraalVM there is no copying or marshaling necessary as it is with
147
147
  other polyglot systems. This lets you achieve high performance when language boundaries are
148
148
  crossed. Most of the time there is no additional cost for crossing a language boundary at all.
149
149
 
@@ -156,6 +156,13 @@ $(document).ready(function () {
156
156
 
157
157
  With GraalVM we aim to allow developers to freely choose the right language for the task at
158
158
  hand without making compromises.</code></pre>
159
+ <p>Interested readers should also check out the following sites:</p>
160
+ <ul>
161
+ <li><a href="https://www.graalvm.org/">GraalVM Home</a></li>
162
+ <li><a href="https://github.com/oracle/truffleruby">TruffleRuby</a></li>
163
+ <li><a href="https://github.com/oracle/fastr">FastR</a></li>
164
+ <li><a href="https://medium.com/graalvm/faster-r-with-fastr-4b8db0e0dceb">Faster R with FastR</a></li>
165
+ </ul>
159
166
  <div id="what-does-galaaz-mean" class="section level2">
160
167
  <h2>What does Galaaz mean</h2>
161
168
  <p>Galaaz is the Portuguese name for “Galahad”. From Wikipedia:</p>
@@ -273,8 +280,8 @@ plot(gg)</code></pre>
273
280
  <div id="an-extension-to-the-example" class="section level1">
274
281
  <h1>An extension to the example</h1>
275
282
  <p>If both codes are so similar, then why would one use Ruby instead of R and what good is galaaz after all?</p>
276
- <p>Ruby is a modern OO language with numerous very useful constructs such as classes, modules, blocks, procs, etc. The example above focus on the coupling of both languages, and does not show the use of other Ruby constructs. In the following example, we will show a more complex example using other Ruby constructs. This is certaily not a very well written and robust Ruby code, but it give the idea of how Ruby and R are stronglyg coupled.</p>
277
- <p>Let’s imagine that we work in a corporation that has its plot themes. So, it has defined a ‘CorpTheme’ module. Plots in this corporation should not have grids, numbers in labels should not use scientific notation and the prefered color is blue.</p>
283
+ <p>Ruby is a modern OO language with numerous very useful constructs such as classes, modules, blocks, procs, etc. The example above focus on the coupling of both languages, and does not show the use of other Ruby constructs. In the following example, we will show a more complex example using other Ruby constructs. This is certainly not a very well written and robust Ruby code, but it give the idea of how Ruby and R are strongly coupled.</p>
284
+ <p>Let’s imagine that we work in a corporation that has its plot themes. So, it has defined a ‘CorpTheme’ module. Plots in this corporation should not have grids, numbers in labels should not use scientific notation and the preferred color is blue.</p>
278
285
  <pre class="galaaz"><code># corp_theme.rb
279
286
  # defines the corporate theme for all plots
280
287
 
@@ -427,6 +434,12 @@ sp.plot('svg')
427
434
  <p class="caption">Midwest Plot with ‘glm’ function and modified theme</p>
428
435
  </div>
429
436
  </div>
437
+ <div id="conclusion" class="section level1">
438
+ <h1>Conclusion</h1>
439
+ <p>R is a very powerful language for statistical analysis, data analytics, machine learning, plotting and many other scientific applications with a very large package ecosystem. However R is often considered hard to learn and lacking modern computer languages constructs such as object oriented classes, modules, lambdas, etc. For this reason, many developers have started or switched from R to Python.</p>
440
+ <p>With Galaaz, R programmers can almost transparently migrate from R to Ruby, since syntax is almost identical and they have fastR as the R engine. FastR, by most benchmarks, can be orders of magnitude faster than Gnu R. Further, by using Galaaz the R developer can start (slowly if needed) using all of Ruby’s constructs and libraries that nicely complement R packages.</p>
441
+ <p>For the Ruby developer, Galaaz allows the immediate use of R functions completely transparently. As shown in the second example above, class ScatterPlot completely hides all the details an R calls from the Ruby developer, furthermore Galaaz is powered by TruffleRuby that can also be orders of magnitude faster than MRI Ruby.</p>
442
+ </div>
430
443
 
431
444
 
432
445
 
@@ -0,0 +1,10 @@
1
+ \BOOKMARK [1][-]{section.1}{\376\377\000I\000n\000t\000r\000o\000d\000u\000c\000t\000i\000o\000n}{}% 1
2
+ \BOOKMARK [2][-]{subsection.1.1}{\376\377\000W\000h\000a\000t\000\040\000d\000o\000e\000s\000\040\000G\000a\000l\000a\000a\000z\000\040\000m\000e\000a\000n}{section.1}% 2
3
+ \BOOKMARK [1][-]{section.2}{\376\377\000G\000a\000l\000a\000a\000z\000\040\000D\000e\000m\000o}{}% 3
4
+ \BOOKMARK [2][-]{subsection.2.1}{\376\377\000P\000r\000e\000r\000e\000q\000u\000i\000s\000i\000t\000e\000s}{section.2}% 4
5
+ \BOOKMARK [2][-]{subsection.2.2}{\376\377\000P\000r\000e\000p\000a\000r\000a\000t\000i\000o\000n}{section.2}% 5
6
+ \BOOKMARK [2][-]{subsection.2.3}{\376\377\000R\000u\000n\000n\000i\000n\000g\000\040\000t\000h\000e\000\040\000d\000e\000m\000o}{section.2}% 6
7
+ \BOOKMARK [2][-]{subsection.2.4}{\376\377\000R\000u\000n\000n\000i\000n\000g\000\040\000o\000t\000h\000e\000r\000\040\000d\000e\000m\000o\000s}{section.2}% 7
8
+ \BOOKMARK [1][-]{section.3}{\376\377\000T\000h\000e\000\040\000d\000e\000m\000o\000\040\000c\000o\000d\000e}{}% 8
9
+ \BOOKMARK [1][-]{section.4}{\376\377\000A\000n\000\040\000e\000x\000t\000e\000n\000s\000i\000o\000n\000\040\000t\000o\000\040\000t\000h\000e\000\040\000e\000x\000a\000m\000p\000l\000e}{}% 9
10
+ \BOOKMARK [1][-]{section.5}{\376\377\000C\000o\000n\000c\000l\000u\000s\000i\000o\000n}{}% 10
@@ -0,0 +1,630 @@
1
+ \documentclass[]{article}
2
+ \usepackage{lmodern}
3
+ \usepackage{amssymb,amsmath}
4
+ \usepackage{ifxetex,ifluatex}
5
+ \usepackage{fixltx2e} % provides \textsubscript
6
+ \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
7
+ \usepackage[T1]{fontenc}
8
+ \usepackage[utf8]{inputenc}
9
+ \else % if luatex or xelatex
10
+ \ifxetex
11
+ \usepackage{mathspec}
12
+ \else
13
+ \usepackage{fontspec}
14
+ \fi
15
+ \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
16
+ \fi
17
+ % use upquote if available, for straight quotes in verbatim environments
18
+ \IfFileExists{upquote.sty}{\usepackage{upquote}}{}
19
+ % use microtype if available
20
+ \IfFileExists{microtype.sty}{%
21
+ \usepackage{microtype}
22
+ \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
23
+ }{}
24
+ \usepackage[margin=1in]{geometry}
25
+ \usepackage{hyperref}
26
+ \hypersetup{unicode=true,
27
+ pdftitle={Ruby Plotting with Galaaz},
28
+ pdfauthor={Rodrigo Botafogo},
29
+ pdfborder={0 0 0},
30
+ breaklinks=true}
31
+ \urlstyle{same} % don't use monospace font for urls
32
+ \usepackage{color}
33
+ \usepackage{fancyvrb}
34
+ \newcommand{\VerbBar}{|}
35
+ \newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
36
+ \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
37
+ % Add ',fontsize=\small' for more characters per line
38
+ \usepackage{framed}
39
+ \definecolor{shadecolor}{RGB}{248,248,248}
40
+ \newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
41
+ \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
42
+ \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
43
+ \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
44
+ \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
45
+ \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
46
+ \newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
47
+ \newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
48
+ \newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
49
+ \newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
50
+ \newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
51
+ \newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
52
+ \newcommand{\ImportTok}[1]{#1}
53
+ \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
54
+ \newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
55
+ \newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
56
+ \newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
57
+ \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
58
+ \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
59
+ \newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
60
+ \newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
61
+ \newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
62
+ \newcommand{\BuiltInTok}[1]{#1}
63
+ \newcommand{\ExtensionTok}[1]{#1}
64
+ \newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
65
+ \newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
66
+ \newcommand{\RegionMarkerTok}[1]{#1}
67
+ \newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
68
+ \newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
69
+ \newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
70
+ \newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
71
+ \newcommand{\NormalTok}[1]{#1}
72
+ \usepackage{graphicx,grffile}
73
+ \makeatletter
74
+ \def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
75
+ \def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
76
+ \makeatother
77
+ % Scale images if necessary, so that they will not overflow the page
78
+ % margins by default, and it is still possible to overwrite the defaults
79
+ % using explicit options in \includegraphics[width, height, ...]{}
80
+ \setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
81
+ \IfFileExists{parskip.sty}{%
82
+ \usepackage{parskip}
83
+ }{% else
84
+ \setlength{\parindent}{0pt}
85
+ \setlength{\parskip}{6pt plus 2pt minus 1pt}
86
+ }
87
+ \setlength{\emergencystretch}{3em} % prevent overfull lines
88
+ \providecommand{\tightlist}{%
89
+ \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
90
+ \setcounter{secnumdepth}{5}
91
+ % Redefines (sub)paragraphs to behave more like sections
92
+ \ifx\paragraph\undefined\else
93
+ \let\oldparagraph\paragraph
94
+ \renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
95
+ \fi
96
+ \ifx\subparagraph\undefined\else
97
+ \let\oldsubparagraph\subparagraph
98
+ \renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
99
+ \fi
100
+
101
+ %%% Use protect on footnotes to avoid problems with footnotes in titles
102
+ \let\rmarkdownfootnote\footnote%
103
+ \def\footnote{\protect\rmarkdownfootnote}
104
+
105
+ %%% Change title format to be more compact
106
+ \usepackage{titling}
107
+
108
+ % Create subtitle command for use in maketitle
109
+ \newcommand{\subtitle}[1]{
110
+ \posttitle{
111
+ \begin{center}\large#1\end{center}
112
+ }
113
+ }
114
+
115
+ \setlength{\droptitle}{-2em}
116
+
117
+ \title{Ruby Plotting with Galaaz}
118
+ \pretitle{\vspace{\droptitle}\centering\huge}
119
+ \posttitle{\par}
120
+ \subtitle{An example of tightly coupling Ruby and R in GraalVM}
121
+ \author{Rodrigo Botafogo}
122
+ \preauthor{\centering\large\emph}
123
+ \postauthor{\par}
124
+ \predate{\centering\large\emph}
125
+ \postdate{\par}
126
+ \date{16 October 2018}
127
+
128
+ % usar portugues do Brasil
129
+ % \usepackage[brazilian]{babel}
130
+ \usepackage[utf8]{inputenc}
131
+
132
+ \usepackage{geometry}
133
+ \geometry{a4paper, top=1in}
134
+
135
+ % necessários para uso com kableExtra
136
+ \usepackage{longtable}
137
+ \usepackage{multirow}
138
+ \usepackage[table]{xcolor}
139
+ \usepackage{wrapfig}
140
+ \usepackage{float}
141
+ \usepackage{colortbl}
142
+ \usepackage{pdflscape}
143
+ \usepackage{tabu}
144
+ \usepackage{threeparttable}
145
+ \usepackage[normalem]{ulem}
146
+
147
+ \usepackage{bbm}
148
+ \usepackage{booktabs}
149
+ \usepackage{expex}
150
+
151
+ \usepackage{graphicx}
152
+ \usepackage{fancyhdr}
153
+ \pagestyle{fancy}
154
+ \fancyhf{}
155
+
156
+ \usepackage{lipsum}
157
+
158
+ % disponibilizar o comando lastpage
159
+ \usepackage{lastpage}
160
+
161
+ % tamanho do font padrão 11pt
162
+ \usepackage[fontsize=10pt]{scrextend}
163
+
164
+ % comandos para formatar uma tabela
165
+ \usepackage{array}
166
+ \newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
167
+ \newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
168
+ \newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
169
+
170
+ % necessário para importar outros arquivos latex
171
+ \usepackage{import}
172
+
173
+ \newcommand{\RtoLatex}[2]{\newcommand{#1}{#2}}
174
+ %\newcommand{\atraso}[1]{\color{red} \textbf {Tempo desde a Assinatura do Contrato: #1 dias}}
175
+
176
+ \begin{document}
177
+ \maketitle
178
+
179
+ \section{Introduction}\label{introduction}
180
+
181
+ Galaaz is a system for tightly coupling Ruby and R. Ruby is a powerful
182
+ language, with a large community, a very large set of libraries and
183
+ great for web development. However, it lacks libraries for data science,
184
+ statistics, scientific plotting and machine learning. On the other hand,
185
+ R is considered one of the most powerful languages for solving all of
186
+ the above problems. Maybe the strongest competitor to R is Python with
187
+ libraries such as NumPy, Panda, SciPy, SciKit-Learn and a couple more.
188
+
189
+ With Galaaz we do not intend to re-implement any of the scientific
190
+ libraries in R, we allow for very tight coupling between the two
191
+ languages to the point that the Ruby developer does not need to know
192
+ that there is an R engine running. For this to happen we use new
193
+ technologies provided by Oracle: GraalVM, TruffleRuby and FastR:
194
+
195
+ \begin{verbatim}
196
+ GraalVM is a universal virtual machine for running applications written in JavaScript,
197
+ Python 3, Ruby, R, JVM-based languages like Java, Scala, Kotlin, and LLVM-based languages
198
+ such as C and C++.
199
+
200
+ GraalVM removes the isolation between programming languages and enables interoperability in a
201
+ shared runtime. It can run either standalone or in the context of OpenJDK, Node.js,
202
+ Oracle Database, or MySQL.
203
+
204
+ GraalVM allows you to write polyglot applications with a seamless way to pass values from one
205
+ language to another. With GraalVM there is no copying or marshaling necessary as it is with
206
+ other polyglot systems. This lets you achieve high performance when language boundaries are
207
+ crossed. Most of the time there is no additional cost for crossing a language boundary at all.
208
+
209
+ Often developers have to make uncomfortable compromises that require them to rewrite
210
+ their software in other languages. For example:
211
+
212
+ * “That library is not available in my language. I need to rewrite it.”
213
+ * “That language would be the perfect fit for my problem, but we cannot run it in our environment.”
214
+ * “That problem is already solved in my language, but the language is too slow.”
215
+
216
+ With GraalVM we aim to allow developers to freely choose the right language for the task at
217
+ hand without making compromises.
218
+ \end{verbatim}
219
+
220
+ Interested readers should also check out the following sites:
221
+
222
+ \begin{itemize}
223
+ \tightlist
224
+ \item
225
+ \href{https://www.graalvm.org/}{GraalVM Home}
226
+ \item
227
+ \href{https://github.com/oracle/truffleruby}{TruffleRuby}
228
+ \item
229
+ \href{https://github.com/oracle/fastr}{FastR}
230
+ \item
231
+ \href{https://medium.com/graalvm/faster-r-with-fastr-4b8db0e0dceb}{Faster
232
+ R with FastR}
233
+ \end{itemize}
234
+
235
+ \subsection{What does Galaaz mean}\label{what-does-galaaz-mean}
236
+
237
+ Galaaz is the Portuguese name for ``Galahad''. From Wikipedia:
238
+
239
+ \begin{verbatim}
240
+ Sir Galahad (sometimes referred to as Galeas or Galath),
241
+ in Arthurian legend, is a knight of King Arthur's Round Table and one of the three
242
+ achievers of the Holy Grail. He is the illegitimate son of Sir Lancelot and Elaine of
243
+ Corbenic, and is renowned for his gallantry and purity as the most perfect of all knights.
244
+ Emerging quite late in the medieval Arthurian tradition, Sir Galahad first appears in the
245
+ Lancelot–Grail cycle, and his story is taken up in later works such as the Post-Vulgate
246
+ Cycle and Sir Thomas Malory's Le Morte d'Arthur. His name should not be mistaken with
247
+ Galehaut, a different knight from Arthurian legend.
248
+ \end{verbatim}
249
+
250
+ \section{Galaaz Demo}\label{galaaz-demo}
251
+
252
+ \subsection{Prerequisites}\label{prerequisites}
253
+
254
+ \begin{itemize}
255
+ \tightlist
256
+ \item
257
+ GraalVM (\textgreater{}= rc7)
258
+ \item
259
+ TruffleRuby
260
+ \item
261
+ FastR
262
+ \end{itemize}
263
+
264
+ The following R packages will be automatically installed when necessary,
265
+ but could be installed prior to the demo if desired:
266
+
267
+ \begin{itemize}
268
+ \tightlist
269
+ \item
270
+ ggplot2
271
+ \item
272
+ gridExtra
273
+ \end{itemize}
274
+
275
+ Installation of R packages requires a development environment. In Linux,
276
+ the gnu compiler and tools should be enough. I am not sure what is
277
+ needed on the Mac.
278
+
279
+ In order to run the `specs' the following Ruby package is necessary:
280
+
281
+ \begin{itemize}
282
+ \tightlist
283
+ \item
284
+ gem install rspec
285
+ \end{itemize}
286
+
287
+ \subsection{Preparation}\label{preparation}
288
+
289
+ \begin{itemize}
290
+ \tightlist
291
+ \item
292
+ gem install galaaz
293
+ \end{itemize}
294
+
295
+ \subsection{Running the demo}\label{running-the-demo}
296
+
297
+ The ggplot for this demos was extracted from:
298
+ \url{http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html}.
299
+
300
+ On the console do
301
+
302
+ \begin{verbatim}
303
+ > galaaz master_list:scatter_plot
304
+ \end{verbatim}
305
+
306
+ \subsection{Running other demos}\label{running-other-demos}
307
+
308
+ Doing on the console
309
+
310
+ \begin{verbatim}
311
+ > galaaz -T
312
+ \end{verbatim}
313
+
314
+ will show a list with all available demos. To run any of the demos in
315
+ the list, substitute the call to `rake' to `galaaz'. For instance, one
316
+ of the examples in the list is `rake sthda:bar'. In order to run this
317
+ example just do `galaaz sthda:bar'. Doing `galaaz sthda:all' will run
318
+ all demos in the sthda cathegory. Some of the examples require `rspec'
319
+ do be available. To install `rspec' just do `gem install rspec'.
320
+
321
+ \section{The demo code}\label{the-demo-code}
322
+
323
+ The following is the Ruby code and plot for the above example. There is
324
+ a small difference between the code in the example and the code bellow.
325
+ If the example is ran, the plot will appear on the screen, bellow, we
326
+ generate an `svg' image and then include it in this document. In order
327
+ to generate and image, the R.svg device is used. To generate the plot on
328
+ the screen, use the R.awt device, as commented on the code.
329
+
330
+ \begin{Shaded}
331
+ \begin{Highlighting}[]
332
+ \NormalTok{require }\StringTok{'galaaz'}
333
+ \NormalTok{require }\StringTok{'ggplot'}
334
+
335
+ \CommentTok{# load package and data}
336
+ \NormalTok{R.options(}\StringTok{scipen: }\DecValTok{999}\NormalTok{) }\CommentTok{# turn-off scientific notation like 1e+48}
337
+ \NormalTok{R.theme_set(R.theme_bw) }\CommentTok{# pre-set the bw theme.}
338
+
339
+ \NormalTok{midwest = ~}\StringTok{:midwest}
340
+ \CommentTok{# midwest <- read.csv("http://goo.gl/G1K41K") # bkup data source}
341
+
342
+ \CommentTok{# R.awt # run the awt device if the plot should show on the screen}
343
+ \NormalTok{R.svg }\CommentTok{# run the svg device if an image should be generated}
344
+
345
+ \CommentTok{# Scatterplot}
346
+ \NormalTok{gg = midwest.ggplot(E.aes(}\StringTok{x: :area}\NormalTok{, }\StringTok{y: :poptotal}\NormalTok{)) + }
347
+ \NormalTok{ R.geom_point(E.aes(}\StringTok{col: :state}\NormalTok{, }\StringTok{size: :popdensity}\NormalTok{)) + }
348
+ \NormalTok{ R.geom_smooth(}\StringTok{method: "loess"}\NormalTok{, }\StringTok{se: }\DecValTok{false}\NormalTok{) + }
349
+ \NormalTok{ R.xlim(R.c(}\DecValTok{0}\NormalTok{, }\FloatTok{0.1}\NormalTok{)) + }
350
+ \NormalTok{ R.ylim(R.c(}\DecValTok{0}\NormalTok{, }\DecValTok{500000}\NormalTok{)) + }
351
+ \NormalTok{ R.labs(}\StringTok{subtitle: "Area Vs Population"}\NormalTok{, }
352
+ \StringTok{y: "Population"}\NormalTok{, }
353
+ \StringTok{x: "Area"}\NormalTok{, }
354
+ \StringTok{title: "Scatterplot"}\NormalTok{, }
355
+ \StringTok{caption: "Source: midwest"}\NormalTok{)}
356
+
357
+ \NormalTok{R.png(}\StringTok{'midwest.png'}\NormalTok{) }\CommentTok{# this line is not necessary with the awt device}
358
+ \NormalTok{puts gg}
359
+
360
+ \NormalTok{R.dev__off }\CommentTok{# R.dev__off turns off the device. If using awt, the plot}
361
+ \CommentTok{# window will be closed}
362
+ \end{Highlighting}
363
+ \end{Shaded}
364
+
365
+ \begin{figure}
366
+ \centering
367
+ \includegraphics[width=0.70000\textwidth]{midwest.png}
368
+ \caption{Midwest Plot}
369
+ \end{figure}
370
+
371
+ In R, the code to generate this plot is the following
372
+
373
+ \begin{Shaded}
374
+ \begin{Highlighting}[]
375
+ \CommentTok{# install.packages("ggplot2")}
376
+ \CommentTok{# load package and data}
377
+ \KeywordTok{options}\NormalTok{(}\DataTypeTok{scipen=}\DecValTok{999}\NormalTok{) }\CommentTok{# turn-off scientific notation like 1e+48}
378
+ \KeywordTok{library}\NormalTok{(ggplot2)}
379
+ \KeywordTok{theme_set}\NormalTok{(}\KeywordTok{theme_bw}\NormalTok{()) }\CommentTok{# pre-set the bw theme.}
380
+ \KeywordTok{data}\NormalTok{(}\StringTok{"midwest"}\NormalTok{, }\DataTypeTok{package =} \StringTok{"ggplot2"}\NormalTok{)}
381
+ \CommentTok{# midwest <- read.csv("http://goo.gl/G1K41K") # bkup data source}
382
+
383
+ \CommentTok{# Scatterplot}
384
+ \NormalTok{gg <-}\StringTok{ }\KeywordTok{ggplot}\NormalTok{(midwest, }\KeywordTok{aes}\NormalTok{(}\DataTypeTok{x=}\NormalTok{area, }\DataTypeTok{y=}\NormalTok{poptotal)) }\OperatorTok{+}\StringTok{ }
385
+ \StringTok{ }\KeywordTok{geom_point}\NormalTok{(}\KeywordTok{aes}\NormalTok{(}\DataTypeTok{col=}\NormalTok{state, }\DataTypeTok{size=}\NormalTok{popdensity)) }\OperatorTok{+}\StringTok{ }
386
+ \StringTok{ }\KeywordTok{geom_smooth}\NormalTok{(}\DataTypeTok{method=}\StringTok{"loess"}\NormalTok{, }\DataTypeTok{se=}\NormalTok{F) }\OperatorTok{+}\StringTok{ }
387
+ \StringTok{ }\KeywordTok{xlim}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }\FloatTok{0.1}\NormalTok{)) }\OperatorTok{+}\StringTok{ }
388
+ \StringTok{ }\KeywordTok{ylim}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{500000}\NormalTok{)) }\OperatorTok{+}\StringTok{ }
389
+ \StringTok{ }\KeywordTok{labs}\NormalTok{(}\DataTypeTok{subtitle=}\StringTok{"Area Vs Population"}\NormalTok{, }
390
+ \DataTypeTok{y=}\StringTok{"Population"}\NormalTok{, }
391
+ \DataTypeTok{x=}\StringTok{"Area"}\NormalTok{, }
392
+ \DataTypeTok{title=}\StringTok{"Scatterplot"}\NormalTok{, }
393
+ \DataTypeTok{caption =} \StringTok{"Source: midwest"}\NormalTok{)}
394
+
395
+ \KeywordTok{plot}\NormalTok{(gg)}
396
+ \end{Highlighting}
397
+ \end{Shaded}
398
+
399
+ Note that both codes are very similar. The Ruby code requires the use of
400
+ ``R.'' before calling any functions, for instance R function
401
+ `geom\_point' becomes `R.geom\_point' in Ruby. R named parameters such
402
+ as (col = state, size = popdensity), become in Ruby (col: :state, size:
403
+ :popdensity).
404
+
405
+ One last point that needs to be observed is the call to the `aes'
406
+ function. In Ruby instead of doing `R.aes', we use `E.aes'. The
407
+ explanation of why E.aes is needed is an advanced topic in R and depends
408
+ on what is know as Non-standard Evaluation (NSE) in R. In short,
409
+ function `aes' is lazily evaluated in R, i.e., in R when calling
410
+ geom\_point(aes(col=state, size=popdensity)), function geom\_point
411
+ receives as argument something similar to a string containing
412
+ `aes(col=state, size=popdensity)', and the aes function will be
413
+ evaluated inside the geom\_point function. In Ruby, there is no Lazy
414
+ evaluation and doing R.aes would try to evaluate aes immediately. In
415
+ order to delay the evaluation of function aes we need to use E.aes. The
416
+ interested reader on NSE in R is directed to
417
+ \url{http://adv-r.had.co.nz/Computing-on-the-language.html}.
418
+
419
+ \section{An extension to the example}\label{an-extension-to-the-example}
420
+
421
+ If both codes are so similar, then why would one use Ruby instead of R
422
+ and what good is galaaz after all?
423
+
424
+ Ruby is a modern OO language with numerous very useful constructs such
425
+ as classes, modules, blocks, procs, etc. The example above focus on the
426
+ coupling of both languages, and does not show the use of other Ruby
427
+ constructs. In the following example, we will show a more complex
428
+ example using other Ruby constructs. This is certainly not a very well
429
+ written and robust Ruby code, but it give the idea of how Ruby and R are
430
+ strongly coupled.
431
+
432
+ Let's imagine that we work in a corporation that has its plot themes.
433
+ So, it has defined a `CorpTheme' module. Plots in this corporation
434
+ should not have grids, numbers in labels should not use scientific
435
+ notation and the preferred color is blue.
436
+
437
+ \begin{Shaded}
438
+ \begin{Highlighting}[]
439
+ \CommentTok{# corp_theme.rb}
440
+ \CommentTok{# defines the corporate theme for all plots}
441
+
442
+ \KeywordTok{module} \DataTypeTok{CorpTheme}
443
+
444
+ \CommentTok{#---------------------------------------------------------------------------------}
445
+ \CommentTok{# Defines the plot theme (visualization). In this theme we remove major and minor}
446
+ \CommentTok{# grids, borders and background. We also turn-off scientific notation.}
447
+ \CommentTok{#---------------------------------------------------------------------------------}
448
+
449
+ \KeywordTok{def} \DecValTok{self}\NormalTok{.global_theme}
450
+
451
+ \NormalTok{ R.options(}\StringTok{scipen: }\DecValTok{999}\NormalTok{) }\CommentTok{# turn-off scientific notation like 1e+48}
452
+
453
+ \CommentTok{# remove major grids}
454
+ \NormalTok{ global_theme = R.theme(}\StringTok{panel__grid__major: }\NormalTok{E.element_blank())}
455
+ \CommentTok{# remove minor grids}
456
+ \NormalTok{ global_theme = global_theme + R.theme(}\StringTok{panel__grid__minor: }\NormalTok{E.element_blank)}
457
+ \CommentTok{# remove border}
458
+ \NormalTok{ global_theme = global_theme + R.theme(}\StringTok{panel__border: }\NormalTok{E.element_blank)}
459
+ \CommentTok{# remove background}
460
+ \NormalTok{ global_theme = global_theme + R.theme(}\StringTok{panel__background: }\NormalTok{E.element_blank)}
461
+ \CommentTok{# Change axis font}
462
+ \NormalTok{ global_theme = global_theme +}
463
+ \NormalTok{ R.theme(}\StringTok{axis__text: }\NormalTok{E.element_text(}\StringTok{size: }\DecValTok{8}\NormalTok{, }\StringTok{color: "#000080"}\NormalTok{))}
464
+ \CommentTok{# change color of axis titles}
465
+ \NormalTok{ global_theme = global_theme +}
466
+ \NormalTok{ R.theme(}\StringTok{axis__title: }\NormalTok{E.element_text(}
467
+ \StringTok{color: "#000080"}\NormalTok{, }
468
+ \StringTok{face: "bold"}\NormalTok{,}
469
+ \StringTok{size: }\DecValTok{8}\NormalTok{,}
470
+ \StringTok{hjust: }\DecValTok{1}\NormalTok{))}
471
+ \KeywordTok{end}
472
+
473
+ \KeywordTok{end}
474
+ \end{Highlighting}
475
+ \end{Shaded}
476
+
477
+ We now define a ScatterPlot class:
478
+
479
+ \begin{Shaded}
480
+ \begin{Highlighting}[]
481
+ \CommentTok{# ScatterPlot.rb}
482
+ \CommentTok{# creates a scatter plot and allow some configuration}
483
+
484
+ \KeywordTok{class} \DataTypeTok{ScatterPlot}
485
+
486
+ \OtherTok{attr_accessor} \StringTok{:title}
487
+ \OtherTok{attr_accessor} \StringTok{:subtitle}
488
+ \OtherTok{attr_accessor} \StringTok{:caption}
489
+ \OtherTok{attr_accessor} \StringTok{:x_label}
490
+ \OtherTok{attr_accessor} \StringTok{:y_label}
491
+
492
+ \CommentTok{#---------------------------------------------------------------------------------}
493
+ \CommentTok{# Initialize the plot with the data and the x and y variables}
494
+ \CommentTok{#---------------------------------------------------------------------------------}
495
+
496
+ \KeywordTok{def}\NormalTok{ initialize(data, x:, y:)}
497
+ \OtherTok{@data}\NormalTok{ = data}
498
+ \OtherTok{@x}\NormalTok{ = x}
499
+ \OtherTok{@y}\NormalTok{ = y}
500
+ \KeywordTok{end}
501
+
502
+ \CommentTok{#---------------------------------------------------------------------------------}
503
+ \CommentTok{# Define groupings by color and size}
504
+ \CommentTok{#---------------------------------------------------------------------------------}
505
+
506
+ \KeywordTok{def}\NormalTok{ group_by(color:, size:)}
507
+ \OtherTok{@color_by}\NormalTok{ = color}
508
+ \OtherTok{@size_by}\NormalTok{ = size}
509
+ \KeywordTok{end}
510
+
511
+ \CommentTok{#---------------------------------------------------------------------------------}
512
+ \CommentTok{# Add a smoothing line, and if confidence is true the add a confidence interval, if}
513
+ \CommentTok{# false does not add the confidence interval}
514
+ \CommentTok{#---------------------------------------------------------------------------------}
515
+
516
+ \KeywordTok{def}\NormalTok{ add_smoothing_line(method:, }\StringTok{confidence: }\DecValTok{true}\NormalTok{)}
517
+ \OtherTok{@method}\NormalTok{ = method}
518
+ \OtherTok{@confidence}\NormalTok{ = confidence}
519
+ \KeywordTok{end}
520
+
521
+ \CommentTok{#---------------------------------------------------------------------------------}
522
+ \CommentTok{# Creates the graph title, properly formated for this theme}
523
+ \CommentTok{# @param title [String] The title to add to the graph}
524
+ \CommentTok{# @return textGrob that can be included in a graph}
525
+ \CommentTok{#---------------------------------------------------------------------------------}
526
+
527
+ \KeywordTok{def}\NormalTok{ graph_params(}\StringTok{title: ""}\NormalTok{, }\StringTok{subtitle: ""}\NormalTok{, }\StringTok{caption: ""}\NormalTok{, }\StringTok{x_label: ""}\NormalTok{, }\StringTok{y_label: ""}\NormalTok{)}
528
+ \NormalTok{ R.labs(}
529
+ \StringTok{title: }\NormalTok{title, }
530
+ \StringTok{subtitle: }\NormalTok{subtitle, }
531
+ \StringTok{caption: }\NormalTok{caption,}
532
+ \StringTok{y_label: }\NormalTok{y_label, }
533
+ \StringTok{x_label: }\NormalTok{x_label, }
534
+ \NormalTok{ )}
535
+ \KeywordTok{end}
536
+
537
+ \CommentTok{#---------------------------------------------------------------------------------}
538
+ \CommentTok{# Prepare the plot's points}
539
+ \CommentTok{#---------------------------------------------------------------------------------}
540
+
541
+ \KeywordTok{def}\NormalTok{ points}
542
+ \NormalTok{ params = \{\}}
543
+ \NormalTok{ params[}\StringTok{:col}\NormalTok{] = }\OtherTok{@color_by} \KeywordTok{if} \OtherTok{@color_by}
544
+ \NormalTok{ params[}\StringTok{:size}\NormalTok{] = }\OtherTok{@size_by} \KeywordTok{if} \OtherTok{@size_by}
545
+ \NormalTok{ R.geom_point(E.aes(params))}
546
+ \KeywordTok{end}
547
+
548
+ \CommentTok{#---------------------------------------------------------------------------------}
549
+ \CommentTok{# Plots the scatterplot}
550
+ \CommentTok{#---------------------------------------------------------------------------------}
551
+
552
+ \KeywordTok{def}\NormalTok{ plot(device = }\StringTok{'awt'}\NormalTok{)}
553
+ \NormalTok{ device == }\StringTok{'awt'}\NormalTok{ ? R.awt : R.svg}
554
+
555
+ \NormalTok{ gg = }\OtherTok{@data}\NormalTok{.ggplot(E.aes(}\StringTok{x: }\OtherTok{@x}\NormalTok{, }\StringTok{y: }\OtherTok{@y}\NormalTok{)) +}
556
+ \NormalTok{ points + }
557
+ \NormalTok{ R.geom_smooth(}\StringTok{method: }\OtherTok{@method}\NormalTok{, }\StringTok{se: }\OtherTok{@confidence}\NormalTok{) +}
558
+ \NormalTok{ R.xlim(R.c(}\DecValTok{0}\NormalTok{, }\FloatTok{0.1}\NormalTok{)) +}
559
+ \NormalTok{ R.ylim(R.c(}\DecValTok{0}\NormalTok{, }\DecValTok{500000}\NormalTok{)) + }
560
+ \NormalTok{ graph_params(}\StringTok{title: }\OtherTok{@title}\NormalTok{,}
561
+ \StringTok{subtitle: }\OtherTok{@subtitle}\NormalTok{, }
562
+ \StringTok{y_label: }\OtherTok{@y_label}\NormalTok{, }
563
+ \StringTok{x_label: }\OtherTok{@x_label}\NormalTok{, }
564
+ \StringTok{caption: }\OtherTok{@caption}\NormalTok{) +}
565
+ \DataTypeTok{CorpTheme}\NormalTok{.global_theme}
566
+
567
+ \NormalTok{ R.png(}\StringTok{'scatter_plot.png'}\NormalTok{) }\KeywordTok{if}\NormalTok{ !(device == }\StringTok{'awt'}\NormalTok{)}
568
+ \NormalTok{ puts gg}
569
+ \NormalTok{ R.dev__off}
570
+
571
+ \KeywordTok{end}
572
+
573
+ \KeywordTok{end}
574
+ \end{Highlighting}
575
+ \end{Shaded}
576
+
577
+ And this is the final code for making the scatter plot with the midwest
578
+ data
579
+
580
+ \begin{Shaded}
581
+ \begin{Highlighting}[]
582
+ \NormalTok{require }\StringTok{'galaaz'}
583
+ \NormalTok{require }\StringTok{'ggplot'}
584
+
585
+ \NormalTok{sp = }\DataTypeTok{ScatterPlot}\NormalTok{.new(~}\StringTok{:midwest}\NormalTok{, }\StringTok{x: :area}\NormalTok{, }\StringTok{y: :poptotal}\NormalTok{)}
586
+ \NormalTok{sp.title = }\StringTok{"Midwest Dataset - Scatterplot"}
587
+ \NormalTok{sp.subtitle = }\StringTok{"Area Vs Population"}
588
+ \NormalTok{sp.caption = }\StringTok{"Source: midwest"}
589
+ \NormalTok{sp.x_label = }\StringTok{"Area"}
590
+ \NormalTok{sp.y_label = }\StringTok{"Population"}
591
+ \NormalTok{sp.group_by(}\StringTok{color: :state}\NormalTok{, }\StringTok{size: :popdensity}\NormalTok{) }\CommentTok{# try sp.group_by(color: :state)}
592
+ \CommentTok{# available methods: "lm", "glm", "loess", "gam"}
593
+ \NormalTok{sp.add_smoothing_line(}\StringTok{method: "glm"}\NormalTok{) }
594
+ \NormalTok{sp.plot(}\StringTok{'svg'}\NormalTok{)}
595
+
596
+ \CommentTok{# require input from the user so that the script does not end removing the plot from}
597
+ \CommentTok{# the screen}
598
+ \end{Highlighting}
599
+ \end{Shaded}
600
+
601
+ \begin{figure}
602
+ \centering
603
+ \includegraphics[width=0.70000\textwidth]{scatter_plot.png}
604
+ \caption{Midwest Plot with `glm' function and modified theme}
605
+ \end{figure}
606
+
607
+ \section{Conclusion}\label{conclusion}
608
+
609
+ R is a very powerful language for statistical analysis, data analytics,
610
+ machine learning, plotting and many other scientific applications with a
611
+ very large package ecosystem. However R is often considered hard to
612
+ learn and lacking modern computer languages constructs such as object
613
+ oriented classes, modules, lambdas, etc. For this reason, many
614
+ developers have started or switched from R to Python.
615
+
616
+ With Galaaz, R programmers can almost transparently migrate from R to
617
+ Ruby, since syntax is almost identical and they have fastR as the R
618
+ engine. FastR, by most benchmarks, can be orders of magnitude faster
619
+ than Gnu R. Further, by using Galaaz the R developer can start (slowly
620
+ if needed) using all of Ruby's constructs and libraries that nicely
621
+ complement R packages.
622
+
623
+ For the Ruby developer, Galaaz allows the immediate use of R functions
624
+ completely transparently. As shown in the second example above, class
625
+ ScatterPlot completely hides all the details an R calls from the Ruby
626
+ developer, furthermore Galaaz is powered by TruffleRuby that can also be
627
+ orders of magnitude faster than MRI Ruby.
628
+
629
+
630
+ \end{document}