galaaz 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +29 -0
  3. data/bin/gknit +208 -10
  4. data/bin/gknit2 +14 -0
  5. data/bin/gknit2~ +6 -0
  6. data/bin/prepareR.rb +3 -0
  7. data/bin/prepareR.rb~ +1 -0
  8. data/bin/tmp.py +51 -0
  9. data/blogs/dev/dev.Rmd +70 -0
  10. data/blogs/dev/dev.Rmd~ +104 -0
  11. data/blogs/dev/dev.html +209 -0
  12. data/blogs/dev/dev.md +72 -0
  13. data/blogs/dev/dev_files/figure-html/bubble-1.png +0 -0
  14. data/blogs/dev/model.rb +41 -0
  15. data/blogs/galaaz_ggplot/galaaz_ggplot.Rmd +55 -27
  16. data/blogs/galaaz_ggplot/galaaz_ggplot.aux +44 -0
  17. data/blogs/galaaz_ggplot/galaaz_ggplot.dvi +0 -0
  18. data/blogs/galaaz_ggplot/galaaz_ggplot.html +17 -4
  19. data/blogs/galaaz_ggplot/galaaz_ggplot.out +10 -0
  20. data/blogs/galaaz_ggplot/galaaz_ggplot.pdf +0 -0
  21. data/blogs/galaaz_ggplot/galaaz_ggplot.tex +630 -0
  22. data/blogs/galaaz_ggplot/midwest.Rmd +1 -1
  23. data/blogs/galaaz_ggplot/midwest_external_png +13 -0
  24. data/blogs/galaaz_ggplot/midwest_external_png~ +1 -0
  25. data/blogs/gknit/gknit.Rmd +500 -0
  26. data/blogs/gknit/gknit.Rmd~ +184 -0
  27. data/blogs/gknit/gknit.Rnd~ +17 -0
  28. data/blogs/gknit/gknit.html +528 -0
  29. data/blogs/gknit/gknit.md +628 -0
  30. data/blogs/gknit/gknit.pdf +0 -0
  31. data/blogs/gknit/gknit.tex +745 -0
  32. data/blogs/gknit/gknit_files/figure-html/bubble-1.png +0 -0
  33. data/blogs/gknit/gknit_files/figure-html/diverging_bar.png +0 -0
  34. data/blogs/gknit/model.rb +41 -0
  35. data/blogs/gknit/model.rb~ +46 -0
  36. data/blogs/ruby_plot/figures/dose_len.png +0 -0
  37. data/blogs/ruby_plot/figures/facet_by_delivery.png +0 -0
  38. data/blogs/ruby_plot/figures/facet_by_dose.png +0 -0
  39. data/blogs/ruby_plot/figures/facets_by_delivery_color.png +0 -0
  40. data/blogs/ruby_plot/figures/facets_by_delivery_color2.png +0 -0
  41. data/blogs/ruby_plot/figures/facets_with_decorations.png +0 -0
  42. data/blogs/ruby_plot/figures/facets_with_jitter.png +0 -0
  43. data/blogs/ruby_plot/figures/facets_with_points.png +0 -0
  44. data/blogs/ruby_plot/figures/final_box_plot.png +0 -0
  45. data/blogs/ruby_plot/figures/final_violin_plot.png +0 -0
  46. data/blogs/ruby_plot/figures/violin_with_jitter.png +0 -0
  47. data/blogs/ruby_plot/ruby_plot.Rmd +680 -0
  48. data/blogs/ruby_plot/ruby_plot.Rmd~ +215 -0
  49. data/blogs/ruby_plot/ruby_plot.html +563 -0
  50. data/blogs/ruby_plot/ruby_plot.md +731 -0
  51. data/blogs/ruby_plot/ruby_plot.pdf +0 -0
  52. data/blogs/ruby_plot/ruby_plot.tex +458 -0
  53. data/examples/sthda_ggplot/all.rb +0 -6
  54. data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_hex.rb +1 -1
  55. data/examples/sthda_ggplot/two_variables_cont_cont/misc.rb +1 -1
  56. data/examples/sthda_ggplot/two_variables_disc_cont/geom_bar.rb +2 -2
  57. data/examples/sthda_ggplot/two_variables_disc_disc/geom_jitter.rb +0 -1
  58. data/lib/R/eng_ruby.R +62 -0
  59. data/lib/R/eng_ruby.R~ +63 -0
  60. data/lib/R_interface/capture_plot.rb~ +23 -0
  61. data/lib/{R → R_interface}/expression.rb +0 -0
  62. data/lib/{R → R_interface}/r.rb +10 -1
  63. data/lib/{R → R_interface}/r.rb~ +0 -0
  64. data/lib/{R → R_interface}/r_methods.rb +21 -5
  65. data/lib/{R → R_interface}/rbinary_operators.rb +6 -1
  66. data/lib/R_interface/rclosure.rb +38 -0
  67. data/lib/{R → R_interface}/rdata_frame.rb +0 -0
  68. data/lib/R_interface/rdevices.R +31 -0
  69. data/lib/R_interface/rdevices.rb +225 -0
  70. data/lib/{R/rclosure.rb → R_interface/rdevices.rb~} +3 -10
  71. data/lib/{R → R_interface}/renvironment.rb +0 -0
  72. data/lib/{R → R_interface}/rexpression.rb +0 -0
  73. data/lib/{R → R_interface}/rindexed_object.rb +0 -0
  74. data/lib/{R → R_interface}/rlanguage.rb +0 -0
  75. data/lib/{R → R_interface}/rlist.rb +0 -0
  76. data/lib/{R → R_interface}/rmatrix.rb +0 -0
  77. data/lib/{R → R_interface}/rmd_indexed_object.rb +0 -0
  78. data/lib/{R → R_interface}/robject.rb +5 -0
  79. data/lib/{R → R_interface}/rpkg.rb +0 -0
  80. data/lib/{R → R_interface}/rsupport.rb +49 -13
  81. data/lib/{R → R_interface}/rsupport_scope.rb +0 -0
  82. data/lib/{R → R_interface}/rsymbol.rb +1 -0
  83. data/lib/{R → R_interface}/ruby_callback.rb +0 -0
  84. data/lib/{R → R_interface}/ruby_extensions.rb +2 -1
  85. data/lib/{R → R_interface}/runary_operators.rb +0 -0
  86. data/lib/{R → R_interface}/rvector.rb +0 -0
  87. data/lib/galaaz.rb +4 -2
  88. data/lib/gknit.rb +27 -0
  89. data/lib/gknit.rb~ +26 -0
  90. data/lib/gknit/knitr_engine.rb +120 -0
  91. data/lib/gknit/knitr_engine.rb~ +102 -0
  92. data/lib/gknit/ruby_engine.rb +70 -0
  93. data/lib/gknit/ruby_engine.rb~ +72 -0
  94. data/lib/util/exec_ruby.rb +8 -7
  95. data/lib/util/inline_file.rb +70 -0
  96. data/lib/util/inline_file.rb~ +23 -0
  97. data/r_requires/ggplot.rb +1 -8
  98. data/r_requires/knitr.rb +27 -0
  99. data/r_requires/knitr.rb~ +4 -0
  100. data/specs/r_language.spec.rb +22 -0
  101. data/specs/r_plots.spec.rb +72 -0
  102. data/specs/r_plots.spec.rb~ +37 -0
  103. data/specs/tmp.rb +255 -1
  104. data/version.rb +1 -1
  105. metadata +89 -39
Binary file
@@ -0,0 +1,745 @@
1
+ \documentclass[]{article}
2
+ \usepackage{lmodern}
3
+ \usepackage{amssymb,amsmath}
4
+ \usepackage{ifxetex,ifluatex}
5
+ \usepackage{fixltx2e} % provides \textsubscript
6
+ \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
7
+ \usepackage[T1]{fontenc}
8
+ \usepackage[utf8]{inputenc}
9
+ \else % if luatex or xelatex
10
+ \ifxetex
11
+ \usepackage{mathspec}
12
+ \else
13
+ \usepackage{fontspec}
14
+ \fi
15
+ \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
16
+ \fi
17
+ % use upquote if available, for straight quotes in verbatim environments
18
+ \IfFileExists{upquote.sty}{\usepackage{upquote}}{}
19
+ % use microtype if available
20
+ \IfFileExists{microtype.sty}{%
21
+ \usepackage{microtype}
22
+ \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
23
+ }{}
24
+ \usepackage[margin=1in]{geometry}
25
+ \usepackage{hyperref}
26
+ \hypersetup{unicode=true,
27
+ pdftitle={gKnit - Ruby and R Knitting with Galaaz in GraalVM},
28
+ pdfauthor={Rodrigo Botafogo},
29
+ pdfborder={0 0 0},
30
+ breaklinks=true}
31
+ \urlstyle{same} % don't use monospace font for urls
32
+ \usepackage{color}
33
+ \usepackage{fancyvrb}
34
+ \newcommand{\VerbBar}{|}
35
+ \newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
36
+ \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
37
+ % Add ',fontsize=\small' for more characters per line
38
+ \usepackage{framed}
39
+ \definecolor{shadecolor}{RGB}{248,248,248}
40
+ \newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
41
+ \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
42
+ \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
43
+ \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
44
+ \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
45
+ \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
46
+ \newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
47
+ \newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
48
+ \newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
49
+ \newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
50
+ \newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
51
+ \newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
52
+ \newcommand{\ImportTok}[1]{#1}
53
+ \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
54
+ \newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
55
+ \newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
56
+ \newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
57
+ \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
58
+ \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
59
+ \newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
60
+ \newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
61
+ \newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
62
+ \newcommand{\BuiltInTok}[1]{#1}
63
+ \newcommand{\ExtensionTok}[1]{#1}
64
+ \newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
65
+ \newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
66
+ \newcommand{\RegionMarkerTok}[1]{#1}
67
+ \newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
68
+ \newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
69
+ \newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
70
+ \newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
71
+ \newcommand{\NormalTok}[1]{#1}
72
+ \usepackage{graphicx,grffile}
73
+ \makeatletter
74
+ \def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
75
+ \def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
76
+ \makeatother
77
+ % Scale images if necessary, so that they will not overflow the page
78
+ % margins by default, and it is still possible to overwrite the defaults
79
+ % using explicit options in \includegraphics[width, height, ...]{}
80
+ \setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
81
+ \IfFileExists{parskip.sty}{%
82
+ \usepackage{parskip}
83
+ }{% else
84
+ \setlength{\parindent}{0pt}
85
+ \setlength{\parskip}{6pt plus 2pt minus 1pt}
86
+ }
87
+ \setlength{\emergencystretch}{3em} % prevent overfull lines
88
+ \providecommand{\tightlist}{%
89
+ \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
90
+ \setcounter{secnumdepth}{5}
91
+ % Redefines (sub)paragraphs to behave more like sections
92
+ \ifx\paragraph\undefined\else
93
+ \let\oldparagraph\paragraph
94
+ \renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
95
+ \fi
96
+ \ifx\subparagraph\undefined\else
97
+ \let\oldsubparagraph\subparagraph
98
+ \renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
99
+ \fi
100
+
101
+ %%% Use protect on footnotes to avoid problems with footnotes in titles
102
+ \let\rmarkdownfootnote\footnote%
103
+ \def\footnote{\protect\rmarkdownfootnote}
104
+
105
+ %%% Change title format to be more compact
106
+ \usepackage{titling}
107
+
108
+ % Create subtitle command for use in maketitle
109
+ \newcommand{\subtitle}[1]{
110
+ \posttitle{
111
+ \begin{center}\large#1\end{center}
112
+ }
113
+ }
114
+
115
+ \setlength{\droptitle}{-2em}
116
+
117
+ \title{gKnit - Ruby and R Knitting with Galaaz in GraalVM}
118
+ \pretitle{\vspace{\droptitle}\centering\huge}
119
+ \posttitle{\par}
120
+ \author{Rodrigo Botafogo}
121
+ \preauthor{\centering\large\emph}
122
+ \postauthor{\par}
123
+ \predate{\centering\large\emph}
124
+ \postdate{\par}
125
+ \date{19 October 2018}
126
+
127
+ % usar portugues do Brasil
128
+ % \usepackage[brazilian]{babel}
129
+ \usepackage[utf8]{inputenc}
130
+
131
+ \usepackage{geometry}
132
+ \geometry{a4paper, top=1in}
133
+
134
+ % necessários para uso com kableExtra
135
+ \usepackage{longtable}
136
+ \usepackage{multirow}
137
+ \usepackage[table]{xcolor}
138
+ \usepackage{wrapfig}
139
+ \usepackage{float}
140
+ \usepackage{colortbl}
141
+ \usepackage{pdflscape}
142
+ \usepackage{tabu}
143
+ \usepackage{threeparttable}
144
+ \usepackage[normalem]{ulem}
145
+
146
+ \usepackage{bbm}
147
+ \usepackage{booktabs}
148
+ \usepackage{expex}
149
+
150
+ \usepackage{graphicx}
151
+ \usepackage{fancyhdr}
152
+ \pagestyle{fancy}
153
+ \fancyhf{}
154
+
155
+ \usepackage{lipsum}
156
+
157
+ % disponibilizar o comando lastpage
158
+ \usepackage{lastpage}
159
+
160
+ % tamanho do font padrão 11pt
161
+ \usepackage[fontsize=10pt]{scrextend}
162
+
163
+ % comandos para formatar uma tabela
164
+ \usepackage{array}
165
+ \newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
166
+ \newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
167
+ \newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
168
+
169
+ % necessário para importar outros arquivos latex
170
+ \usepackage{import}
171
+
172
+ \newcommand{\RtoLatex}[2]{\newcommand{#1}{#2}}
173
+ %\newcommand{\atraso}[1]{\color{red} \textbf {Tempo desde a Assinatura do Contrato: #1 dias}}
174
+
175
+ \begin{document}
176
+ \maketitle
177
+
178
+ \section{Introduction}\label{introduction}
179
+
180
+ The idea of ``literate programming'' was first introduced by Donald
181
+ Knuth in the 1980's. The main intention of this approach was to develop
182
+ software interspersing macro snippets, traditional source code, and a
183
+ natural language such as English that could be compiled into executable
184
+ code and at the same time easily read by a human developer. According to
185
+ Knuth ``The practitioner of literate programming can be regarded as an
186
+ essayist, whose main concern is with exposition and excellence of
187
+ style.''
188
+
189
+ The idea of literate programming envolved into the idea of reproducible
190
+ research, in which all the data, software code, documentation, graphics
191
+ etc. needed to reproduce the research and its reports could be included
192
+ in a single document or set of documents that when distributed to peers
193
+ could be rerun generating the same output and reports.
194
+
195
+ The R community has put a great deal of effort in reproducible research.
196
+ In 2002, Sweave was introduced and it allowed mixing R code with Latex
197
+ generating hight quality PDF documents. Those documents could include
198
+ the code, the result of executing the code, graphics and text. This
199
+ contained the whole narrative to reproduce the research. But Sweave had
200
+ many problems and in 2012, Knitr, developed by Yihui Xie from RStudio
201
+ was released, solving many of the long lasting problems from Sweave and
202
+ including in one single package many extensions and add-on packages that
203
+ were necessary for Sweave.
204
+
205
+ With Knitr, R markdown was also developed, an extension the the Markdown
206
+ format. With R markdown and Knitr it is possible to generate reports in
207
+ a multitude of formats such as HTML, markdown, Latex, PDF, dvi, etc. R
208
+ markdown also allows the use of multiple programming languages in the
209
+ same document. In R markdown text is interspersed with code chunks that
210
+ can be executed and both the code as the result of executing the code
211
+ can become part of the final report. Although R markdown allows multiple
212
+ programming languages in the same document, only R and Python (with the
213
+ reticulate package) can persist variables between chunks. For other
214
+ languages, such as Ruby, every chunk will start a new process and thus
215
+ all data is lost between chunks, unless it is somehow stored in a data
216
+ file that is read by the next chunk.
217
+
218
+ Being able to persist data between chunks is critical for literate
219
+ programming otherwise the flow of the narrative is lost by all the
220
+ effort of having to save data and then reload it. Probably, because of
221
+ this impossibility, it is very rare to see any R markdown document
222
+ document in the Ruby community.
223
+
224
+ In the Python community, the same effort to have code and text in an
225
+ integrated environment started also on the first decade of 2000. In 2006
226
+ iPython 0.7.2 was released. In 2014, Fernando Pérez, spun off project
227
+ Jupyter from iPython creating a web-based interactive computation
228
+ environment. Jupyter can now be used with many languages, including Ruby
229
+ with the iruby gem (\url{https://github.com/SciRuby/iruby}). I am not
230
+ sure if multiple languages can be used in a Jupyter notebook.
231
+
232
+ \section{gKnitting a Document}\label{gknitting-a-document}
233
+
234
+ This document describes gKnit. gKnit uses Knitr and R markdown to knit a
235
+ document in Ruby or R and output it in any of the available formats for
236
+ R markdown. The only difference between gKnit and normal Knitr documents
237
+ is that gKnit runs atop of GraalVM, and Galaaz (an integration library
238
+ between Ruby and R). Another blog post on Galaaz and its integration
239
+ with ggplot2 can be found at:
240
+ \url{https://towardsdatascience.com/ruby-plotting-with-galaaz-an-example-of-tightly-coupling-ruby-and-r-in-graalvm-520b69e21021}.
241
+ With Galaaz, gKnit can knit documents in Ruby and R and both Ruby and R
242
+ execute on the same process and memory, variables, classes, etc. will be
243
+ preserved between chunks of code.
244
+
245
+ This is not a blog post on rmarkdown, and the interested user is
246
+ directed to
247
+
248
+ \begin{itemize}
249
+ \tightlist
250
+ \item
251
+ \url{https://rmarkdown.rstudio.com/} or
252
+ \item
253
+ \url{https://bookdown.org/yihui/rmarkdown/} for detailed information
254
+ on its capabilities and use.
255
+ \end{itemize}
256
+
257
+ Here, we will describe quickly the main aspects of R markdown, so the
258
+ user can start gKnitting Ruby and R documents quickly.
259
+
260
+ \subsection{The Yaml header}\label{the-yaml-header}
261
+
262
+ An R markdown document should start with a Yaml header and be stored in
263
+ a file with `.Rmd' extension. This document has the following header for
264
+ gKitting an HTML document.
265
+
266
+ \begin{verbatim}
267
+ ---
268
+ title: "gKnit - Ruby and R Knitting with Galaaz in GraalVM"
269
+ author: "Rodrigo Botafogo"
270
+ tags: [Galaaz, Ruby, R, TruffleRuby, FastR, GraalVM, knitr, gknit]
271
+ date: "29 October 2018"
272
+ output:
273
+ html_document:
274
+ keep_md: true
275
+ ---
276
+ \end{verbatim}
277
+
278
+ For more information on the options in the Yaml header, check
279
+ \url{https://bookdown.org/yihui/rmarkdown/html-document.html}.
280
+
281
+ \subsection{R Markdown formatting}\label{r-markdown-formatting}
282
+
283
+ Document formating can be done with simple markups such as:
284
+
285
+ \subsubsection{Headers}\label{headers}
286
+
287
+ \begin{verbatim}
288
+ # Header 1
289
+
290
+ ## Header 2
291
+
292
+ ### Header 3
293
+ \end{verbatim}
294
+
295
+ \subsubsection{Lists}\label{lists}
296
+
297
+ \begin{verbatim}
298
+ Unordered lists:
299
+
300
+ * Item 1
301
+ * Item 2
302
+ + Item 2a
303
+ + Item 2b
304
+ \end{verbatim}
305
+
306
+ \begin{verbatim}
307
+ Ordered Lists
308
+
309
+ 1. Item 1
310
+ 2. Item 2
311
+ 3. Item 3
312
+ + Item 3a
313
+ + Item 3b
314
+ \end{verbatim}
315
+
316
+ Please, go to \url{https://rmarkdown.rstudio.com/authoring_basics.html},
317
+ for more R markdown formating.
318
+
319
+ \subsection{Code Chunks}\label{code-chunks}
320
+
321
+ Running and executing Ruby and R code is actually what really interests
322
+ us is this blog. Inserting a code chunk is done by adding code in a
323
+ block delimited by three back ticks followed by a block with the engine
324
+ name (r, ruby, rb, include, others), an optional chunk\_label and
325
+ optional options, as shown bellow:
326
+
327
+ \begin{verbatim}
328
+ ```{engine_name [chunk_label], [chunk_options]}
329
+ ```
330
+ \end{verbatim}
331
+
332
+ for instance, let's add an R chunk to the document labeled
333
+ `first\_r\_chunk'. In this case, the code should not be shown in the
334
+ document, so the option `echo=FALSE' was added.
335
+
336
+ \begin{verbatim}
337
+ ```{r first_r_chunk, echo = FALSE}
338
+ ```
339
+ \end{verbatim}
340
+
341
+ A description of the available chunk options can be found in the
342
+ documentation cited above.
343
+
344
+ For including a Ruby chunk, just change the name of the engine to ruby
345
+ as follows:
346
+
347
+ \begin{verbatim}
348
+ ```{ruby first_ruby_chunk}
349
+ ```
350
+ \end{verbatim}
351
+
352
+ In this example, the ruby chunk is called `first\_ruby\_chunk'. One
353
+ important aspect of chunk labels is that they cannot be duplicate. If a
354
+ chunk label is duplicate, the knitting will stop with an error.
355
+
356
+ \subsubsection{R chunks}\label{r-chunks}
357
+
358
+ Let's now add an R chunk to this document. In this example, a vector
359
+ `r\_vec' is created and a new function `redundat\_sum' is defined. The
360
+ chunk specification is
361
+
362
+ \begin{verbatim}
363
+ ```{r data_creation}
364
+ r_vec <- c(1, 2, 3, 4, 5)
365
+
366
+ redef_sum <- function(...) {
367
+ Reduce(sum, as.list(...))
368
+ }
369
+ ```
370
+ \end{verbatim}
371
+
372
+ and this is how it will look like once executed. From now on, we will
373
+ not show the chunk definition any longer.
374
+
375
+ \begin{Shaded}
376
+ \begin{Highlighting}[]
377
+ \NormalTok{r_vec <-}\StringTok{ }\KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{)}
378
+
379
+ \NormalTok{redef_sum <-}\StringTok{ }\ControlFlowTok{function}\NormalTok{(...) \{}
380
+ \KeywordTok{Reduce}\NormalTok{(sum, }\KeywordTok{as.list}\NormalTok{(...))}
381
+ \NormalTok{\}}
382
+ \end{Highlighting}
383
+ \end{Shaded}
384
+
385
+ We can, possibly in another chunk, access the vector and call the
386
+ function as follows:
387
+
388
+ \begin{Shaded}
389
+ \begin{Highlighting}[]
390
+ \KeywordTok{print}\NormalTok{(r_vec)}
391
+ \end{Highlighting}
392
+ \end{Shaded}
393
+
394
+ \begin{verbatim}
395
+ ## [1] 1 2 3 4 5
396
+ \end{verbatim}
397
+
398
+ \begin{Shaded}
399
+ \begin{Highlighting}[]
400
+ \KeywordTok{print}\NormalTok{(}\KeywordTok{redef_sum}\NormalTok{(r_vec))}
401
+ \end{Highlighting}
402
+ \end{Shaded}
403
+
404
+ \begin{verbatim}
405
+ ## [1] 15
406
+ \end{verbatim}
407
+
408
+ \subsubsection{Ruby chunks}\label{ruby-chunks}
409
+
410
+ In the same way that an R chunk was created, let's now create a Ruby
411
+ chunk. One important aspect of Ruby is that in Ruby every evaluation of
412
+ a chunk occurs on its own local scope, so, creating a variable in a
413
+ chunk will be out of scope in the next chunk. To make sure that
414
+ variables are available between chunks, they should be made global.
415
+
416
+ In this chunk, variable `\$a', `\$b' and `\$c' are standard Ruby
417
+ variables and `\$vec' and `\$vec2' are two vectors created by a call to
418
+ FastR. It should be clear that there is no requirement in gknit to call
419
+ or use R functions. gKnit will knit standard Ruby code, or even general
420
+ text without code.
421
+
422
+ \begin{Shaded}
423
+ \begin{Highlighting}[]
424
+ \DataTypeTok{$a}\NormalTok{ = [}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{]}
425
+ \DataTypeTok{$b}\NormalTok{ = }\StringTok{"US$ 250.000"}
426
+ \DataTypeTok{$c}\NormalTok{ = }\StringTok{"Inline text in a Heading"}
427
+
428
+ \DataTypeTok{$vec}\NormalTok{ = R.c(}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{)}
429
+ \DataTypeTok{$vec2}\NormalTok{ = R.c(}\DecValTok{10}\NormalTok{, }\DecValTok{20}\NormalTok{, }\DecValTok{30}\NormalTok{)}
430
+ \end{Highlighting}
431
+ \end{Shaded}
432
+
433
+ In this next block, variables `\$a', `\$vec' and `\$vec2' are used and
434
+ printed.
435
+
436
+ \begin{Shaded}
437
+ \begin{Highlighting}[]
438
+ \NormalTok{puts }\DataTypeTok{$a}
439
+ \NormalTok{puts }\DataTypeTok{$vec}\NormalTok{ * }\DataTypeTok{$vec2}
440
+ \end{Highlighting}
441
+ \end{Shaded}
442
+
443
+ \begin{verbatim}
444
+ ## 1
445
+ ## 2
446
+ ## 3
447
+ ## [1] 10 40 90
448
+ \end{verbatim}
449
+
450
+ \subsubsection{Accessing R from Ruby}\label{accessing-r-from-ruby}
451
+
452
+ One of the nice aspects of Galaaz on GraalVM, is that variables and
453
+ functions defined in R, can be easily accessed from Ruby. This next
454
+ chunk, reads data from R and uses the `redef\_fun' function defined
455
+ previously. To access an R variable from Ruby the `\textasciitilde{}'
456
+ function shoud be applied to the Ruby symbol representing the R
457
+ variable. Since the R variable is called `r\_vec', in Ruby, the symbol
458
+ to acess it is `:r\_vec' and thus `\textasciitilde{}:r\_vec' retrieves
459
+ the value of the variable.
460
+
461
+ \begin{Shaded}
462
+ \begin{Highlighting}[]
463
+ \NormalTok{puts ~}\StringTok{:r_vec}
464
+ \end{Highlighting}
465
+ \end{Shaded}
466
+
467
+ \begin{verbatim}
468
+ ## [1] 1 2 3 4 5
469
+ \end{verbatim}
470
+
471
+ In order to call an R function, the `R.' module is used as follows
472
+
473
+ \begin{Shaded}
474
+ \begin{Highlighting}[]
475
+ \NormalTok{puts R.redef_sum(}\DataTypeTok{$vec}\NormalTok{)}
476
+ \end{Highlighting}
477
+ \end{Shaded}
478
+
479
+ \begin{verbatim}
480
+ ## [1] 6
481
+ \end{verbatim}
482
+
483
+ \subsubsection{Inline Ruby code}\label{inline-ruby-code}
484
+
485
+ Knitr allows inserting R inline by adding `r code` . Unfortunately, this
486
+ is not possible with Ruby code as there is no provision in knitr for
487
+ adding this kind of inline engine. However, gKnit allows adding inline
488
+ Ruby code with the `rb' engine. The following text will create and
489
+ inline Ruby text:
490
+
491
+ \begin{verbatim}
492
+ This is some text with inline Ruby accessing variable \$b which has value:
493
+ ```{rb puts $b}
494
+ ```
495
+ and is followed by some other text!
496
+ \end{verbatim}
497
+
498
+ The result of executing the above ckunk is the following sentence with
499
+ inline Ruby code
500
+
501
+ This is some text with inline Ruby accessing variable \$b which has
502
+ value: US\$ 250.000 and is followed by some other text!
503
+
504
+ In an inline block, it is possible to execute multiple Ruby statements
505
+ by adding a semicolom between them:
506
+
507
+ \begin{verbatim}
508
+ Multiple statements in the 'rb' engine use semicolom:
509
+ ```{rb puts $a, puts $b}
510
+ ```
511
+ \end{verbatim}
512
+
513
+ Multiple statements in the `rb' engine use semicolom: 1 2 3 US\$ 250.000
514
+
515
+ \subsubsection{Inline text in a Heading}\label{inline-text-in-a-heading}
516
+
517
+ Sometimes one wants to add an inline text in a heading. To do that in
518
+ Ruby the whole heading needs to be returned by the inline Ruby engine.
519
+ For example the heading above, was created by the following chunk:
520
+
521
+ \begin{verbatim}
522
+ ```{rb puts "### #{$c}"}
523
+ ```
524
+ \end{verbatim}
525
+
526
+ Remember that variable `\$\c' was defined in a previous Ruby chunk and
527
+ is now being used to create the section heading for this section.
528
+
529
+ \subsubsection{Including Ruby files}\label{including-ruby-files}
530
+
531
+ R is a language that was created to be easy and fast for statisticians
532
+ to use. It was not a language to be used for developing large systems.
533
+ Of course, there are large systems and libraries in R, but the focus of
534
+ the language is for developing statistical models and distribute that to
535
+ peers.
536
+
537
+ Ruby on the other hand, is a language for large software development.
538
+ Systems written in Ruby will have dozens or hundreds of files. In order
539
+ to document a large system with literate programming we cannot expect
540
+ the developer to add all the files in a single `.Rmd' file. gKnit
541
+ provides the `include' chunk engine to include a Ruby file as if it had
542
+ being typed in the `.Rmd' file.
543
+
544
+ To include a file the following chunk should be created, whre is the
545
+ name of the file to be include and where the extension, if it is `.rb',
546
+ does not need to be added. If the `relative' option is not included,
547
+ then it is treated as TRUE. When `relative' is true, `require\_relative'
548
+ semantics is used to load the file, when false, Ruby's \$LOAD\_PATH is
549
+ searched to find the file and it is 'require'd.
550
+
551
+ \begin{verbatim}
552
+ ```{include <filename>, relative = <TRUE/FALSE>}
553
+ ```
554
+ \end{verbatim}
555
+
556
+ Here we include file `model.rb' which is in the same directory of this
557
+ blog. This code uses R `caret' package to split a dataset in a train and
558
+ test sets.
559
+
560
+ \begin{verbatim}
561
+ ```{include model}
562
+ ```
563
+ \end{verbatim}
564
+
565
+ \begin{verbatim}
566
+ require 'galaaz'
567
+
568
+ # Loads the R 'caret' package. If not present, installs it
569
+ R.install_and_loads 'caret'
570
+
571
+ class Model
572
+
573
+ attr_reader :data
574
+ attr_reader :test
575
+ attr_reader :train
576
+
577
+ #==========================================================
578
+ #
579
+ #==========================================================
580
+
581
+ def initialize(data, percent_train:, seed: 123)
582
+
583
+ R.set__seed(seed)
584
+ @data = data
585
+ @percent_train = percent_train
586
+ @seed = seed
587
+
588
+ end
589
+
590
+ #==========================================================
591
+ #
592
+ #==========================================================
593
+
594
+ def partition(field)
595
+
596
+ train_index =
597
+ R.createDataPartition(@data.send(field), p: @percet_train,
598
+ list: false, times: 1)
599
+ @train = @data[train_index, :all]
600
+ @test = @data[-train_index, :all]
601
+
602
+ end
603
+
604
+ end
605
+ \end{verbatim}
606
+
607
+ \begin{Shaded}
608
+ \begin{Highlighting}[]
609
+ \NormalTok{mtcars = ~}\StringTok{:mtcars}
610
+ \NormalTok{model = }\DataTypeTok{Model}\NormalTok{.new(mtcars, }\StringTok{percent_train: }\FloatTok{0.8}\NormalTok{)}
611
+ \NormalTok{model.partition(}\StringTok{:mpg}\NormalTok{)}
612
+ \NormalTok{puts model.train.head}
613
+ \NormalTok{puts model.test.head}
614
+ \end{Highlighting}
615
+ \end{Shaded}
616
+
617
+ \begin{verbatim}
618
+ ## mpg cyl disp hp drat wt qsec vs am gear carb
619
+ ## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
620
+ ## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
621
+ ## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
622
+ ## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
623
+ ## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
624
+ ## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
625
+ ## mpg cyl disp hp drat wt qsec vs am gear carb
626
+ ## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
627
+ ## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
628
+ ## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
629
+ ## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
630
+ ## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
631
+ ## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
632
+ \end{verbatim}
633
+
634
+ \subsubsection{Documenting Gems}\label{documenting-gems}
635
+
636
+ \begin{verbatim}
637
+ # Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. This
638
+ # code is released under a tri EPL/GPL/LGPL license. You can use it,
639
+ # redistribute it and/or modify it under the terms of the:
640
+ #
641
+ # Eclipse Public License version 1.0, or
642
+ # GNU General Public License version 2, or
643
+ # GNU Lesser General Public License version 2.1.
644
+
645
+ warn "#{File.basename(__FILE__)}: warning: callcc is obsolete; use Fiber instead"
646
+
647
+ class Continuation
648
+ def initialize
649
+ @fiber = Fiber.current
650
+ end
651
+
652
+ def call
653
+ if Fiber.current != @fiber
654
+ raise 'continuation called across fiber'
655
+ end
656
+ raise 'Continuations are unsupported on TruffleRuby'
657
+ end
658
+ end
659
+
660
+ module Kernel
661
+ def callcc
662
+ yield Continuation.new
663
+ end
664
+ module_function :callcc
665
+ end
666
+ \end{verbatim}
667
+
668
+ \subsection{Converting to PDF}\label{converting-to-pdf}
669
+
670
+ One of the beauties of knitr is that the same input can be converted to
671
+ many different outputs. One very useful format, is, of course, PDF. In
672
+ order to converted an R markdown file to PDF it is necessary to have
673
+ LaTeX installed on the system. We will not explain here how to install
674
+ LaTeX as there are plenty of documents on the web showing how to
675
+ proceed.
676
+
677
+ gKnit comes with a simple LaTeX style file for gknitting this blog as a
678
+ PDF document. Here is the Yaml header to generate this blog in PDF
679
+ format instead of HTML:
680
+
681
+ \begin{verbatim}
682
+ ---
683
+ title: "gKnit - Ruby and R Knitting with Galaaz in GraalVM"
684
+ author: "Rodrigo Botafogo"
685
+ tags: [Galaaz, Ruby, R, TruffleRuby, FastR, GraalVM, knitr, gknit]
686
+ date: "29 October 2018"
687
+ output:
688
+ pdf_document:
689
+ includes:
690
+ in_header: ["../../sty/galaaz.sty"]
691
+ number_sections: yes
692
+ ---
693
+ \end{verbatim}
694
+
695
+ \section{Conclusion}\label{conclusion}
696
+
697
+ \section{Installing gKnit}\label{installing-gknit}
698
+
699
+ \subsection{Prerequisites}\label{prerequisites}
700
+
701
+ \begin{itemize}
702
+ \tightlist
703
+ \item
704
+ GraalVM (\textgreater{}= rc7)
705
+ \item
706
+ TruffleRuby
707
+ \item
708
+ FastR
709
+ \end{itemize}
710
+
711
+ The following R packages will be automatically installed when necessary,
712
+ but could be installed prior to using gKnit if desired:
713
+
714
+ \begin{itemize}
715
+ \tightlist
716
+ \item
717
+ ggplot2
718
+ \item
719
+ gridExtra
720
+ \item
721
+ knitr
722
+ \end{itemize}
723
+
724
+ Installation of R packages requires a development environment and can be
725
+ time consuming. In Linux, the gnu compiler and tools should be enough. I
726
+ am not sure what is needed on the Mac.
727
+
728
+ \subsection{Preparation}\label{preparation}
729
+
730
+ \begin{itemize}
731
+ \tightlist
732
+ \item
733
+ gem install galaaz
734
+ \end{itemize}
735
+
736
+ \subsection{Usage}\label{usage}
737
+
738
+ \begin{itemize}
739
+ \tightlist
740
+ \item
741
+ gknit {[}filename{]}
742
+ \end{itemize}
743
+
744
+
745
+ \end{document}