galaaz 2.1.7 → 2.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +39 -0
  3. data/Rakefile +20 -2
  4. data/bin/check_gemfile_lock_version +46 -0
  5. data/bin/release_bump +26 -0
  6. data/blogs/README.md +4 -0
  7. data/blogs/galaaz_2_0/galaaz_2_0.Rmd +385 -0
  8. data/blogs/galaaz_2_0/galaaz_2_0.md +409 -0
  9. data/blogs/galaaz_2_0/galaaz_2_0.tex +756 -0
  10. data/blogs/galaaz_2_0/images/galaaz-header.png +0 -0
  11. data/blogs/galaaz_2_0/images/galaaz-lockup-stacked.png +0 -0
  12. data/blogs/galaaz_ggplot/galaaz_ggplot.Rmd +76 -58
  13. data/blogs/galaaz_ggplot/galaaz_ggplot.md +188 -161
  14. data/blogs/galaaz_ggplot/galaaz_ggplot.tex +185 -117
  15. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/midwest_rb.png +0 -0
  16. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/scatter_plot_rb.png +0 -0
  17. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-markdown_github/midwest_rb.png +0 -0
  18. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-markdown_github/scatter_plot_rb.png +0 -0
  19. data/blogs/galaaz_ggplot/images/galaaz-lockup-stacked.png +0 -0
  20. data/blogs/gknit/gknit.Rmd +48 -28
  21. data/blogs/gknit/gknit.md +60 -43
  22. data/blogs/gknit/gknit.tex +1409 -0
  23. data/blogs/gknit/gknit_files/figure-html/bubble-1.png +0 -0
  24. data/blogs/gknit/gknit_files/figure-html/diverging_bar.png +0 -0
  25. data/blogs/gknit/gknit_files/figure-latex/bubble-1.png +0 -0
  26. data/blogs/gknit/images/galaaz-lockup-stacked.png +0 -0
  27. data/blogs/manual/images/galaaz-lockup-stacked.png +0 -0
  28. data/blogs/manual/manual.Rmd +161 -71
  29. data/blogs/manual/manual.md +573 -482
  30. data/blogs/manual/manual.tex +1113 -517
  31. data/blogs/manual/manual_files/figure-html/bubble-1.png +0 -0
  32. data/blogs/manual/manual_files/figure-latex/bubble-1.png +0 -0
  33. data/blogs/manual/manual_files/figure-markdown_github/bubble-1.png +0 -0
  34. data/blogs/manual/manual_files/figure-markdown_github/diverging_bar.png +0 -0
  35. data/blogs/nse_dplyr/images/galaaz-lockup-stacked.png +0 -0
  36. data/blogs/nse_dplyr/nse_dplyr.Rmd +42 -8
  37. data/blogs/nse_dplyr/nse_dplyr.md +69 -50
  38. data/blogs/nse_dplyr/nse_dplyr.tex +1626 -0
  39. data/blogs/oh_my/images/galaaz-lockup-stacked.png +0 -0
  40. data/blogs/oh_my/oh_my.Rmd +206 -55
  41. data/blogs/oh_my/oh_my.md +247 -96
  42. data/blogs/oh_my/oh_my.tex +2038 -93
  43. data/blogs/r_on_rails_ledger/images/00_portfolio_page.png +0 -0
  44. data/blogs/r_on_rails_ledger/images/01_results_panel.png +0 -0
  45. data/blogs/r_on_rails_ledger/images/02_density_tail_risk.png +0 -0
  46. data/blogs/r_on_rails_ledger/images/03_mc_cone.png +0 -0
  47. data/blogs/r_on_rails_ledger/images/04_rolling_var.png +0 -0
  48. data/blogs/r_on_rails_ledger/images/galaaz-lockup-stacked.png +0 -0
  49. data/blogs/r_on_rails_ledger/r_on_rails_ledger.Rmd +354 -0
  50. data/blogs/r_on_rails_ledger/r_on_rails_ledger.md +365 -0
  51. data/blogs/r_on_rails_ledger/r_on_rails_ledger.tex +670 -0
  52. data/blogs/ruby_plot/images/galaaz-lockup-stacked.png +0 -0
  53. data/blogs/ruby_plot/ruby_plot.Rmd +56 -35
  54. data/blogs/ruby_plot/ruby_plot.md +63 -47
  55. data/blogs/ruby_plot/ruby_plot.tex +194 -125
  56. data/blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.png +0 -0
  57. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_delivery.png +0 -0
  58. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_dose.png +0 -0
  59. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color.png +0 -0
  60. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color2.png +0 -0
  61. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_decorations.png +0 -0
  62. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_jitter.png +0 -0
  63. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_points.png +0 -0
  64. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_box_plot.png +0 -0
  65. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_violin_plot.png +0 -0
  66. data/blogs/ruby_plot/ruby_plot_files/figure-html/violin_with_jitter.png +0 -0
  67. data/blogs/ruby_plot/ruby_plot_files/figure-latex/dose_len.png +0 -0
  68. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_delivery.png +0 -0
  69. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_dose.png +0 -0
  70. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color.png +0 -0
  71. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color2.png +0 -0
  72. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_decorations.png +0 -0
  73. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_jitter.png +0 -0
  74. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_points.png +0 -0
  75. data/blogs/ruby_plot/ruby_plot_files/figure-latex/final_box_plot.png +0 -0
  76. data/blogs/ruby_plot/ruby_plot_files/figure-latex/final_violin_plot.png +0 -0
  77. data/blogs/ruby_plot/ruby_plot_files/figure-latex/violin_with_jitter.png +0 -0
  78. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/dose_len.png +0 -0
  79. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facet_by_delivery.png +0 -0
  80. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facet_by_dose.png +0 -0
  81. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_by_delivery_color.png +0 -0
  82. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_by_delivery_color2.png +0 -0
  83. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_with_decorations.png +0 -0
  84. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_with_jitter.png +0 -0
  85. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_with_points.png +0 -0
  86. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/final_box_plot.png +0 -0
  87. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/final_violin_plot.png +0 -0
  88. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/violin_with_jitter.png +0 -0
  89. data/lib/galaaz/cli.rb +98 -13
  90. data/logos/icon-font/README.md +27 -0
  91. data/logos/icon-font/build_font.py +130 -0
  92. data/logos/icon-font/galaaz-mark.svg +34 -0
  93. data/script/omarchy/README.md +9 -2
  94. data/script/omarchy/fonts/galaaz.ttf +0 -0
  95. data/script/omarchy/galaaz-guide.sh +1 -1
  96. data/script/omarchy/install-galaaz.sh +8 -1
  97. data/script/omarchy/omarchy-menu.jsonc +21 -9
  98. data/sty/galaaz-header.png +0 -0
  99. data/sty/galaaz-headers-from-p3.tex +4 -0
  100. data/sty/galaaz.sty +76 -23
  101. data/version.rb +1 -1
  102. metadata +46 -7
  103. data/blogs/galaaz_ggplot/galaaz_ggplot.log +0 -754
  104. data/blogs/manual/manual.log +0 -1786
  105. data/blogs/nse_dplyr/nse_dplyr.log +0 -928
  106. data/blogs/oh_my/oh_my.log +0 -804
  107. data/blogs/ruby_plot/ruby_plot.log +0 -885
@@ -0,0 +1,1409 @@
1
+ % Options for packages loaded elsewhere
2
+ \PassOptionsToPackage{unicode}{hyperref}
3
+ \PassOptionsToPackage{hyphens}{url}
4
+ \documentclass[
5
+ ]{article}
6
+ \usepackage{xcolor}
7
+ \usepackage[margin=1in]{geometry}
8
+ \usepackage{amsmath,amssymb}
9
+ \setcounter{secnumdepth}{5}
10
+ \usepackage{iftex}
11
+ \ifPDFTeX
12
+ \usepackage[T1]{fontenc}
13
+ \usepackage[utf8]{inputenc}
14
+ \usepackage{textcomp} % provide euro and other symbols
15
+ \else % if luatex or xetex
16
+ \usepackage{unicode-math} % this also loads fontspec
17
+ \defaultfontfeatures{Scale=MatchLowercase}
18
+ \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
19
+ \fi
20
+ \usepackage{lmodern}
21
+ \ifPDFTeX\else
22
+ % xetex/luatex font selection
23
+ \fi
24
+ % Use upquote if available, for straight quotes in verbatim environments
25
+ \IfFileExists{upquote.sty}{\usepackage{upquote}}{}
26
+ \IfFileExists{microtype.sty}{% use microtype if available
27
+ \usepackage[]{microtype}
28
+ \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
29
+ }{}
30
+ \makeatletter
31
+ \@ifundefined{KOMAClassName}{% if non-KOMA class
32
+ \IfFileExists{parskip.sty}{%
33
+ \usepackage{parskip}
34
+ }{% else
35
+ \setlength{\parindent}{0pt}
36
+ \setlength{\parskip}{6pt plus 2pt minus 1pt}}
37
+ }{% if KOMA class
38
+ \KOMAoptions{parskip=half}}
39
+ \makeatother
40
+ \usepackage{color}
41
+ \usepackage{fancyvrb}
42
+ \newcommand{\VerbBar}{|}
43
+ \newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
44
+ \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
45
+ % Add ',fontsize=\small' for more characters per line
46
+ \usepackage{framed}
47
+ \definecolor{shadecolor}{RGB}{248,248,248}
48
+ \newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
49
+ \newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
50
+ \newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
51
+ \newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
52
+ \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
53
+ \newcommand{\BuiltInTok}[1]{#1}
54
+ \newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
55
+ \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
56
+ \newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
57
+ \newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
58
+ \newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
59
+ \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
60
+ \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
61
+ \newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
62
+ \newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
63
+ \newcommand{\ExtensionTok}[1]{#1}
64
+ \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
65
+ \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
66
+ \newcommand{\ImportTok}[1]{#1}
67
+ \newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
68
+ \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
69
+ \newcommand{\NormalTok}[1]{#1}
70
+ \newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
71
+ \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
72
+ \newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
73
+ \newcommand{\RegionMarkerTok}[1]{#1}
74
+ \newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
75
+ \newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
76
+ \newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
77
+ \newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
78
+ \newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
79
+ \newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
80
+ \usepackage{graphicx}
81
+ \makeatletter
82
+ \newsavebox\pandoc@box
83
+ \newcommand*\pandocbounded[1]{% scales image to fit in text height/width
84
+ \sbox\pandoc@box{#1}%
85
+ \Gscale@div\@tempa{\textheight}{\dimexpr\ht\pandoc@box+\dp\pandoc@box\relax}%
86
+ \Gscale@div\@tempb{\linewidth}{\wd\pandoc@box}%
87
+ \ifdim\@tempb\p@<\@tempa\p@\let\@tempa\@tempb\fi% select the smaller of both
88
+ \ifdim\@tempa\p@<\p@\scalebox{\@tempa}{\usebox\pandoc@box}%
89
+ \else\usebox{\pandoc@box}%
90
+ \fi%
91
+ }
92
+ % Set default figure placement to htbp
93
+ \def\fps@figure{htbp}
94
+ \makeatother
95
+ % definitions for citeproc citations
96
+ \NewDocumentCommand\citeproctext{}{}
97
+ \NewDocumentCommand\citeproc{mm}{%
98
+ \begingroup\def\citeproctext{#2}\cite{#1}\endgroup}
99
+ \makeatletter
100
+ % allow citations to break across lines
101
+ \let\@cite@ofmt\@firstofone
102
+ % avoid brackets around text for \cite:
103
+ \def\@biblabel#1{}
104
+ \def\@cite#1#2{{#1\if@tempswa , #2\fi}}
105
+ \makeatother
106
+ \newlength{\cslhangindent}
107
+ \setlength{\cslhangindent}{1.5em}
108
+ \newlength{\csllabelwidth}
109
+ \setlength{\csllabelwidth}{3em}
110
+ \newenvironment{CSLReferences}[2] % #1 hanging-indent, #2 entry-spacing
111
+ {\begin{list}{}{%
112
+ \setlength{\itemindent}{0pt}
113
+ \setlength{\leftmargin}{0pt}
114
+ \setlength{\parsep}{0pt}
115
+ % turn on hanging indent if param 1 is 1
116
+ \ifodd #1
117
+ \setlength{\leftmargin}{\cslhangindent}
118
+ \setlength{\itemindent}{-1\cslhangindent}
119
+ \fi
120
+ % set entry spacing
121
+ \setlength{\itemsep}{#2\baselineskip}}}
122
+ {\end{list}}
123
+ \usepackage{calc}
124
+ \newcommand{\CSLBlock}[1]{\hfill\break\parbox[t]{\linewidth}{\strut\ignorespaces#1\strut}}
125
+ \newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{\strut#1\strut}}
126
+ \newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{\strut#1\strut}}
127
+ \newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1}
128
+ \setlength{\emergencystretch}{3em} % prevent overfull lines
129
+ \providecommand{\tightlist}{%
130
+ \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
131
+ % usar portugues do Brasil
132
+ % \usepackage[brazilian]{babel}
133
+ \usepackage[utf8]{inputenc}
134
+
135
+ \usepackage{geometry}
136
+ \geometry{a4paper, top=1.05in, headheight=30pt, headsep=14pt}
137
+
138
+ % needed for kableExtra
139
+ \usepackage{longtable}
140
+ \usepackage{multirow}
141
+ \usepackage[table]{xcolor}
142
+ \usepackage{wrapfig}
143
+ \usepackage{float}
144
+ \usepackage{colortbl}
145
+ \usepackage{pdflscape}
146
+ \usepackage{tabu}
147
+ \usepackage{threeparttable}
148
+ \usepackage[normalem]{ulem}
149
+
150
+ \usepackage{bbm}
151
+ \usepackage{booktabs}
152
+ \usepackage{expex}
153
+
154
+ \usepackage{graphicx}
155
+
156
+ \usepackage{fancyhdr}
157
+ % Header: small Galaaz mark (left); page number + section title (right),
158
+ % matching classic LaTeX style: "4 AN EXTENSION TO THE EXAMPLE"
159
+ % (upright page number, uppercase italic section name).
160
+ % Logo paths cover repo blogs (../../sty/), installed ~/sty/, and local images/.
161
+ % Blogs may delay headers with \renewcommand{\galaazheaderfrompage}{3} after this sty
162
+ % (e.g. skip title/TOC and a logo splash page).
163
+ \newcommand{\galaazheaderfrompage}{1}
164
+ \newcommand{\galaazheaderlogo}{%
165
+ \raisebox{-0.12\height}{%
166
+ \IfFileExists{../../sty/galaaz-header.png}{%
167
+ \includegraphics[height=0.32in]{../../sty/galaaz-header.png}%
168
+ }{%
169
+ \IfFileExists{galaaz-header.png}{%
170
+ \includegraphics[height=0.32in]{galaaz-header.png}%
171
+ }{%
172
+ \IfFileExists{images/galaaz-header.png}{%
173
+ \includegraphics[height=0.32in]{images/galaaz-header.png}%
174
+ }{}%
175
+ }%
176
+ }%
177
+ }%
178
+ }
179
+ % Section title only in marks (no \thesection); page number is separate.
180
+ \renewcommand{\sectionmark}[1]{\markboth{#1}{}}
181
+ \renewcommand{\subsectionmark}[1]{}% keep the current section in the header
182
+ \newcommand{\galaazmaybeheaderlogo}{%
183
+ \ifnum\value{page}<\galaazheaderfrompage\relax\else\galaazheaderlogo\fi}
184
+ \newcommand{\galaazmayberighthead}{%
185
+ \ifnum\value{page}<\galaazheaderfrompage\relax
186
+ \else
187
+ \textup{\thepage}\hspace{1em}\textit{\MakeUppercase{\leftmark}}%
188
+ \fi
189
+ }
190
+ \pagestyle{fancy}
191
+ \fancyhf{}
192
+ \fancyhead[L]{\galaazmaybeheaderlogo}
193
+ \fancyhead[R]{\galaazmayberighthead}
194
+ \renewcommand{\headrulewidth}{0.3pt}
195
+ \makeatletter
196
+ \renewcommand{\headrule}{%
197
+ \ifnum\value{page}<\galaazheaderfrompage\relax
198
+ \else
199
+ \hrule\@height\headrulewidth\@width\headwidth
200
+ \vskip-\headrulewidth
201
+ \fi
202
+ }
203
+ \makeatother
204
+ % First page often uses plain; same conditional header logic.
205
+ \fancypagestyle{plain}{%
206
+ \fancyhf{}%
207
+ \fancyhead[L]{\galaazmaybeheaderlogo}%
208
+ \fancyhead[R]{\galaazmayberighthead}%
209
+ \renewcommand{\headrulewidth}{0.3pt}%
210
+ }
211
+
212
+ % make available command lastpage
213
+ \usepackage{lastpage}
214
+
215
+ % default fontsize 11pt better to add
216
+ % fontsize on the yaml header
217
+ % \usepackage[fontsize=11pt]{scrextend}
218
+
219
+ % comandos para formatar uma tabela
220
+ \usepackage{array}
221
+ \newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
222
+ \newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
223
+ \newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
224
+
225
+ % necessário if we need to import other latex documents
226
+ \usepackage{import}
227
+
228
+ % Command to import an R variable to latex
229
+ \newcommand{\RtoLatex}[2]{\newcommand{#1}{#2}}
230
+
231
+ % Soft-wrap Pandoc highlighted code/output boxes (Shaded + Highlighting).
232
+ % This runs after Pandoc's default \DefineVerbatimEnvironment{Highlighting}
233
+ % (header includes come later in the generated .tex). Prefer manual line
234
+ % breaks in Rmd sources; breaklines is a safety net for leftovers/output.
235
+ % Requires TinyTeX/TeX Live package: tlmgr install fvextra
236
+ \IfFileExists{fvextra.sty}{%
237
+ \usepackage{fvextra}%
238
+ \DefineVerbatimEnvironment{Highlighting}{Verbatim}{%
239
+ breaklines=true,
240
+ breakanywhere=true,
241
+ breakindent=1.5em,
242
+ fontsize=\small,
243
+ commandchars=\\\{\}%
244
+ }%
245
+ }{%
246
+ % fancyvrb is already loaded by Pandoc; shrink code so more fits per line.
247
+ \DefineVerbatimEnvironment{Highlighting}{Verbatim}{%
248
+ fontsize=\small,
249
+ commandchars=\\\{\}%
250
+ }%
251
+ }
252
+
253
+ %
254
+ %\newcommand{\atraso}[1]{\color{red} \textbf {Tempo desde a Assinatura do Contrato: #1 dias}}
255
+ % No header on title/TOC (p.1) or splash logo page (p.2);
256
+ % logo + page number + section title from page 3 onward.
257
+ % Load after galaaz.sty.
258
+ \renewcommand{\galaazheaderfrompage}{3}
259
+ \usepackage{booktabs}
260
+ \usepackage{longtable}
261
+ \usepackage{array}
262
+ \usepackage{multirow}
263
+ \usepackage{wrapfig}
264
+ \usepackage{float}
265
+ \usepackage{colortbl}
266
+ \usepackage{pdflscape}
267
+ \usepackage{tabu}
268
+ \usepackage{threeparttable}
269
+ \usepackage{threeparttablex}
270
+ \usepackage[normalem]{ulem}
271
+ \usepackage{makecell}
272
+ \usepackage{xcolor}
273
+ \usepackage{bookmark}
274
+ \IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
275
+ \urlstyle{same}
276
+ \hypersetup{
277
+ pdftitle={How to do reproducible research in Ruby with gKnit},
278
+ pdfauthor={Rodrigo Botafogo; Daniel Mossé - University of Pittsburgh},
279
+ hidelinks,
280
+ pdfcreator={LaTeX via pandoc}}
281
+
282
+ \title{How to do reproducible research in Ruby with gKnit}
283
+ \author{Rodrigo Botafogo \and Daniel Mossé - University of Pittsburgh}
284
+ \date{29/04/2019 (narrative updated for Galaaz 2.0, 2026)}
285
+
286
+ \begin{document}
287
+ \maketitle
288
+
289
+ {
290
+ \setcounter{tocdepth}{2}
291
+ \tableofcontents
292
+ }
293
+ \begin{center}\includegraphics[width=0.45\linewidth]{images/galaaz-lockup-stacked} \end{center}
294
+
295
+ \section{Introduction}\label{introduction}
296
+
297
+ The idea of ``literate programming'' was first introduced by Donald
298
+ Knuth in the 1980's (Knuth 1984). The main intention of this approach
299
+ was to develop software interspersing macro snippets, traditional source
300
+ code, and a natural language such as English in a document that could be
301
+ compiled into executable code and at the same time easily read by a
302
+ human developer. According to Knuth ``The practitioner of literate
303
+ programming can be regarded as an essayist, whose main concern is with
304
+ exposition and excellence of style.''
305
+
306
+ The idea of literate programming evolved into the idea of reproducible
307
+ research, in which all the data, software code, documentation, graphics
308
+ etc. needed to reproduce the research and its reports could be included
309
+ in a single document or set of documents that when distributed to peers
310
+ could be rerun generating the same output and reports.
311
+
312
+ The R community has put a great deal of effort in reproducible research.
313
+ In 2002, Sweave was introduced and it allowed mixing R code with LaTeX,
314
+ generating high-quality PDF documents. A Sweave document could include
315
+ code, the results of executing the code, graphics and text such that it
316
+ contained the whole narrative to reproduce the research. In 2012, Knitr,
317
+ developed by Yihui Xie from RStudio was released to replace Sweave and
318
+ to consolidate in one single package the many extensions and add-on
319
+ packages that were necessary for Sweave.
320
+
321
+ With Knitr, \textbf{R markdown} was also developed, an extension to the
322
+ Markdown format. With \textbf{R markdown} and Knitr it is possible to
323
+ generate reports in a multitude of formats such as HTML, Markdown,
324
+ LaTeX, PDF, DVI, etc. \textbf{R markdown} also allows the use of
325
+ multiple programming languages such as R, Ruby, Python, etc. in the same
326
+ document.
327
+
328
+ In \textbf{R markdown}, text is interspersed with code chunks that can
329
+ be executed and both the code and its results can become part of the
330
+ final report. Although \textbf{R markdown} allows multiple programming
331
+ languages in the same document, only R and Python (with the reticulate
332
+ package) can persist variables between chunks. For other languages, such
333
+ as Ruby, every chunk will start a new process and thus all data is lost
334
+ between chunks, unless it is somehow stored in a data file that is read
335
+ by the next chunk.
336
+
337
+ Being able to persist data between chunks is critical for literate
338
+ programming otherwise the flow of the narrative is lost by all the
339
+ effort of having to save data and then reload it. Although this might,
340
+ at first, seem like a small nuisance, not being able to persist data
341
+ between chunks is a major issue. For example, let's take a look at the
342
+ following simple example in which we want to show how to create a list
343
+ and the use it. Let's first assume that data cannot be persisted between
344
+ chunks. In the next chunk we create a list, then we would need to save
345
+ it to file, but to save it, we need somehow to marshal the data into a
346
+ binary format:
347
+
348
+ \begin{Shaded}
349
+ \begin{Highlighting}[]
350
+ \NormalTok{lst }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.list}\NormalTok{(}\WarningTok{a:} \DecValTok{1}\NormalTok{, }\WarningTok{b:} \DecValTok{2}\NormalTok{, }\WarningTok{c:} \DecValTok{3}\NormalTok{)}
351
+ \NormalTok{lst}\AttributeTok{.saveRDS}\NormalTok{(}\StringTok{"lst.rds"}\NormalTok{)}
352
+ \end{Highlighting}
353
+ \end{Shaded}
354
+
355
+ then, on the next chunk, where variable `lst' is used, we need to read
356
+ back it's value
357
+
358
+ \begin{Shaded}
359
+ \begin{Highlighting}[]
360
+ \NormalTok{lst }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.readRDS}\NormalTok{(}\StringTok{"lst.rds"}\NormalTok{)}
361
+ \FunctionTok{puts}\NormalTok{ lst}
362
+ \end{Highlighting}
363
+ \end{Shaded}
364
+
365
+ \begin{verbatim}
366
+ ## $a
367
+ ## [1] 1
368
+ ##
369
+ ## $b
370
+ ## [1] 2
371
+ ##
372
+ ## $c
373
+ ## [1] 3
374
+ \end{verbatim}
375
+
376
+ Now, any single code has dozens of variables that we might want to use
377
+ and reuse between chunks. Clearly, such an approach becomes quickly
378
+ unmanageable. Probably, because of this problem, it is very rare to see
379
+ any \textbf{R markdown} document in the Ruby community.
380
+
381
+ When variables can be used accross chunks, then no overhead is needed:
382
+
383
+ \begin{Shaded}
384
+ \begin{Highlighting}[]
385
+ \NormalTok{lst }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.list}\NormalTok{(}\WarningTok{a:} \DecValTok{1}\NormalTok{, }\WarningTok{b:} \DecValTok{2}\NormalTok{, }\WarningTok{c:} \DecValTok{3}\NormalTok{)}
386
+ \CommentTok{\# any other code can be added here}
387
+ \end{Highlighting}
388
+ \end{Shaded}
389
+
390
+ \begin{Shaded}
391
+ \begin{Highlighting}[]
392
+ \FunctionTok{puts}\NormalTok{ lst}
393
+ \end{Highlighting}
394
+ \end{Shaded}
395
+
396
+ \begin{verbatim}
397
+ ## $a
398
+ ## [1] 1
399
+ ##
400
+ ## $b
401
+ ## [1] 2
402
+ ##
403
+ ## $c
404
+ ## [1] 3
405
+ \end{verbatim}
406
+
407
+ In the Python community, the same effort to have code and text in an
408
+ integrated environment started around the first decade of the 2000s. In
409
+ 2006 IPython 0.7.2 was released. In 2014, Fernando Pérez spun off the
410
+ Jupyter project from IPython, creating a web-based interactive
411
+ computation environment. Jupyter can now be used with many languages,
412
+ including Ruby with the iruby gem
413
+ (\url{https://github.com/SciRuby/iruby}). In order to have multiple
414
+ languages in a Jupyter notebook the SoS kernel was developed
415
+ (\url{https://vatlab.github.io/sos-docs/}).
416
+
417
+ \section{gKnitting a Document}\label{gknitting-a-document}
418
+
419
+ This document describes gKnit. gKnit is based on knitr and \textbf{R
420
+ markdown} and can knit a document written both in Ruby and/or R and
421
+ output it in any of the available formats of \textbf{R markdown}. gKnit
422
+ allows ruby developers to do literate programming and reproducible
423
+ research by allowing them to have in a single document, text and code.
424
+
425
+ gKnit runs with \textbf{JRuby or CRuby}, \textbf{GNU R}, and
426
+ \textbf{Galaaz} (the integration layer between Ruby and R---see below).
427
+ Knitr and \textbf{R Markdown} orchestrate the document; Galaaz's engine
428
+ keeps \textbf{Ruby state across chunks} and talks to R through the
429
+ \textbf{bridge}. Ruby chunks can read and update R variables
430
+ (\texttt{\textasciitilde{}R{[}:name{]}}, \texttt{R.*}) without
431
+ GraalVM-style polyglot interop.
432
+
433
+ Galaaz has already been describe in the following posts:
434
+
435
+ \begin{itemize}
436
+ \tightlist
437
+ \item
438
+ \url{https://towardsdatascience.com/ruby-plotting-with-galaaz-an-example-of-tightly-coupling-ruby-and-r-in-graalvm-520b69e21021}
439
+ (older GraalVM-era article; plotting ideas still apply).
440
+ \item
441
+ \url{https://medium.freecodecamp.org/how-to-make-beautiful-ruby-plots-with-galaaz-320848058857}
442
+ \end{itemize}
443
+
444
+ This is not a blog post on \textbf{R markdown}, and the interested user
445
+ is directed to the following links for detailed information on its
446
+ capabilities and use.
447
+
448
+ \begin{itemize}
449
+ \tightlist
450
+ \item
451
+ \url{https://rmarkdown.rstudio.com/} or
452
+ \item
453
+ \url{https://bookdown.org/yihui/rmarkdown/}
454
+ \end{itemize}
455
+
456
+ In this post, we will describe just the main aspects of \textbf{R
457
+ markdown}, so the user can start gKnitting Ruby and R documents quickly.
458
+
459
+ \subsection{The Yaml header}\label{the-yaml-header}
460
+
461
+ An \textbf{R markdown} document should start with a Yaml header and be
462
+ stored in a file with `.Rmd' extension. This document has the following
463
+ header for gKnitting an HTML document.
464
+
465
+ \begin{verbatim}
466
+ ---
467
+ title: "How to do reproducible research in Ruby with gKnit"
468
+ author:
469
+ - "Rodrigo Botafogo"
470
+ - "Daniel Mossé - University of Pittsburgh"
471
+ tags: [Tech, Data Science, Ruby, R, JRuby, CRuby, "GNU R", Galaaz]
472
+ date: "20/02/2019"
473
+ output:
474
+ html_document:
475
+ self_contained: true
476
+ keep_md: true
477
+ pdf_document:
478
+ includes:
479
+ in_header: ["../../sty/galaaz.sty"]
480
+ number_sections: yes
481
+ ---
482
+ \end{verbatim}
483
+
484
+ For more information on the options in the Yaml header, check
485
+ \url{https://bookdown.org/yihui/rmarkdown/html-document.html}.
486
+
487
+ \subsection{\texorpdfstring{\textbf{R Markdown}
488
+ formatting}{R Markdown formatting}}\label{r-markdown-formatting}
489
+
490
+ Document formatting can be done with simple markups such as:
491
+
492
+ \subsubsection{Headers}\label{headers}
493
+
494
+ \begin{verbatim}
495
+ # Header 1
496
+
497
+ ## Header 2
498
+
499
+ ### Header 3
500
+ \end{verbatim}
501
+
502
+ \subsubsection{Lists}\label{lists}
503
+
504
+ \begin{verbatim}
505
+ Unordered lists:
506
+
507
+ * Item 1
508
+ * Item 2
509
+ + Item 2a
510
+ + Item 2b
511
+ \end{verbatim}
512
+
513
+ \begin{verbatim}
514
+ Ordered Lists
515
+
516
+ 1. Item 1
517
+ 2. Item 2
518
+ 3. Item 3
519
+ + Item 3a
520
+ + Item 3b
521
+ \end{verbatim}
522
+
523
+ For more R markdown formatting go to
524
+ \url{https://rmarkdown.rstudio.com/authoring_basics.html}.
525
+
526
+ \subsubsection{R chunks}\label{r-chunks}
527
+
528
+ Running and executing Ruby and R code is actually what really interests
529
+ us is this blog.\\
530
+ Inserting a code chunk is done by adding code in a block delimited by
531
+ three back ticks followed by an open curly brace (`\{') followed with
532
+ the engine name (r, ruby, rb, include, \ldots), an any optional
533
+ chunk\_label and options, as shown below:
534
+
535
+ \begin{verbatim}
536
+ ```{engine_name [chunk_label], [chunk_options]}
537
+ ```
538
+ \end{verbatim}
539
+
540
+ for instance, let's add an R chunk to the document labeled
541
+ `first\_r\_chunk'. This is a very simple code just to create a variable
542
+ and print it out, as follows:
543
+
544
+ \begin{verbatim}
545
+ ```{r first_r_chunk}
546
+ vec <- c(1, 2, 3)
547
+ print(vec)
548
+ ```
549
+ \end{verbatim}
550
+
551
+ If this block is added to an \textbf{R markdown} document and gKnitted
552
+ the result will be:
553
+
554
+ \begin{Shaded}
555
+ \begin{Highlighting}[]
556
+ \NormalTok{vec }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{)}
557
+ \FunctionTok{print}\NormalTok{(vec)}
558
+ \end{Highlighting}
559
+ \end{Shaded}
560
+
561
+ \begin{verbatim}
562
+ ## [1] 1 2 3
563
+ \end{verbatim}
564
+
565
+ Now let's say that we want to do some analysis in the code, but just
566
+ print the result and not the code itself. For this, we need to add the
567
+ option `echo = FALSE'.
568
+
569
+ \begin{verbatim}
570
+ ```{r second_r_chunk, echo = FALSE}
571
+ vec2 <- c(10, 20, 30)
572
+ vec3 <- vec * vec2
573
+ print(vec3)
574
+ ```
575
+ \end{verbatim}
576
+
577
+ Here is how this block will show up in the document. Observe that the
578
+ code is not shown and we only see the execution result in a white box
579
+
580
+ \begin{verbatim}
581
+ ## [1] 10 40 90
582
+ \end{verbatim}
583
+
584
+ A description of the available chunk options can be found in
585
+ \url{https://yihui.name/knitr/}.
586
+
587
+ Let's add another R chunk with a function definition. In this example, a
588
+ vector `r\_vec' is created and a new function `reduce\_sum' is defined.
589
+ The chunk specification is
590
+
591
+ \begin{verbatim}
592
+ ```{r data_creation}
593
+ r_vec <- c(1, 2, 3, 4, 5)
594
+
595
+ reduce_sum <- function(...) {
596
+ Reduce(sum, as.list(...))
597
+ }
598
+ ```
599
+ \end{verbatim}
600
+
601
+ and this is how it will look like once executed. From now on, to be
602
+ concise in the presentation we will not show chunk definitions any
603
+ longer.
604
+
605
+ \begin{Shaded}
606
+ \begin{Highlighting}[]
607
+ \NormalTok{r\_vec }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{)}
608
+
609
+ \NormalTok{reduce\_sum }\OtherTok{\textless{}{-}} \ControlFlowTok{function}\NormalTok{(...) \{}
610
+ \FunctionTok{Reduce}\NormalTok{(sum, }\FunctionTok{as.list}\NormalTok{(...))}
611
+ \NormalTok{\}}
612
+ \end{Highlighting}
613
+ \end{Shaded}
614
+
615
+ We can, possibly in another chunk, access the vector and call the
616
+ function as follows:
617
+
618
+ \begin{Shaded}
619
+ \begin{Highlighting}[]
620
+ \FunctionTok{print}\NormalTok{(r\_vec)}
621
+ \end{Highlighting}
622
+ \end{Shaded}
623
+
624
+ \begin{verbatim}
625
+ ## [1] 1 2 3 4 5
626
+ \end{verbatim}
627
+
628
+ \begin{Shaded}
629
+ \begin{Highlighting}[]
630
+ \FunctionTok{print}\NormalTok{(}\FunctionTok{reduce\_sum}\NormalTok{(r\_vec))}
631
+ \end{Highlighting}
632
+ \end{Shaded}
633
+
634
+ \begin{verbatim}
635
+ ## [1] 15
636
+ \end{verbatim}
637
+
638
+ \subsubsection{R Graphics with ggplot}\label{r-graphics-with-ggplot}
639
+
640
+ In the following chunk, we create a bubble chart in R using ggplot and
641
+ include it in this document. Note that there is no directive in the code
642
+ to include the image, this occurs automatically. The `mpg' dataframe is
643
+ natively available to R and to Galaaz as well.
644
+
645
+ For the reader not knowledgeable of ggplot, ggplot is a graphics library
646
+ based on ``the grammar of graphics'' (Wilkinson 2005). The idea of the
647
+ grammar of graphics is to build a graphics by adding layers to the plot.
648
+ More information can be found in
649
+ \url{https://towardsdatascience.com/a-comprehensive-guide-to-the-grammar-of-graphics-for-effective-visualization-of-multi-dimensional-1f92b4ed4149}.
650
+
651
+ In the plot below the `mpg' dataset from base R is used. ``The data
652
+ concerns city-cycle fuel consumption in miles per gallon, to be
653
+ predicted in terms of 3 multivalued discrete and 5 continuous
654
+ attributes.'' (Quinlan, 1993)
655
+
656
+ First, the `mpg' dataset if filtered to extract only cars from the
657
+ following manumactures: Audi, Ford, Honda, and Hyundai and stored in the
658
+ `mpg\_select' variable. Then, the selected dataframe is passed to the
659
+ ggplot function specifying in the aesthetic method (aes) that
660
+ `displacement' (disp) should be plotted in the `x' axis and `city
661
+ mileage' should be on the `y' axis. In the `labs' layer we pass the
662
+ `title' and `subtitle' for the plot. To the basic plot `g', geom\_jitter
663
+ is added, that plots cars from the same manufactures with the same color
664
+ (col=manufactures) and the size of the car point equal its high way
665
+ consumption (size = hwy). Finally, a last layer is plotter containing a
666
+ linear regression line (method = ``lm'') for every manufacturer.
667
+
668
+ \begin{Shaded}
669
+ \begin{Highlighting}[]
670
+ \CommentTok{\# load package and data}
671
+ \FunctionTok{library}\NormalTok{(ggplot2)}
672
+ \FunctionTok{data}\NormalTok{(mpg, }\AttributeTok{package=}\StringTok{"ggplot2"}\NormalTok{)}
673
+
674
+ \NormalTok{mpg\_select }\OtherTok{\textless{}{-}}\NormalTok{ mpg[}
675
+ \NormalTok{ mpg}\SpecialCharTok{$}\NormalTok{manufacturer }\SpecialCharTok{\%in\%} \FunctionTok{c}\NormalTok{(}\StringTok{"audi"}\NormalTok{, }\StringTok{"ford"}\NormalTok{, }\StringTok{"honda"}\NormalTok{, }\StringTok{"hyundai"}\NormalTok{),}
676
+ \NormalTok{]}
677
+
678
+ \CommentTok{\# Scatterplot}
679
+ \FunctionTok{theme\_set}\NormalTok{(}\FunctionTok{theme\_bw}\NormalTok{()) }\CommentTok{\# pre{-}set the bw theme.}
680
+ \NormalTok{g }\OtherTok{\textless{}{-}} \FunctionTok{ggplot}\NormalTok{(mpg\_select, }\FunctionTok{aes}\NormalTok{(displ, cty)) }\SpecialCharTok{+}
681
+ \FunctionTok{labs}\NormalTok{(}\AttributeTok{subtitle=}\StringTok{"mpg: Displacement vs City Mileage"}\NormalTok{,}
682
+ \AttributeTok{title=}\StringTok{"Bubble chart"}\NormalTok{)}
683
+
684
+ \NormalTok{g }\SpecialCharTok{+} \FunctionTok{geom\_jitter}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{col=}\NormalTok{manufacturer, }\AttributeTok{size=}\NormalTok{hwy)) }\SpecialCharTok{+}
685
+ \FunctionTok{geom\_smooth}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{col=}\NormalTok{manufacturer), }\AttributeTok{method=}\StringTok{"lm"}\NormalTok{, }\AttributeTok{se=}\NormalTok{F)}
686
+ \end{Highlighting}
687
+ \end{Shaded}
688
+
689
+ \begin{verbatim}
690
+ ## `geom_smooth()` using formula = 'y ~ x'
691
+ \end{verbatim}
692
+
693
+ \pandocbounded{\includegraphics[keepaspectratio]{gknit_files/gknit_files/figure-latex/bubble-1.png}}
694
+
695
+ \subsubsection{Ruby chunks}\label{ruby-chunks}
696
+
697
+ Including a Ruby chunk is just as easy as including an R chunk in the
698
+ document: just change the name of the engine to `ruby'. It is also
699
+ possible to pass chunk options to the Ruby engine; however, this version
700
+ does not accept all the options that are available to R chunks. Future
701
+ versions will add those options.
702
+
703
+ \begin{verbatim}
704
+ ```{ruby first_ruby_chunk}
705
+ ```
706
+ \end{verbatim}
707
+
708
+ In this example, the ruby chunk is called `first\_ruby\_chunk'. One
709
+ important aspect of chunk labels is that they cannot be duplicated. If a
710
+ chunk label is duplicated, gKnit will stop with an error.
711
+
712
+ In the following chunk, variable `a', `b' and `c' are standard Ruby
713
+ variables and `vec' and `vec2' are two vectors created by calling the
714
+ `c' method on the R module.
715
+
716
+ In Galaaz, the R module allows us to access R functions transparently.
717
+ The `c' function in R, is a function that concatenates its arguments
718
+ making a vector.
719
+
720
+ It should be clear that there is no requirement in gknit to call or use
721
+ any R functions. gKnit will knit standard Ruby code, or even general
722
+ text without any code.
723
+
724
+ \begin{Shaded}
725
+ \begin{Highlighting}[]
726
+ \NormalTok{a }\OperatorTok{=} \KeywordTok{[}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\KeywordTok{]}
727
+ \NormalTok{b }\OperatorTok{=} \StringTok{"US$ 250.000"}
728
+ \NormalTok{c }\OperatorTok{=} \StringTok{"The \textquotesingle{}outputs\textquotesingle{} function"}
729
+
730
+ \NormalTok{vec }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{)}
731
+ \NormalTok{vec2 }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\DecValTok{10}\NormalTok{, }\DecValTok{20}\NormalTok{, }\DecValTok{30}\NormalTok{)}
732
+ \end{Highlighting}
733
+ \end{Shaded}
734
+
735
+ In the next block, variables `a', `vec' and `vec2' are used and printed.
736
+
737
+ \begin{Shaded}
738
+ \begin{Highlighting}[]
739
+ \FunctionTok{puts}\NormalTok{ a}
740
+ \FunctionTok{puts}\NormalTok{ vec }\OperatorTok{*}\NormalTok{ vec2}
741
+ \end{Highlighting}
742
+ \end{Shaded}
743
+
744
+ \begin{verbatim}
745
+ ## 1
746
+ ## 2
747
+ ## 3
748
+ ## [1] 10 40 90
749
+ \end{verbatim}
750
+
751
+ Note that `a' is a standard Ruby Array and `vec' and `vec2' are vectors
752
+ that behave accordingly, where multiplication works as expected.
753
+
754
+ \subsubsection{Accessing R from Ruby}\label{accessing-r-from-ruby}
755
+
756
+ One of the nice aspects of Galaaz 2.0 is that variables and functions
757
+ defined in R can be easily accessed from Ruby. This next chunk, reads
758
+ data from R and uses the `reduce\_sum' function defined previously. To
759
+ access an R variable from Ruby the `\textasciitilde{}' function should
760
+ be applied to the Ruby symbol representing the R variable. Since the R
761
+ variable is called `r\_vec', in Ruby, the symbol to acess it is
762
+ `:r\_vec' and thus `\textasciitilde R{[}:r\_vec{]}' retrieves the value
763
+ of the variable.
764
+
765
+ \begin{Shaded}
766
+ \begin{Highlighting}[]
767
+ \FunctionTok{puts} \OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:r\_vec}\KeywordTok{]}
768
+ \end{Highlighting}
769
+ \end{Shaded}
770
+
771
+ \begin{verbatim}
772
+ ## [1] 1 2 3 4 5
773
+ \end{verbatim}
774
+
775
+ In order to call an R function, the `R.' module is used as follows
776
+
777
+ \begin{Shaded}
778
+ \begin{Highlighting}[]
779
+ \FunctionTok{puts} \ConstantTok{R}\AttributeTok{.reduce\_sum}\NormalTok{(}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:r\_vec}\KeywordTok{]}\NormalTok{)}
780
+ \end{Highlighting}
781
+ \end{Shaded}
782
+
783
+ \begin{verbatim}
784
+ ## [1] 15
785
+ \end{verbatim}
786
+
787
+ \subsubsection{Ruby Plotting}\label{ruby-plotting}
788
+
789
+ We have seen an example of plotting with R. Plotting with Ruby does not
790
+ require anything different from plotting with R. In the following
791
+ example, we plot a diverging bar graph using the `mtcars' dataframe from
792
+ R. This data was extracted from the 1974 Motor Trend US magazine, and
793
+ comprises fuel consumption and 10 aspects of automobile design and
794
+ performance for 32 automobiles (1973--74 models). The ten aspects are:
795
+
796
+ \begin{itemize}
797
+ \tightlist
798
+ \item
799
+ mpg: Miles/(US) gallon
800
+ \item
801
+ cyl: Number of cylinders
802
+ \item
803
+ disp: Displacement (cu.in.)
804
+ \item
805
+ hp: Gross horsepower
806
+ \item
807
+ drat: Rear axle ratio
808
+ \item
809
+ wt: Weight (1000 lbs)
810
+ \item
811
+ qsec: 1/4 mile time
812
+ \item
813
+ vs: Engine (0 = V-shaped, 1 = straight)
814
+ \item
815
+ am: Transmission (0 = automatic, 1 = manual)
816
+ \item
817
+ gear: Number of forward gears
818
+ \item
819
+ carb: Number of carburetors
820
+ \end{itemize}
821
+
822
+ \begin{Shaded}
823
+ \begin{Highlighting}[]
824
+ \CommentTok{\# copy the R variable :mtcars to the Ruby mtcars variable}
825
+ \NormalTok{mtcars }\OperatorTok{=} \OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}
826
+
827
+ \CommentTok{\# New column \textquotesingle{}car\_name\textquotesingle{} for plotting (rownames alone are not}
828
+ \CommentTok{\# usable as plot data).}
829
+ \NormalTok{mtcars}\AttributeTok{.car\_name} \OperatorTok{=} \ConstantTok{R}\AttributeTok{.rownames}\NormalTok{(}\WarningTok{:mtcars}\NormalTok{)}
830
+
831
+ \CommentTok{\# Normalized mpg in column mpg\_z: (mpg {-} mean) / sd, rounded.}
832
+ \NormalTok{mtcars}\AttributeTok{.mpg\_z} \OperatorTok{=}
833
+ \NormalTok{ ((mtcars}\AttributeTok{.mpg} \OperatorTok{{-}}\NormalTok{ mtcars}\AttributeTok{.mpg.mean}\NormalTok{) }\OperatorTok{/}\NormalTok{ mtcars}\AttributeTok{.mpg.sd}\NormalTok{)}\AttributeTok{.round} \DecValTok{2}
834
+
835
+ \CommentTok{\# mpg\_type: \textquotesingle{}below\textquotesingle{} if mpg\_z \textless{} 0, else \textquotesingle{}above\textquotesingle{} (vectorized ifelse).}
836
+ \NormalTok{mtcars}\AttributeTok{.mpg\_type} \OperatorTok{=}
837
+ \NormalTok{ (mtcars}\AttributeTok{.mpg\_z} \OperatorTok{\textless{}} \DecValTok{0}\NormalTok{)}\AttributeTok{.ifelse}\NormalTok{(}\StringTok{"below"}\NormalTok{, }\StringTok{"above"}\NormalTok{)}
838
+
839
+ \CommentTok{\# Order rows by mpg\_z ascending.}
840
+ \NormalTok{mtcars }\OperatorTok{=}\NormalTok{ mtcars}\KeywordTok{[}\NormalTok{mtcars}\AttributeTok{.mpg\_z.order}\NormalTok{, }\WarningTok{:all}\KeywordTok{]}
841
+
842
+ \CommentTok{\# Factor car\_name so plot keeps sorted order.}
843
+ \NormalTok{mtcars}\AttributeTok{.car\_name} \OperatorTok{=}
844
+ \NormalTok{ mtcars}\AttributeTok{.car\_name.factor} \WarningTok{levels:}\NormalTok{ mtcars}\AttributeTok{.car\_name}
845
+
846
+ \CommentTok{\# First records of the final data frame}
847
+ \FunctionTok{puts}\NormalTok{ mtcars}\AttributeTok{.head}
848
+ \end{Highlighting}
849
+ \end{Shaded}
850
+
851
+ \begin{verbatim}
852
+ ## mpg cyl disp hp drat wt qsec vs am gear
853
+ ## Cadillac Fleetwood 10.4 8 472 205 2.93 5.250 17.98 0 0 3
854
+ ## Lincoln Continental 10.4 8 460 215 3.00 5.424 17.82 0 0 3
855
+ ## Camaro Z28 13.3 8 350 245 3.73 3.840 15.41 0 0 3
856
+ ## Duster 360 14.3 8 360 245 3.21 3.570 15.84 0 0 3
857
+ ## Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3
858
+ ## Maserati Bora 15.0 8 301 335 3.54 3.570 14.60 0 1 5
859
+ ## carb car_name mpg_z mpg_type
860
+ ## Cadillac Fleetwood 4 Cadillac Fleetwood -1.61 below
861
+ ## Lincoln Continental 4 Lincoln Continental -1.61 below
862
+ ## Camaro Z28 4 Camaro Z28 -1.13 below
863
+ ## Duster 360 4 Duster 360 -0.96 below
864
+ ## Chrysler Imperial 4 Chrysler Imperial -0.89 below
865
+ ## Maserati Bora 8 Maserati Bora -0.84 below
866
+ \end{verbatim}
867
+
868
+ \begin{Shaded}
869
+ \begin{Highlighting}[]
870
+ \FunctionTok{require} \VerbatimStringTok{\textquotesingle{}ggplot\textquotesingle{}}
871
+
872
+ \FunctionTok{puts}\NormalTok{ mtcars}\AttributeTok{.ggplot}\NormalTok{(}
873
+ \ConstantTok{E}\AttributeTok{.aes}\NormalTok{(}\WarningTok{x:} \WarningTok{:car\_name}\NormalTok{, }\WarningTok{y:} \WarningTok{:mpg\_z}\NormalTok{, }\WarningTok{label:} \WarningTok{:mpg\_z}\NormalTok{)) }\OperatorTok{+}
874
+ \ConstantTok{R}\AttributeTok{.geom\_bar}\NormalTok{(}\ConstantTok{E}\AttributeTok{.aes}\NormalTok{(}\WarningTok{fill:} \WarningTok{:mpg\_type}\NormalTok{),}
875
+ \WarningTok{stat:} \VerbatimStringTok{\textquotesingle{}identity\textquotesingle{}}\NormalTok{, }\WarningTok{width:} \FloatTok{0.5}\NormalTok{) }\OperatorTok{+}
876
+ \ConstantTok{R}\AttributeTok{.scale\_fill\_manual}\NormalTok{(}
877
+ \WarningTok{name:} \VerbatimStringTok{\textquotesingle{}Mileage\textquotesingle{}}\NormalTok{,}
878
+ \WarningTok{labels:} \ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}Above Average\textquotesingle{}}\NormalTok{, }\VerbatimStringTok{\textquotesingle{}Below Average\textquotesingle{}}\NormalTok{),}
879
+ \WarningTok{values:} \ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}above\textquotesingle{}}\WarningTok{:} \VerbatimStringTok{\textquotesingle{}\#00ba38\textquotesingle{}}\NormalTok{,}
880
+ \VerbatimStringTok{\textquotesingle{}below\textquotesingle{}}\WarningTok{:} \VerbatimStringTok{\textquotesingle{}\#f8766d\textquotesingle{}}\NormalTok{)) }\OperatorTok{+}
881
+ \ConstantTok{R}\AttributeTok{.labs}\NormalTok{(}\WarningTok{subtitle:} \StringTok{"Normalised mileage from \textquotesingle{}mtcars\textquotesingle{}"}\NormalTok{,}
882
+ \WarningTok{title:} \StringTok{"Diverging Bars"}\NormalTok{) }\OperatorTok{+}
883
+ \ConstantTok{R}\AttributeTok{.coord\_flip}
884
+ \end{Highlighting}
885
+ \end{Shaded}
886
+
887
+ \pandocbounded{\includegraphics[keepaspectratio]{gknit_files/gknit_files/figure-latex/diverging_bar.pdf}}
888
+
889
+ \subsubsection{Inline Ruby code}\label{inline-ruby-code}
890
+
891
+ When using a Ruby chunk, the code and the output are formatted in blocks
892
+ as seen above. This formatting is not always desired. Sometimes, we want
893
+ to have the results of the Ruby evaluation included in the middle of a
894
+ phrase. gKnit allows adding inline Ruby code with the `rb' engine. The
895
+ following chunk specification will create and inline Ruby text:
896
+
897
+ \begin{verbatim}
898
+ This is some text with inline Ruby accessing
899
+ variable 'b' which has value:
900
+ ```{rb puts b}
901
+ ```
902
+ and is followed by some other text!
903
+ \end{verbatim}
904
+
905
+ This is some text with inline Ruby accessing variable `b' which has
906
+ value: US\$ 250.000 and is followed by some other text!
907
+
908
+ Note that it is important not to add any new line before of after the
909
+ code block if we want everything to be in only one line, resulting in
910
+ the following sentence with inline Ruby code.
911
+
912
+ \subsubsection{The `outputs' function}\label{the-outputs-function}
913
+
914
+ He have previously used the standard `puts' method in Ruby chunks in
915
+ order produce output. The result of a `puts', as seen in all previous
916
+ chunks that use it, is formatted inside a white box that follows the
917
+ code block. Many times however, we would like to do some processing in
918
+ the Ruby chunk and have the result of this processing generate and
919
+ output that is ``included'' in the document as if we had typed it in
920
+ \textbf{R markdown} document.
921
+
922
+ For example, suppose we want to create a new heading in our document,
923
+ but the heading phrase is the result of some code processing: maybe it's
924
+ the first line of a file we are going to read. Method `outputs' adds its
925
+ output as if typed in the \textbf{R markdown} document.
926
+
927
+ Take now a look at variable `c' (it was defined in a previous block
928
+ above) as `c = ``The `outputs' function''. ``The `outputs' function'' is
929
+ actually the name of this section and it was created using the 'outputs'
930
+ function inside a Ruby chunk.
931
+
932
+ The ruby chunk to generate this heading is:
933
+
934
+ \begin{verbatim}
935
+ ```{ruby heading}
936
+ outputs "### #{c}"
937
+ ```
938
+ \end{verbatim}
939
+
940
+ The three `\#\#\#' is the way we add a Heading 3 in \textbf{R markdown}.
941
+
942
+ \subsubsection{HTML Output from Ruby
943
+ Chunks}\label{html-output-from-ruby-chunks}
944
+
945
+ We've just seen the use of method `outputs' to add text to the the
946
+ \textbf{R markdown} document. This technique can also be used to add
947
+ HTML code to the document. In \textbf{R markdown}, any html code typed
948
+ directly in the document will be properly rendered.\\
949
+ Here, for instance, is a table definition in HTML and its output in the
950
+ document:
951
+
952
+ \begin{verbatim}
953
+ <table style="width:100%">
954
+ <tr>
955
+ <th>Firstname</th>
956
+ <th>Lastname</th>
957
+ <th>Age</th>
958
+ </tr>
959
+ <tr>
960
+ <td>Jill</td>
961
+ <td>Smith</td>
962
+ <td>50</td>
963
+ </tr>
964
+ <tr>
965
+ <td>Eve</td>
966
+ <td>Jackson</td>
967
+ <td>94</td>
968
+ </tr>
969
+ </table>
970
+ \end{verbatim}
971
+
972
+ Firstname
973
+
974
+ Lastname
975
+
976
+ Age
977
+
978
+ Jill
979
+
980
+ Smith
981
+
982
+ 50
983
+
984
+ Eve
985
+
986
+ Jackson
987
+
988
+ 94
989
+
990
+ But manually creating HTML output is not always easy or desirable,
991
+ specially if we intend the document to be rendered in other formats, for
992
+ example, as LaTeX. Also, The above table looks ugly. The `kableExtra'
993
+ library is a great library for creating beautiful tables. Take a look at
994
+ \url{https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html}
995
+
996
+ In the next chunk, we output the `mtcars' dataframe from R in a nicely
997
+ formatted table. Note that we retrieve the mtcars dataframe by using
998
+ `\textasciitilde R{[}:mtcars{]}'.
999
+
1000
+ \begin{Shaded}
1001
+ \begin{Highlighting}[]
1002
+ \ConstantTok{R}\AttributeTok{.install\_and\_loads}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}kableExtra\textquotesingle{}}\NormalTok{)}
1003
+ \NormalTok{outputs (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}\NormalTok{)}\AttributeTok{.kable.kable\_styling}
1004
+ \end{Highlighting}
1005
+ \end{Shaded}
1006
+
1007
+ \begin{longtable}[t]{lrrrrrrrrrrr}
1008
+ \toprule
1009
+ & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
1010
+ \midrule
1011
+ Mazda RX4 & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4\\
1012
+ Mazda RX4 Wag & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4\\
1013
+ Datsun 710 & 22.8 & 4 & 108.0 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1\\
1014
+ Hornet 4 Drive & 21.4 & 6 & 258.0 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1\\
1015
+ Hornet Sportabout & 18.7 & 8 & 360.0 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2\\
1016
+ \addlinespace
1017
+ Valiant & 18.1 & 6 & 225.0 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1\\
1018
+ Duster 360 & 14.3 & 8 & 360.0 & 245 & 3.21 & 3.570 & 15.84 & 0 & 0 & 3 & 4\\
1019
+ Merc 240D & 24.4 & 4 & 146.7 & 62 & 3.69 & 3.190 & 20.00 & 1 & 0 & 4 & 2\\
1020
+ Merc 230 & 22.8 & 4 & 140.8 & 95 & 3.92 & 3.150 & 22.90 & 1 & 0 & 4 & 2\\
1021
+ Merc 280 & 19.2 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.30 & 1 & 0 & 4 & 4\\
1022
+ \addlinespace
1023
+ Merc 280C & 17.8 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.90 & 1 & 0 & 4 & 4\\
1024
+ Merc 450SE & 16.4 & 8 & 275.8 & 180 & 3.07 & 4.070 & 17.40 & 0 & 0 & 3 & 3\\
1025
+ Merc 450SL & 17.3 & 8 & 275.8 & 180 & 3.07 & 3.730 & 17.60 & 0 & 0 & 3 & 3\\
1026
+ Merc 450SLC & 15.2 & 8 & 275.8 & 180 & 3.07 & 3.780 & 18.00 & 0 & 0 & 3 & 3\\
1027
+ Cadillac Fleetwood & 10.4 & 8 & 472.0 & 205 & 2.93 & 5.250 & 17.98 & 0 & 0 & 3 & 4\\
1028
+ \addlinespace
1029
+ Lincoln Continental & 10.4 & 8 & 460.0 & 215 & 3.00 & 5.424 & 17.82 & 0 & 0 & 3 & 4\\
1030
+ Chrysler Imperial & 14.7 & 8 & 440.0 & 230 & 3.23 & 5.345 & 17.42 & 0 & 0 & 3 & 4\\
1031
+ Fiat 128 & 32.4 & 4 & 78.7 & 66 & 4.08 & 2.200 & 19.47 & 1 & 1 & 4 & 1\\
1032
+ Honda Civic & 30.4 & 4 & 75.7 & 52 & 4.93 & 1.615 & 18.52 & 1 & 1 & 4 & 2\\
1033
+ Toyota Corolla & 33.9 & 4 & 71.1 & 65 & 4.22 & 1.835 & 19.90 & 1 & 1 & 4 & 1\\
1034
+ \addlinespace
1035
+ Toyota Corona & 21.5 & 4 & 120.1 & 97 & 3.70 & 2.465 & 20.01 & 1 & 0 & 3 & 1\\
1036
+ Dodge Challenger & 15.5 & 8 & 318.0 & 150 & 2.76 & 3.520 & 16.87 & 0 & 0 & 3 & 2\\
1037
+ AMC Javelin & 15.2 & 8 & 304.0 & 150 & 3.15 & 3.435 & 17.30 & 0 & 0 & 3 & 2\\
1038
+ Camaro Z28 & 13.3 & 8 & 350.0 & 245 & 3.73 & 3.840 & 15.41 & 0 & 0 & 3 & 4\\
1039
+ Pontiac Firebird & 19.2 & 8 & 400.0 & 175 & 3.08 & 3.845 & 17.05 & 0 & 0 & 3 & 2\\
1040
+ \addlinespace
1041
+ Fiat X1-9 & 27.3 & 4 & 79.0 & 66 & 4.08 & 1.935 & 18.90 & 1 & 1 & 4 & 1\\
1042
+ Porsche 914-2 & 26.0 & 4 & 120.3 & 91 & 4.43 & 2.140 & 16.70 & 0 & 1 & 5 & 2\\
1043
+ Lotus Europa & 30.4 & 4 & 95.1 & 113 & 3.77 & 1.513 & 16.90 & 1 & 1 & 5 & 2\\
1044
+ Ford Pantera L & 15.8 & 8 & 351.0 & 264 & 4.22 & 3.170 & 14.50 & 0 & 1 & 5 & 4\\
1045
+ Ferrari Dino & 19.7 & 6 & 145.0 & 175 & 3.62 & 2.770 & 15.50 & 0 & 1 & 5 & 6\\
1046
+ \addlinespace
1047
+ Maserati Bora & 15.0 & 8 & 301.0 & 335 & 3.54 & 3.570 & 14.60 & 0 & 1 & 5 & 8\\
1048
+ Volvo 142E & 21.4 & 4 & 121.0 & 109 & 4.11 & 2.780 & 18.60 & 1 & 1 & 4 & 2\\
1049
+ \bottomrule
1050
+ \end{longtable}
1051
+
1052
+ \subsubsection{Including Ruby files in a
1053
+ chunk}\label{including-ruby-files-in-a-chunk}
1054
+
1055
+ R is a language that was created to be easy and fast for statisticians
1056
+ to use. As far as I know, it was not a language to be used for
1057
+ developing large systems. Of course, there are large systems and
1058
+ libraries in R, but the focus of the language is for developing
1059
+ statistical models and distribute that to peers.
1060
+
1061
+ Ruby on the other hand, is a language for large software development.
1062
+ Systems written in Ruby will have dozens, hundreds or even thousands of
1063
+ files. To document a large system with literate programming, we cannot
1064
+ expect the developer to add all the files in a single `.Rmd' file. gKnit
1065
+ provides the `include' chunk engine to include a Ruby file as if it had
1066
+ being typed in the `.Rmd' file.
1067
+
1068
+ To include a file, the following chunk should be created, where is the
1069
+ name of the file to be included and where the extension, if it is `.rb',
1070
+ does not need to be added. If the `relative' option is not included,
1071
+ then it is treated as TRUE. When `relative' is true, ruby's
1072
+ `require\_relative' semantics is used to load the file, when false,
1073
+ Ruby's \$LOAD\_PATH is searched to find the file and it is 'require'd.
1074
+
1075
+ \begin{verbatim}
1076
+ ```{include <filename>, relative = <TRUE/FALSE>}
1077
+ ```
1078
+ \end{verbatim}
1079
+
1080
+ Below we include file `model.rb', which is in the same directory of this
1081
+ blog.\\
1082
+ This code uses R `caret' package to split a dataset in a train and test
1083
+ sets. The `caret' package is a very important a useful package for doing
1084
+ Data Analysis, it has hundreds of functions for all steps of the Data
1085
+ Analysis workflow. To use `caret' just to split a dataset is like using
1086
+ the proverbial cannon to kill the fly. We use it here only to show that
1087
+ integrating Ruby and R and using even a very complex package as `caret'
1088
+ is trivial with Galaaz.
1089
+
1090
+ A word of advice: the `caret' package has lots of dependencies and
1091
+ installing it in a Linux system is a time consuming operation. Method
1092
+ `R.install\_and\_loads' will install the package if it is not already
1093
+ installed (via \textbf{\texttt{R::Job}}: a child \texttt{Rscript}, so
1094
+ the bridge stays free) and can take a while.
1095
+
1096
+ \begin{verbatim}
1097
+ ```{include model}
1098
+ ```
1099
+ \end{verbatim}
1100
+
1101
+ \begin{Shaded}
1102
+ \begin{Highlighting}[]
1103
+ \NormalTok{require \textquotesingle{}galaaz\textquotesingle{}}
1104
+
1105
+ \NormalTok{\# Loads the R \textquotesingle{}caret\textquotesingle{} package. If not present, installs it }
1106
+ \NormalTok{R.install\_and\_loads \textquotesingle{}caret\textquotesingle{}}
1107
+
1108
+ \NormalTok{class Model}
1109
+
1110
+ \NormalTok{ attr\_reader :data}
1111
+ \NormalTok{ attr\_reader :test}
1112
+ \NormalTok{ attr\_reader :train}
1113
+
1114
+ \NormalTok{ \#==========================================================}
1115
+ \NormalTok{ \#}
1116
+ \NormalTok{ \#==========================================================}
1117
+
1118
+ \NormalTok{ def initialize(data, percent\_train:, seed: 123)}
1119
+
1120
+ \NormalTok{ R.set\_\_seed(seed)}
1121
+ \NormalTok{ @data = data}
1122
+ \NormalTok{ @percent\_train = percent\_train}
1123
+ \NormalTok{ @seed = seed}
1124
+
1125
+ \NormalTok{ end}
1126
+
1127
+ \NormalTok{ \#==========================================================}
1128
+ \NormalTok{ \#}
1129
+ \NormalTok{ \#==========================================================}
1130
+
1131
+ \NormalTok{ def partition(field)}
1132
+
1133
+ \NormalTok{ train\_index =}
1134
+ \NormalTok{ R.createDataPartition(@data.send(field), p: @percent\_train,}
1135
+ \NormalTok{ list: false, times: 1)}
1136
+ \NormalTok{ @train = @data[train\_index, :all]}
1137
+ \NormalTok{ @test = @data[{-}train\_index, :all]}
1138
+
1139
+ \NormalTok{ end}
1140
+
1141
+ \NormalTok{end}
1142
+ \end{Highlighting}
1143
+ \end{Shaded}
1144
+
1145
+ \begin{Shaded}
1146
+ \begin{Highlighting}[]
1147
+ \NormalTok{mtcars }\OperatorTok{=} \OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}
1148
+ \NormalTok{model }\OperatorTok{=} \DataTypeTok{Model}\AttributeTok{.new}\NormalTok{(mtcars, }\WarningTok{percent\_train:} \FloatTok{0.8}\NormalTok{)}
1149
+ \NormalTok{model}\AttributeTok{.partition}\NormalTok{(}\WarningTok{:mpg}\NormalTok{)}
1150
+ \FunctionTok{puts}\NormalTok{ model}\AttributeTok{.train.head}
1151
+ \FunctionTok{puts}\NormalTok{ model}\AttributeTok{.test.head}
1152
+ \end{Highlighting}
1153
+ \end{Shaded}
1154
+
1155
+ \begin{verbatim}
1156
+ ## mpg cyl disp hp drat wt qsec vs am gear carb
1157
+ ## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
1158
+ ## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
1159
+ ## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
1160
+ ## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
1161
+ ## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
1162
+ ## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
1163
+ ## mpg cyl disp hp drat wt qsec vs am gear carb
1164
+ ## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
1165
+ ## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
1166
+ ## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
1167
+ ## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
1168
+ \end{verbatim}
1169
+
1170
+ \subsubsection{Documenting Gems}\label{documenting-gems}
1171
+
1172
+ gKnit also allows developers to document and load files that are not in
1173
+ the same directory of the `.Rmd' file.
1174
+
1175
+ Here is an example of loading the `find.rb' file from Ruby (via
1176
+ \texttt{\$LOAD\_PATH}). In this example, relative is set to FALSE, so
1177
+ Ruby will look for the file in its \$LOAD\_PATH, and the user does not
1178
+ need to know its directory.
1179
+
1180
+ \begin{verbatim}
1181
+ ```{include find, relative = FALSE}
1182
+ ```
1183
+ \end{verbatim}
1184
+
1185
+ \begin{Shaded}
1186
+ \begin{Highlighting}[]
1187
+ \NormalTok{\# frozen\_string\_literal: true}
1188
+ \NormalTok{\#}
1189
+ \NormalTok{\# find.rb: the Find module for processing all files under a given directory.}
1190
+ \NormalTok{\#}
1191
+
1192
+ \NormalTok{\#}
1193
+ \NormalTok{\# The +Find+ module supports the top{-}down traversal of a set of file paths.}
1194
+ \NormalTok{\#}
1195
+ \NormalTok{\# For example, to total the size of all files under your home directory,}
1196
+ \NormalTok{\# ignoring anything in a "dot" directory (e.g. $HOME/.ssh):}
1197
+ \NormalTok{\#}
1198
+ \NormalTok{\# require \textquotesingle{}find\textquotesingle{}}
1199
+ \NormalTok{\#}
1200
+ \NormalTok{\# total\_size = 0}
1201
+ \NormalTok{\#}
1202
+ \NormalTok{\# Find.find(ENV["HOME"]) do |path|}
1203
+ \NormalTok{\# if FileTest.directory?(path)}
1204
+ \NormalTok{\# if File.basename(path).start\_with?(\textquotesingle{}.\textquotesingle{})}
1205
+ \NormalTok{\# Find.prune \# Don\textquotesingle{}t look any further into this directory.}
1206
+ \NormalTok{\# else}
1207
+ \NormalTok{\# next}
1208
+ \NormalTok{\# end}
1209
+ \NormalTok{\# else}
1210
+ \NormalTok{\# total\_size += FileTest.size(path)}
1211
+ \NormalTok{\# end}
1212
+ \NormalTok{\# end}
1213
+ \NormalTok{\#}
1214
+ \NormalTok{module Find}
1215
+
1216
+ \NormalTok{ VERSION = "0.2.0"}
1217
+
1218
+ \NormalTok{ \#}
1219
+ \NormalTok{ \# Calls the associated block with the name of every file and directory listed}
1220
+ \NormalTok{ \# as arguments, then recursively on their subdirectories, and so on.}
1221
+ \NormalTok{ \#}
1222
+ \NormalTok{ \# Returns an enumerator if no block is given.}
1223
+ \NormalTok{ \#}
1224
+ \NormalTok{ \# See the +Find+ module documentation for an example.}
1225
+ \NormalTok{ \#}
1226
+ \NormalTok{ def find(*paths, ignore\_error: true) \# :yield: path}
1227
+ \NormalTok{ block\_given? or return enum\_for(\_\_method\_\_, *paths, ignore\_error: ignore\_error)}
1228
+
1229
+ \NormalTok{ fs\_encoding = Encoding.find("filesystem")}
1230
+
1231
+ \NormalTok{ paths.collect!\{|d| raise Errno::ENOENT, d unless File.exist?(d); d.dup\}.each do |path|}
1232
+ \NormalTok{ path = path.to\_path if path.respond\_to? :to\_path}
1233
+ \NormalTok{ enc = path.encoding == Encoding::US\_ASCII ? fs\_encoding : path.encoding}
1234
+ \NormalTok{ ps = [path]}
1235
+ \NormalTok{ while file = ps.shift}
1236
+ \NormalTok{ catch(:prune) do}
1237
+ \NormalTok{ yield file.dup}
1238
+ \NormalTok{ begin}
1239
+ \NormalTok{ s = File.lstat(file)}
1240
+ \NormalTok{ rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG, Errno::EINVAL}
1241
+ \NormalTok{ raise unless ignore\_error}
1242
+ \NormalTok{ next}
1243
+ \NormalTok{ end}
1244
+ \NormalTok{ if s.directory? then}
1245
+ \NormalTok{ begin}
1246
+ \NormalTok{ fs = Dir.children(file, encoding: enc)}
1247
+ \NormalTok{ rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG, Errno::EINVAL}
1248
+ \NormalTok{ raise unless ignore\_error}
1249
+ \NormalTok{ next}
1250
+ \NormalTok{ end}
1251
+ \NormalTok{ fs.sort!}
1252
+ \NormalTok{ fs.reverse\_each \{|f|}
1253
+ \NormalTok{ f = File.join(file, f)}
1254
+ \NormalTok{ ps.unshift f}
1255
+ \NormalTok{ \}}
1256
+ \NormalTok{ end}
1257
+ \NormalTok{ end}
1258
+ \NormalTok{ end}
1259
+ \NormalTok{ end}
1260
+ \NormalTok{ nil}
1261
+ \NormalTok{ end}
1262
+
1263
+ \NormalTok{ \#}
1264
+ \NormalTok{ \# Skips the current file or directory, restarting the loop with the next}
1265
+ \NormalTok{ \# entry. If the current file is a directory, that directory will not be}
1266
+ \NormalTok{ \# recursively entered. Meaningful only within the block associated with}
1267
+ \NormalTok{ \# Find::find.}
1268
+ \NormalTok{ \#}
1269
+ \NormalTok{ \# See the +Find+ module documentation for an example.}
1270
+ \NormalTok{ \#}
1271
+ \NormalTok{ def prune}
1272
+ \NormalTok{ throw :prune}
1273
+ \NormalTok{ end}
1274
+
1275
+ \NormalTok{ module\_function :find, :prune}
1276
+ \NormalTok{end}
1277
+ \end{Highlighting}
1278
+ \end{Shaded}
1279
+
1280
+ \subsection{Converting to PDF}\label{converting-to-pdf}
1281
+
1282
+ One of the beauties of knitr is that the same input can be converted to
1283
+ many different outputs. One very useful format, is, of course, PDF. In
1284
+ order to converted an \textbf{R markdown} file to PDF it is necessary to
1285
+ have LaTeX installed on the system. We will not explain here how to
1286
+ install LaTeX as there are plenty of documents on the web showing how to
1287
+ proceed.
1288
+
1289
+ gKnit comes with a simple LaTeX style file for gknitting this blog as a
1290
+ PDF document. Here is the Yaml header to generate this blog in PDF
1291
+ format instead of HTML:
1292
+
1293
+ \begin{verbatim}
1294
+ ---
1295
+ title: "gKnit - Ruby and R Knitting with Galaaz"
1296
+ author: "Rodrigo Botafogo"
1297
+ tags: [Galaaz, Ruby, R, JRuby, CRuby, "GNU R", knitr, gknit]
1298
+ date: "29 October 2018"
1299
+ output:
1300
+ pdf\_document:
1301
+ includes:
1302
+ in\_header: ["../../sty/galaaz.sty"]
1303
+ number\_sections: yes
1304
+ ---
1305
+ \end{verbatim}
1306
+
1307
+ \section{Conclusion}\label{conclusion}
1308
+
1309
+ In order to do reproducible research, one of the main basic tools needed
1310
+ is a system that allows ``literate programming'' where text, code and
1311
+ possibly a set of files can be compiled onto a report that can be easily
1312
+ distributed to peers. Peers should be able to use this same set of files
1313
+ to rerun the compilation by their own obtaining the exact same original
1314
+ report. gKnit is such a system for Ruby and R. It uses \textbf{R
1315
+ Markdown} to integrate text and code chunks, where code chunks can
1316
+ either be part of the \textbf{R Markdown} file or be imported from files
1317
+ in the system. Ideally, in reproducible research, all the files needed
1318
+ to rebuild a report should be easily packed together (in the same zipped
1319
+ directory) and distributed to peers for reexecution.
1320
+
1321
+ \textbf{Galaaz 2.0} pairs \textbf{JRuby or CRuby} with \textbf{GNU R}:
1322
+ you keep the full CRAN/Bioconductor world in R while writing
1323
+ orchestration, reuse, and application code in Ruby. The effort to wrap
1324
+ Ruby over R (Galaaz) and to wrap Knitr as gKnit was tiny compared to
1325
+ reimplementing R's ecosystem in Ruby---much like Python's investment in
1326
+ NumPy and Pandas, which no Ruby project is likely to duplicate.
1327
+
1328
+ An \textbf{earlier} prototype used Oracle's \textbf{GraalVM} and Truffle
1329
+ interop; the \textbf{current} stack is deliberately \textbf{standard GNU
1330
+ R} plus the Galaaz \textbf{bridge} on JRuby or CRuby, documented in the
1331
+ project manual.
1332
+
1333
+ More interesting than wrapping the R libraries with Ruby, is that Ruby
1334
+ adds value to R, by allowing developers to use powerful and modern
1335
+ constructs for code reuse that are not the strong points of R. As shown
1336
+ in this blog, R and Ruby can easily communicate and R can be structured
1337
+ in classes and modules in a way that greatly expands its power and
1338
+ readability.
1339
+
1340
+ \section{Installing gKnit}\label{installing-gknit}
1341
+
1342
+ \subsection{Prerequisites (Galaaz 2.0)}\label{prerequisites-galaaz-2.0}
1343
+
1344
+ \begin{itemize}
1345
+ \tightlist
1346
+ \item
1347
+ \textbf{JRuby} and a compatible \textbf{JDK}, \emph{or} \textbf{CRuby
1348
+ 3.3+}
1349
+ \item
1350
+ \textbf{GNU R} on your \texttt{PATH}
1351
+ \end{itemize}
1352
+
1353
+ The following R packages will be automatically installed when necessary,
1354
+ but could be installed prior to using gKnit if desired:
1355
+
1356
+ \begin{itemize}
1357
+ \tightlist
1358
+ \item
1359
+ ggplot2
1360
+ \item
1361
+ gridExtra
1362
+ \item
1363
+ knitr
1364
+ \end{itemize}
1365
+
1366
+ Installation of R packages requires a development environment and can be
1367
+ time consuming. On Linux, the usual build tools are typically enough. On
1368
+ macOS, Xcode command-line tools are commonly required.
1369
+
1370
+ \subsection{Preparation}\label{preparation}
1371
+
1372
+ \begin{itemize}
1373
+ \tightlist
1374
+ \item
1375
+ Install the \textbf{galaaz} gem (RubyGems or a local build /
1376
+ \texttt{path:}).
1377
+ \end{itemize}
1378
+
1379
+ \subsection{Usage}\label{usage}
1380
+
1381
+ \begin{itemize}
1382
+ \tightlist
1383
+ \item
1384
+ \textbf{\texttt{bin/gknit}} \textless filename\textgreater{} (from the
1385
+ Galaaz repo or your install layout); use
1386
+ \textbf{\texttt{-\/-output\_format\ all}} for HTML and PDF together.
1387
+ \item
1388
+ Run Ruby with \textbf{\texttt{bin/galaaz-ruby}} (either engine) or
1389
+ \textbf{\texttt{bin/galaaz-jruby}} when you need JRuby JVM flags (see
1390
+ the manual).
1391
+ \end{itemize}
1392
+
1393
+ \section*{References}\label{references}
1394
+ \addcontentsline{toc}{section}{References}
1395
+
1396
+ \protect\phantomsection\label{refs}
1397
+ \begin{CSLReferences}{1}{1}
1398
+ \bibitem[\citeproctext]{ref-Knuth:literate_programming}
1399
+ Knuth, Donald E. 1984. {``Literate Programming.''} \emph{Comput. J.}
1400
+ (Oxford, UK) 27 (2): 97--111.
1401
+ \url{https://doi.org/10.1093/comjnl/27.2.97}.
1402
+
1403
+ \bibitem[\citeproctext]{ref-Wilkinson:grammar_of_graphics}
1404
+ Wilkinson, Leland. 2005. \emph{The Grammar of Graphics (Statistics and
1405
+ Computing)}. Springer-Verlag.
1406
+
1407
+ \end{CSLReferences}
1408
+
1409
+ \end{document}