galaaz 2.1.7 → 2.1.8

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