galaaz 0.4.10 → 0.5.0

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