galaaz 2.1.7 → 2.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +39 -0
  3. data/Rakefile +20 -2
  4. data/bin/check_gemfile_lock_version +46 -0
  5. data/bin/release_bump +26 -0
  6. data/blogs/README.md +4 -0
  7. data/blogs/galaaz_2_0/galaaz_2_0.Rmd +385 -0
  8. data/blogs/galaaz_2_0/galaaz_2_0.md +409 -0
  9. data/blogs/galaaz_2_0/galaaz_2_0.tex +756 -0
  10. data/blogs/galaaz_2_0/images/galaaz-header.png +0 -0
  11. data/blogs/galaaz_2_0/images/galaaz-lockup-stacked.png +0 -0
  12. data/blogs/galaaz_ggplot/galaaz_ggplot.Rmd +76 -58
  13. data/blogs/galaaz_ggplot/galaaz_ggplot.md +188 -161
  14. data/blogs/galaaz_ggplot/galaaz_ggplot.tex +185 -117
  15. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/midwest_rb.png +0 -0
  16. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/scatter_plot_rb.png +0 -0
  17. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-markdown_github/midwest_rb.png +0 -0
  18. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-markdown_github/scatter_plot_rb.png +0 -0
  19. data/blogs/galaaz_ggplot/images/galaaz-lockup-stacked.png +0 -0
  20. data/blogs/gknit/gknit.Rmd +48 -28
  21. data/blogs/gknit/gknit.md +60 -43
  22. data/blogs/gknit/gknit.tex +1409 -0
  23. data/blogs/gknit/gknit_files/figure-html/bubble-1.png +0 -0
  24. data/blogs/gknit/gknit_files/figure-html/diverging_bar.png +0 -0
  25. data/blogs/gknit/gknit_files/figure-latex/bubble-1.png +0 -0
  26. data/blogs/gknit/images/galaaz-lockup-stacked.png +0 -0
  27. data/blogs/manual/images/galaaz-lockup-stacked.png +0 -0
  28. data/blogs/manual/manual.Rmd +161 -71
  29. data/blogs/manual/manual.md +573 -482
  30. data/blogs/manual/manual.tex +1113 -517
  31. data/blogs/manual/manual_files/figure-html/bubble-1.png +0 -0
  32. data/blogs/manual/manual_files/figure-latex/bubble-1.png +0 -0
  33. data/blogs/manual/manual_files/figure-markdown_github/bubble-1.png +0 -0
  34. data/blogs/manual/manual_files/figure-markdown_github/diverging_bar.png +0 -0
  35. data/blogs/nse_dplyr/images/galaaz-lockup-stacked.png +0 -0
  36. data/blogs/nse_dplyr/nse_dplyr.Rmd +42 -8
  37. data/blogs/nse_dplyr/nse_dplyr.md +69 -50
  38. data/blogs/nse_dplyr/nse_dplyr.tex +1626 -0
  39. data/blogs/oh_my/images/galaaz-lockup-stacked.png +0 -0
  40. data/blogs/oh_my/oh_my.Rmd +206 -55
  41. data/blogs/oh_my/oh_my.md +247 -96
  42. data/blogs/oh_my/oh_my.tex +2038 -93
  43. data/blogs/r_on_rails_ledger/images/00_portfolio_page.png +0 -0
  44. data/blogs/r_on_rails_ledger/images/01_results_panel.png +0 -0
  45. data/blogs/r_on_rails_ledger/images/02_density_tail_risk.png +0 -0
  46. data/blogs/r_on_rails_ledger/images/03_mc_cone.png +0 -0
  47. data/blogs/r_on_rails_ledger/images/04_rolling_var.png +0 -0
  48. data/blogs/r_on_rails_ledger/images/galaaz-lockup-stacked.png +0 -0
  49. data/blogs/r_on_rails_ledger/r_on_rails_ledger.Rmd +354 -0
  50. data/blogs/r_on_rails_ledger/r_on_rails_ledger.md +365 -0
  51. data/blogs/r_on_rails_ledger/r_on_rails_ledger.tex +670 -0
  52. data/blogs/ruby_plot/images/galaaz-lockup-stacked.png +0 -0
  53. data/blogs/ruby_plot/ruby_plot.Rmd +56 -35
  54. data/blogs/ruby_plot/ruby_plot.md +63 -47
  55. data/blogs/ruby_plot/ruby_plot.tex +194 -125
  56. data/blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.png +0 -0
  57. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_delivery.png +0 -0
  58. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_dose.png +0 -0
  59. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color.png +0 -0
  60. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color2.png +0 -0
  61. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_decorations.png +0 -0
  62. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_jitter.png +0 -0
  63. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_points.png +0 -0
  64. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_box_plot.png +0 -0
  65. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_violin_plot.png +0 -0
  66. data/blogs/ruby_plot/ruby_plot_files/figure-html/violin_with_jitter.png +0 -0
  67. data/blogs/ruby_plot/ruby_plot_files/figure-latex/dose_len.png +0 -0
  68. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_delivery.png +0 -0
  69. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_dose.png +0 -0
  70. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color.png +0 -0
  71. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color2.png +0 -0
  72. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_decorations.png +0 -0
  73. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_jitter.png +0 -0
  74. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_points.png +0 -0
  75. data/blogs/ruby_plot/ruby_plot_files/figure-latex/final_box_plot.png +0 -0
  76. data/blogs/ruby_plot/ruby_plot_files/figure-latex/final_violin_plot.png +0 -0
  77. data/blogs/ruby_plot/ruby_plot_files/figure-latex/violin_with_jitter.png +0 -0
  78. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/dose_len.png +0 -0
  79. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facet_by_delivery.png +0 -0
  80. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facet_by_dose.png +0 -0
  81. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_by_delivery_color.png +0 -0
  82. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_by_delivery_color2.png +0 -0
  83. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_with_decorations.png +0 -0
  84. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_with_jitter.png +0 -0
  85. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_with_points.png +0 -0
  86. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/final_box_plot.png +0 -0
  87. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/final_violin_plot.png +0 -0
  88. data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/violin_with_jitter.png +0 -0
  89. data/lib/galaaz/cli.rb +98 -13
  90. data/logos/icon-font/README.md +27 -0
  91. data/logos/icon-font/build_font.py +130 -0
  92. data/logos/icon-font/galaaz-mark.svg +34 -0
  93. data/script/omarchy/README.md +9 -2
  94. data/script/omarchy/fonts/galaaz.ttf +0 -0
  95. data/script/omarchy/galaaz-guide.sh +1 -1
  96. data/script/omarchy/install-galaaz.sh +8 -1
  97. data/script/omarchy/omarchy-menu.jsonc +21 -9
  98. data/sty/galaaz-header.png +0 -0
  99. data/sty/galaaz-headers-from-p3.tex +4 -0
  100. data/sty/galaaz.sty +76 -23
  101. data/version.rb +1 -1
  102. metadata +46 -7
  103. data/blogs/galaaz_ggplot/galaaz_ggplot.log +0 -754
  104. data/blogs/manual/manual.log +0 -1786
  105. data/blogs/nse_dplyr/nse_dplyr.log +0 -928
  106. data/blogs/oh_my/oh_my.log +0 -804
  107. data/blogs/ruby_plot/ruby_plot.log +0 -885
@@ -145,7 +145,7 @@
145
145
  \usepackage[utf8]{inputenc}
146
146
 
147
147
  \usepackage{geometry}
148
- \geometry{a4paper, top=1in}
148
+ \geometry{a4paper, top=1.05in, headheight=30pt, headsep=14pt}
149
149
 
150
150
  % needed for kableExtra
151
151
  \usepackage{longtable}
@@ -166,29 +166,60 @@
166
166
  \usepackage{graphicx}
167
167
 
168
168
  \usepackage{fancyhdr}
169
- % set the header and foot style
170
- % style 'fancy' adds the section name on the header
171
- % and the page number on the footer
169
+ % Header: small Galaaz mark (left); page number + section title (right),
170
+ % matching classic LaTeX style: "4 AN EXTENSION TO THE EXAMPLE"
171
+ % (upright page number, uppercase italic section name).
172
+ % Logo paths cover repo blogs (../../sty/), installed ~/sty/, and local images/.
173
+ % Blogs may delay headers with \renewcommand{\galaazheaderfrompage}{3} after this sty
174
+ % (e.g. skip title/TOC and a logo splash page).
175
+ \newcommand{\galaazheaderfrompage}{1}
176
+ \newcommand{\galaazheaderlogo}{%
177
+ \raisebox{-0.12\height}{%
178
+ \IfFileExists{../../sty/galaaz-header.png}{%
179
+ \includegraphics[height=0.32in]{../../sty/galaaz-header.png}%
180
+ }{%
181
+ \IfFileExists{galaaz-header.png}{%
182
+ \includegraphics[height=0.32in]{galaaz-header.png}%
183
+ }{%
184
+ \IfFileExists{images/galaaz-header.png}{%
185
+ \includegraphics[height=0.32in]{images/galaaz-header.png}%
186
+ }{}%
187
+ }%
188
+ }%
189
+ }%
190
+ }
191
+ % Section title only in marks (no \thesection); page number is separate.
192
+ \renewcommand{\sectionmark}[1]{\markboth{#1}{}}
193
+ \renewcommand{\subsectionmark}[1]{}% keep the current section in the header
194
+ \newcommand{\galaazmaybeheaderlogo}{%
195
+ \ifnum\value{page}<\galaazheaderfrompage\relax\else\galaazheaderlogo\fi}
196
+ \newcommand{\galaazmayberighthead}{%
197
+ \ifnum\value{page}<\galaazheaderfrompage\relax
198
+ \else
199
+ \textup{\thepage}\hspace{1em}\textit{\MakeUppercase{\leftmark}}%
200
+ \fi
201
+ }
172
202
  \pagestyle{fancy}
173
-
174
- % style 'fancyhf' leaves header and footer empty
175
- %\fancyhf{}
176
-
177
- % sets the left head element to \rightmark, which contains the
178
- % current section (\leftmark is the current chapter)
179
- %\fancyhead[L]{\rightmark} .
180
-
181
- % sets the right head element to the page number.
182
- % \fancyhead[R]{\thepage}
183
-
184
- % lets the head rule disappear.
185
- % \renewcommand{\headrulewidth}{0pt}
186
- % Possible selectors for the optional argument of \fancyhead/\fancyfoot
187
- % are L (left), C (center) or R (right) for the position of the element
188
- % and E (even) or O (odd) to distinguish even and odd pages. If you omit
189
- % E/O the element is set for all pages.
190
-
191
- % \usepackage{lipsum}
203
+ \fancyhf{}
204
+ \fancyhead[L]{\galaazmaybeheaderlogo}
205
+ \fancyhead[R]{\galaazmayberighthead}
206
+ \renewcommand{\headrulewidth}{0.3pt}
207
+ \makeatletter
208
+ \renewcommand{\headrule}{%
209
+ \ifnum\value{page}<\galaazheaderfrompage\relax
210
+ \else
211
+ \hrule\@height\headrulewidth\@width\headwidth
212
+ \vskip-\headrulewidth
213
+ \fi
214
+ }
215
+ \makeatother
216
+ % First page often uses plain; same conditional header logic.
217
+ \fancypagestyle{plain}{%
218
+ \fancyhf{}%
219
+ \fancyhead[L]{\galaazmaybeheaderlogo}%
220
+ \fancyhead[R]{\galaazmayberighthead}%
221
+ \renewcommand{\headrulewidth}{0.3pt}%
222
+ }
192
223
 
193
224
  % make available command lastpage
194
225
  \usepackage{lastpage}
@@ -209,8 +240,34 @@
209
240
  % Command to import an R variable to latex
210
241
  \newcommand{\RtoLatex}[2]{\newcommand{#1}{#2}}
211
242
 
243
+ % Soft-wrap Pandoc highlighted code/output boxes (Shaded + Highlighting).
244
+ % This runs after Pandoc's default \DefineVerbatimEnvironment{Highlighting}
245
+ % (header includes come later in the generated .tex). Prefer manual line
246
+ % breaks in Rmd sources; breaklines is a safety net for leftovers/output.
247
+ % Requires TinyTeX/TeX Live package: tlmgr install fvextra
248
+ \IfFileExists{fvextra.sty}{%
249
+ \usepackage{fvextra}%
250
+ \DefineVerbatimEnvironment{Highlighting}{Verbatim}{%
251
+ breaklines=true,
252
+ breakanywhere=true,
253
+ breakindent=1.5em,
254
+ fontsize=\small,
255
+ commandchars=\\\{\}%
256
+ }%
257
+ }{%
258
+ % fancyvrb is already loaded by Pandoc; shrink code so more fits per line.
259
+ \DefineVerbatimEnvironment{Highlighting}{Verbatim}{%
260
+ fontsize=\small,
261
+ commandchars=\\\{\}%
262
+ }%
263
+ }
264
+
212
265
  %
213
266
  %\newcommand{\atraso}[1]{\color{red} \textbf {Tempo desde a Assinatura do Contrato: #1 dias}}
267
+ % No header on title/TOC (p.1) or splash logo page (p.2);
268
+ % logo + page number + section title from page 3 onward.
269
+ % Load after galaaz.sty.
270
+ \renewcommand{\galaazheaderfrompage}{3}
214
271
  \usepackage{booktabs}
215
272
  \usepackage{longtable}
216
273
  \usepackage{array}
@@ -241,7 +298,7 @@
241
298
  \apptocmd{\@title}{\par {\large #1 \par}}{}{}
242
299
  }
243
300
  \makeatother
244
- \subtitle{Coupling Ruby (JRuby) and GNU R for data science}
301
+ \subtitle{R-on-Rails: GNU R meets Ruby for the web}
245
302
  \author{Rodrigo Botafogo}
246
303
  \date{2026}
247
304
 
@@ -252,6 +309,8 @@
252
309
  \setcounter{tocdepth}{3}
253
310
  \tableofcontents
254
311
  }
312
+ \begin{center}\includegraphics[width=0.45\linewidth]{images/galaaz-lockup-stacked} \end{center}
313
+
255
314
  \section{Introduction}\label{introduction}
256
315
 
257
316
  Galaaz is a system for tightly coupling Ruby and R. Ruby is a powerful
@@ -295,22 +354,26 @@ Until recently, there was no real perspective for Ruby to bridge this
295
354
  gap. Implementing a complete scientific computing infrastructure would
296
355
  take too long.
297
356
 
298
- \textbf{Galaaz 2.0} couples \textbf{JRuby} (Ruby on the JVM) with
299
- \textbf{GNU R}---the same R you use for CRAN and Bioconductor. Ruby and
300
- R run in \textbf{separate processes}; the \textbf{Galaaz bridge} sends
301
- requests to R and returns results to Ruby. From your point of view you
302
- still write Ruby: \texttt{R.c(...)},
357
+ \textbf{Galaaz 2.0} supports \textbf{JRuby} and \textbf{CRuby} equally
358
+ for the same NewBridge protocol. Pick the Ruby that fits your app: JRuby
359
+ when you want real multithreading for web and I/O; CRuby when you prefer
360
+ MRI. R remains the same \textbf{GNU R} you use interactively---including
361
+ compiled extensions and Bioconductor. Ruby and R run in \textbf{separate
362
+ processes}; the \textbf{Galaaz bridge} sends requests to R and returns
363
+ results to Ruby. From your point of view you still write Ruby:
364
+ \texttt{R.c(...)},
303
365
  \texttt{R.library(\textquotesingle{}ggplot2\textquotesingle{})},
304
- \texttt{\textasciitilde{}:mtcars}, and dplyr-style chains on R objects.
305
- You do not need to learn R syntax to get a lot done, though reading R
306
- documentation for individual packages remains useful.
366
+ \texttt{\textasciitilde{}R{[}:mtcars{]}}, and dplyr-style chains on R
367
+ objects. You do not need to learn R syntax to get a lot done, though
368
+ reading R documentation for individual packages remains useful.
307
369
 
308
370
  Earlier experiments with Galaaz used Oracle's \textbf{GraalVM} with
309
371
  TruffleRuby and FastR so that Ruby and R could share one runtime. That
310
372
  path is no longer the focus: \textbf{standard GNU R} gives full
311
373
  compatibility with the R package ecosystem (including compiled
312
- extensions and Bioconductor) while JRuby gives a mature Ruby with
313
- \textbf{real multithreading} for application and I/O code.
374
+ extensions and Bioconductor) while either Ruby engine talks to R over
375
+ NewBridge (JRuby for mature multithreading on the application side;
376
+ CRuby when you prefer MRI).
314
377
 
315
378
  The bridge handles \textbf{communication and typing} between the two
316
379
  worlds; large tables can also flow through \textbf{Apache Arrow} on the
@@ -356,12 +419,15 @@ the gemspec: \textbf{\texttt{galaaz}}, \textbf{\texttt{gstudio}},
356
419
  \textbf{\texttt{gknit-draft}}); for development and CI, prefer the
357
420
  \textbf{\texttt{bin/}} copies so JVM flags and paths stay correct.
358
421
 
359
- Below, \textbf{current (Galaaz 2.0 + JRuby + GNU R)} means the tool is
360
- wired to \textbf{\texttt{jruby}} and
361
- \textbf{\texttt{bin/galaaz\_jruby\_env.inc.sh}} (or equivalent logic in
362
- Ruby via \texttt{lib/galaaz\_jruby.rb}). \textbf{Legacy} means the
363
- script still targets \textbf{GraalVM} polyglot Ruby / FastR-era
364
- invocation and is \textbf{not} expected to work on a typical JRuby-only
422
+ Below, \textbf{current (Galaaz 2.0 + JRuby or CRuby + GNU R)} means the
423
+ tool uses \textbf{\texttt{bin/galaaz-ruby}} /
424
+ \textbf{\texttt{GALAAZ\_RUBY}} (default: \texttt{ruby} on \texttt{PATH})
425
+ and applies JVM flags only when the interpreter is JRuby
426
+ (\texttt{bin/galaaz\_jruby\_env.inc.sh} /
427
+ \texttt{lib/galaaz\_jruby.rb}). \textbf{\texttt{bin/galaaz-jruby}}
428
+ forces JRuby. \textbf{Legacy} means the script still targets
429
+ \textbf{GraalVM} polyglot Ruby / FastR-era invocation and is
430
+ \textbf{not} expected to work on a typical JRuby or CRuby NewBridge
365
431
  setup.
366
432
 
367
433
  \textbf{Table layout:} names in the first column are
@@ -371,8 +437,8 @@ options and examples sit \textbf{outside} the tables so PDF columns stay
371
437
  readable.
372
438
 
373
439
  \begin{verbatim}
374
- ## Warning in styling_latex_scale(out, table_info, "down"): Longtable cannot be
375
- ## resized.
440
+ ## Warning in styling_latex_scale(out, table_info, "down"): Longtable
441
+ ## cannot be resized.
376
442
  \end{verbatim}
377
443
 
378
444
  \begingroup\fontsize{9}{11}\selectfont
@@ -381,9 +447,9 @@ readable.
381
447
  \toprule
382
448
  Script & Role & 2.0?\\
383
449
  \midrule
384
- galaaz-bootstrap & WSL2 helper: Docker checks; optional TinyTeX or poppler for gKnit PDF. & Yes*\\
385
- galaaz-jruby & JRuby with repo lib/ on LOAD\_PATH and required JVM flags (e.g. Arrow). & Yes\\
386
- galaaz\_jruby\_env.inc.sh & Sourced by bash wrappers; sets GALAAZ\_REQUIRED\_JRUBY\_J\_ARGS. & Yes†\\
450
+ galaaz-bootstrap & WSL2 helper: Docker checks; optional TinyTeX/poppler. & Yes*\\
451
+ galaaz-jruby & JRuby with repo lib/ on LOAD\_PATH and JVM flags. & Yes\\
452
+ galaaz\_jruby\_env.inc.sh & Sourced by bash wrappers; sets JRUBY\_J\_ARGS. & Yes†\\
387
453
  install-tinytex & Install TinyTeX for PDF output. & Yes\\
388
454
  \bottomrule
389
455
  \end{longtable}
@@ -409,8 +475,8 @@ install-tinytex & Install TinyTeX for PDF output. & Yes\\
409
475
  Rake}\label{interactive-use-examples-and-rake}
410
476
 
411
477
  \begin{verbatim}
412
- ## Warning in styling_latex_scale(out, table_info, "down"): Longtable cannot be
413
- ## resized.
478
+ ## Warning in styling_latex_scale(out, table_info, "down"): Longtable
479
+ ## cannot be resized.
414
480
  \end{verbatim}
415
481
 
416
482
  \begingroup\fontsize{9}{11}\selectfont
@@ -429,8 +495,8 @@ galaaz & Forward arguments to rake (needs rake; usually JRuby). & Yes\\
429
495
  \subsection{gKnit and document drafts}\label{gknit-and-document-drafts}
430
496
 
431
497
  \begin{verbatim}
432
- ## Warning in styling_latex_scale(out, table_info, "down"): Longtable cannot be
433
- ## resized.
498
+ ## Warning in styling_latex_scale(out, table_info, "down"): Longtable
499
+ ## cannot be resized.
434
500
  \end{verbatim}
435
501
 
436
502
  \begingroup\fontsize{9}{11}\selectfont
@@ -440,7 +506,7 @@ galaaz & Forward arguments to rake (needs rake; usually JRuby). & Yes\\
440
506
  Script & Role & 2.0?\\
441
507
  \midrule
442
508
  gknit & Knit .Rmd via JRuby and R Markdown render. & Yes\\
443
- gknit-draft & Drafts from rticles-style templates; wrapper still uses legacy polyglot ruby. & Legacy\\
509
+ gknit-draft & Drafts from rticles templates; legacy polyglot wrapper. & Legacy\\
444
510
  gknit-draft.rb & Ruby entry: GKnit.draft (use with JRuby + LOAD\_PATH). & JRuby\\
445
511
  gknit\_Rscript & Polyglot Rscript launcher; hard-coded LOAD\_PATH sample. & No\\
446
512
  \bottomrule
@@ -459,8 +525,8 @@ workflows until that wrapper matches the \textbf{\texttt{gknit}} stack.
459
525
  \subsection{Tests}\label{tests}
460
526
 
461
527
  \begin{verbatim}
462
- ## Warning in styling_latex_scale(out, table_info, "down"): Longtable cannot be
463
- ## resized.
528
+ ## Warning in styling_latex_scale(out, table_info, "down"): Longtable
529
+ ## cannot be resized.
464
530
  \end{verbatim}
465
531
 
466
532
  \begingroup\fontsize{9}{11}\selectfont
@@ -470,7 +536,7 @@ workflows until that wrapper matches the \textbf{\texttt{gknit}} stack.
470
536
  Script & Role & 2.0?\\
471
537
  \midrule
472
538
  run\_rspec & Top-level specs/*\_spec.rb with spec\_helper (see docs/testing.md). & Yes\\
473
- run\_all\_rspec & Compile ext/new\_bridge; run specs/ and new\_bridge\_specs/ together. & Yes\\
539
+ run\_all\_rspec & Compile ext/new\_bridge; run specs/ and new\_bridge\_specs/. & Yes\\
474
540
  run\_slow\_rspec & Suites under slow-specs/ (read script header for spec\_helper). & Yes\\
475
541
  run\_old\_rspec & Legacy suites under old\_specs/. & Yes\\
476
542
  run\_rspec\_subset & Numbered subset 1–18 (Documentation/Spec\_Subsets.md). & Yes\\
@@ -481,8 +547,8 @@ run\_rspec\_subset & Numbered subset 1–18 (Documentation/Spec\_Subsets.md). &
481
547
  \subsection{Other}\label{other}
482
548
 
483
549
  \begin{verbatim}
484
- ## Warning in styling_latex_scale(out, table_info, "down"): Longtable cannot be
485
- ## resized.
550
+ ## Warning in styling_latex_scale(out, table_info, "down"): Longtable
551
+ ## cannot be resized.
486
552
  \end{verbatim}
487
553
 
488
554
  \begingroup\fontsize{9}{11}\selectfont
@@ -491,14 +557,15 @@ run\_rspec\_subset & Numbered subset 1–18 (Documentation/Spec\_Subsets.md). &
491
557
  \toprule
492
558
  Script & Role & 2.0?\\
493
559
  \midrule
494
- grun & Graal-era launcher: polyglot ruby with --jvm. Use galaaz-jruby -S instead. & No\\
560
+ grun & Graal-era launcher: polyglot ruby --jvm; prefer galaaz-jruby. & No\\
495
561
  gstudio\_irb.rb / gstudio\_pry.rb & Loaded by gstudio; not meant to be run standalone. & Yes\\
496
562
  \bottomrule
497
563
  \end{longtable}
498
564
  \endgroup{}
499
565
 
500
566
  For day-to-day \textbf{2.0} use, rely on
501
- \textbf{\texttt{bin/galaaz-jruby}}, \textbf{\texttt{bin/gstudio}},
567
+ \textbf{\texttt{bin/galaaz-ruby}} (or \textbf{\texttt{bin/galaaz-jruby}}
568
+ when you want to force JRuby), \textbf{\texttt{bin/gstudio}},
502
569
  \textbf{\texttt{bin/gknit}}, \textbf{\texttt{bin/run\_example}},
503
570
  \textbf{\texttt{bin/run\_rspec}} /
504
571
  \textbf{\texttt{bin/run\_all\_rspec}}, and
@@ -506,7 +573,7 @@ For day-to-day \textbf{2.0} use, rely on
506
573
  Treat \textbf{\texttt{grun}}, \textbf{\texttt{gknit\_Rscript}}, and the
507
574
  polyglot \textbf{\texttt{ruby}} invocation in
508
575
  \textbf{\texttt{gknit-draft}} as \textbf{legacy} until they are ported
509
- to the same JRuby path as \textbf{\texttt{gknit}}.
576
+ to the same launcher path as \textbf{\texttt{gknit}}.
510
577
 
511
578
  \section{System Compatibility}\label{system-compatibility}
512
579
 
@@ -518,7 +585,8 @@ Typical development and CI targets:
518
585
  \textbf{Linux} --- recent Ubuntu LTS or comparable distributions
519
586
  (x86\_64).
520
587
  \item
521
- \textbf{macOS} --- recent releases with JRuby and GNU R available.
588
+ \textbf{macOS} --- recent releases with JRuby or CRuby and GNU R
589
+ available.
522
590
  \item
523
591
  \textbf{Windows} --- use \textbf{WSL2} (same Linux stack as above);
524
592
  native Windows is not the primary target.
@@ -533,16 +601,19 @@ is built with \texttt{make} and a C++ toolchain; see the project
533
601
  \begin{itemize}
534
602
  \tightlist
535
603
  \item
536
- \textbf{JRuby} --- Ruby implementation on the JVM (Galaaz 2.0 is
537
- developed and tested with JRuby).
604
+ \textbf{Ruby} --- \textbf{JRuby} or \textbf{CRuby} (both supported for
605
+ NewBridge). Tested with \textbf{JRuby 10.1.1.0} (+ \textbf{JDK 21})
606
+ and \textbf{CRuby 3.3.12}. Use \texttt{bin/galaaz-ruby} (honors
607
+ \texttt{GALAAZ\_RUBY}) or plain \texttt{gem\ install\ galaaz} under
608
+ the Ruby you choose.
538
609
  \item
539
- A \textbf{Java runtime} (JDK) compatible with your JRuby version.
610
+ \textbf{GNU R} --- \texttt{R} and \texttt{Rscript} on your
611
+ \texttt{PATH} (tested with \textbf{4.3.3}), plus a C++ toolchain
612
+ (\texttt{g++}, \texttt{make}) and the \textbf{Rcpp} package to compile
613
+ the gatekeeper.
540
614
  \item
541
- \textbf{GNU R} --- \texttt{R} on your \texttt{PATH}, with ability to
542
- install packages (CRAN / Bioconductor as needed).
543
- \item
544
- \textbf{Ruby gems} --- from the Galaaz repository,
545
- \texttt{bundle\ install} (see the \texttt{Gemfile} / gemspec).
615
+ \textbf{galaaz gem} --- runtime dependency \texttt{msgpack} is pulled
616
+ in by \texttt{gem\ install}.
546
617
  \item
547
618
  Optional: \textbf{Docker} --- if you run R in a container (common on
548
619
  WSL2); see bootstrap below.
@@ -555,43 +626,93 @@ is built with \texttt{make} and a C++ toolchain; see the project
555
626
 
556
627
  \section{Installation}\label{installation}
557
628
 
558
- From a clone of the Galaaz repository:
629
+ The supported install is \textbf{\texttt{gem\ install} + compile the
630
+ gatekeeper}. You do not need a git clone.
559
631
 
560
632
  \begin{enumerate}
561
633
  \def\labelenumi{\arabic{enumi}.}
562
- \tightlist
563
634
  \item
564
- Install \textbf{JRuby} and \textbf{GNU R} using your preferred package
565
- manager or Ruby version manager.
635
+ Install \textbf{JRuby} (and a compatible \textbf{JDK}) \textbf{or
636
+ CRuby 3.3+}, plus \textbf{GNU R} (with \texttt{Rscript} and a C++
637
+ compiler).
566
638
  \item
567
- Install \textbf{bundler} if needed, then run
568
- \textbf{\texttt{bundle\ install}} in the repository root.
639
+ In R, install \textbf{Rcpp}: \texttt{install.packages("Rcpp")}.
569
640
  \item
570
- Build the bridge native code:
571
- \textbf{\texttt{make\ -C\ ext/new\_bridge\ all}} (or
572
- \textbf{\texttt{rake\ compile\_gatekeeper}}).
641
+ Install the gem: \texttt{jruby\ -S\ gem\ install\ galaaz} (or
642
+ \texttt{gem\ install\ galaaz} under CRuby).
573
643
  \item
574
- Run Ruby scripts with the project load path and JVM flags the project
575
- expects --- the \textbf{\texttt{bin/galaaz-jruby}} wrapper sources
576
- \textbf{\texttt{bin/galaaz\_jruby\_env.inc.sh}} and adds
577
- \textbf{\texttt{-I\ lib}}. This matters especially for \textbf{Apache
578
- Arrow} integration (see \texttt{docs/testing.md}).
644
+ Compile the native gatekeeper from the installed gem:
645
+
646
+ \begin{verbatim}
647
+ gem_dir="$(ruby -e \
648
+ "puts Gem::Specification.find_by_name('galaaz').full_gem_path")"
649
+ # under JRuby: use jruby -e instead of ruby -e
650
+ make -C "${gem_dir}/ext/new_bridge" all
651
+ \end{verbatim}
579
652
  \item
580
653
  Ensure \textbf{\texttt{R}} starts GNU R and can install packages
581
- (network access to CRAN mirrors when you first call
582
- \texttt{R.install\_and\_loads}).
654
+ (network access to CRAN when you first call
655
+ \texttt{R.install\_and\_loads}). For \textbf{Apache Arrow} on
656
+ \textbf{JRuby} (Java 9+), the child JVM needs
657
+ \texttt{-\/-add-opens=java.base/java.nio=ALL-UNNAMED} via
658
+ \textbf{\texttt{JAVA\_OPTS}} (from a checkout,
659
+ \texttt{bin/galaaz-jruby} and \texttt{mise.toml} set this; a leading
660
+ \texttt{jruby\ -J...\ -S\ bundle\ exec} does \textbf{not} pass
661
+ \texttt{-J} to rspec). On \textbf{CRuby}, install Apache Arrow GLib
662
+ (\texttt{libarrow-glib-dev} from the
663
+ \href{https://arrow.apache.org/install/}{Apache Arrow APT} repo) and
664
+ \texttt{gem\ install\ red-arrow} matching
665
+ \texttt{pkg-config\ -\/-modversion\ arrow-glib}. Do not install the
666
+ unrelated Rubygems package named \texttt{arrow}.
583
667
  \end{enumerate}
584
668
 
585
669
  For \textbf{gKnit}, \textbf{knitr}, \textbf{rmarkdown}, and LaTeX (PDF
586
- output), install the corresponding R packages and a TeX distribution if
587
- you need PDF; the repository includes helpers such as
588
- \textbf{\texttt{bin/install-tinytex}} where appropriate.
670
+ output), install the corresponding R packages, \textbf{Pandoc}, and a
671
+ TeX distribution if you need PDF; the repository includes helpers such
672
+ as \textbf{\texttt{bin/install-tinytex}} where appropriate.
589
673
 
590
- A \textbf{table of all \texttt{bin/} scripts} (bootstrap, JRuby wrapper,
674
+ A \textbf{table of all \texttt{bin/} scripts} (bootstrap, Ruby launcher,
591
675
  gstudio, gknit, test runners, and which ones are legacy) is in the
592
676
  section \textbf{Command-line tools (\texttt{bin/})} earlier in this
593
677
  manual.
594
678
 
679
+ \subsubsection{From a repository checkout
680
+ (contributors)}\label{from-a-repository-checkout-contributors}
681
+
682
+ \begin{enumerate}
683
+ \def\labelenumi{\arabic{enumi}.}
684
+ \tightlist
685
+ \item
686
+ Install \textbf{bundler} if needed, then run
687
+ \textbf{\texttt{bundle\ install}} with your chosen Ruby
688
+ (\texttt{jruby\ -S\ bundle\ install} or CRuby
689
+ \texttt{bundle\ install}) in the repository root.
690
+ \item
691
+ Build the bridge native code:
692
+ \textbf{\texttt{make\ -C\ ext/new\_bridge\ all}} (or
693
+ \textbf{\texttt{rake\ compile\_gatekeeper}}).
694
+ \item
695
+ Run scripts with \textbf{\texttt{bin/galaaz-ruby}} (uses \texttt{ruby}
696
+ on \texttt{PATH}; set \textbf{\texttt{GALAAZ\_RUBY=jruby}} or
697
+ \textbf{\texttt{GALAAZ\_RUBY=ruby}} to force an engine). Spec runners:
698
+ \textbf{\texttt{bin/run\_rspec}} /
699
+ \textbf{\texttt{bin/run\_all\_rspec}} (same \texttt{GALAAZ\_RUBY}
700
+ rule). \textbf{\texttt{bin/galaaz-jruby}} remains a thin wrapper that
701
+ forces JRuby.
702
+ \end{enumerate}
703
+
704
+ A \textbf{gstudio} try image with Galaaz already installed is available
705
+ for both engines: \textbf{JRuby} ---
706
+ \texttt{docker\ run\ -\/-rm\ -it\ ghcr.io/rbotafogo/galaaz-try:gstudio}
707
+ (or \texttt{./docker/try-gstudio/run.sh} from a checkout);
708
+ \textbf{CRuby} ---
709
+ \texttt{docker\ run\ -\/-rm\ -it\ ghcr.io/rbotafogo/galaaz-try:cruby}
710
+ (or \texttt{./docker/try-cruby/run.sh}). Maintainers can prove a
711
+ RubyGems install on a throwaway Ubuntu machine (no repo inside the
712
+ container) with \texttt{./docker/cold-install/run.sh\ published-specs}
713
+ (JRuby) or \texttt{./docker/cold-install-cruby/run.sh\ published-specs}
714
+ (CRuby).
715
+
595
716
  \subsection{Windows + WSL2 (optional: Docker / R in a
596
717
  container)}\label{windows-wsl2-optional-docker-r-in-a-container}
597
718
 
@@ -651,12 +772,42 @@ installed.
651
772
  \begin{Highlighting}[]
652
773
  \NormalTok{ vec }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{2}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{4}\NormalTok{)}
653
774
  \FunctionTok{puts}\NormalTok{ vec}
775
+
776
+ \CommentTok{\# R.foo(...) calls an R *function*. Datasets are objects — fetch with}
777
+ \CommentTok{\# \textasciitilde{}:}
778
+ \NormalTok{ df }\OperatorTok{=} \OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}
779
+ \FunctionTok{puts} \ConstantTok{R}\AttributeTok{.summary}\NormalTok{(df)}
654
780
  \end{Highlighting}
655
781
  \end{Shaded}
656
782
 
657
783
  \begin{verbatim}
658
784
  ## [1] 1 2 3 4
659
- \end{verbatim}
785
+ ## mpg cyl disp hp
786
+ ## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
787
+ ## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
788
+ ## Median :19.20 Median :6.000 Median :196.3 Median :123.0
789
+ ## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
790
+ ## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
791
+ ## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
792
+ ## drat wt qsec vs
793
+ ## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
794
+ ## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
795
+ ## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
796
+ ## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
797
+ ## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
798
+ ## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
799
+ ## am gear carb
800
+ ## Min. :0.0000 Min. :3.000 Min. :1.000
801
+ ## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
802
+ ## Median :0.0000 Median :4.000 Median :2.000
803
+ ## Mean :0.4062 Mean :3.688 Mean :2.812
804
+ ## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
805
+ ## Max. :1.0000 Max. :5.000 Max. :8.000
806
+ \end{verbatim}
807
+
808
+ (\texttt{R.mtcars} is wrong: it becomes \texttt{mtcars()} in R and
809
+ fails. With \texttt{using\ Galaaz::SymbolDSL}, the short form
810
+ \texttt{\textasciitilde{}:mtcars} also works.)
660
811
 
661
812
  \begin{itemize}
662
813
  \item
@@ -697,13 +848,16 @@ installed.
697
848
  \end{quote}
698
849
  \end{itemize}
699
850
 
700
- \section{JRuby, multithreading, and the R
701
- bridge}\label{jruby-multithreading-and-the-r-bridge}
851
+ \section{JRuby, CRuby, multithreading, and the R
852
+ bridge}\label{jruby-cruby-multithreading-and-the-r-bridge}
702
853
 
703
- Galaaz 2.0 runs Ruby on \textbf{JRuby}, so your application can use
704
- \textbf{real parallel threads} for I/O-bound work (HTTP clients,
705
- database connections, message consumers, and so on). R itself is still
706
- executed in a \textbf{single GNU R process} behind the Galaaz bridge.
854
+ Galaaz 2.0 supports \textbf{JRuby} and \textbf{CRuby} equally for
855
+ NewBridge. On \textbf{JRuby}, your application can use \textbf{real
856
+ parallel threads} for I/O-bound work (HTTP clients, database
857
+ connections, message consumers, and so on). On \textbf{CRuby}, prefer
858
+ multi-process scaling for CPU-bound concurrency. R itself is still
859
+ executed in a \textbf{single GNU R process} behind the Galaaz bridge
860
+ (use multiple R workers when you need more R throughput).
707
861
 
708
862
  When several Ruby threads call into R at the same time, the bridge
709
863
  \textbf{serializes} those calls: each request is matched to a reply
@@ -739,9 +893,15 @@ bridge itself, see \texttt{specs/bridge\_concurrent\_spec.rb} and
739
893
  \subsection{Long-running R calls and a completion
740
894
  block}\label{long-running-r-calls-and-a-completion-block}
741
895
 
742
- For R work that can take a long time, the bridge can avoid a Ruby-side
743
- \textbf{wait timeout} by scheduling the call and resuming in a
744
- \textbf{block} when the \texttt{RET} arrives.
896
+ For R work that can take a long time \textbf{on the bridge}, the bridge
897
+ can avoid a Ruby-side \textbf{wait timeout} by scheduling the call and
898
+ resuming in a \textbf{block} when the \texttt{RET} arrives.
899
+
900
+ \textbf{Important distinction:} this keeps the \textbf{same} GNU R
901
+ process busy. Other sync \texttt{eval\_r} / gknit chunks still wait on
902
+ that R. For CRAN installs and other work that must \textbf{not}
903
+ monopolize the bridge (or that can OOM a small VM if abandoned
904
+ mid-compile), use \textbf{\texttt{R::Job}} in the next section instead.
745
905
 
746
906
  \begin{itemize}
747
907
  \tightlist
@@ -789,9 +949,11 @@ error.)
789
949
 
790
950
  \ConstantTok{R}\AttributeTok{.eval\_r\_async}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}(\{ Sys.sleep(0.3); 42L \})\textquotesingle{}}\NormalTok{, }\WarningTok{timeout:} \DecValTok{nil}\NormalTok{) }\ControlFlowTok{do} \OperatorTok{|}\NormalTok{result}\OperatorTok{|}
791
951
  \ControlFlowTok{if}\NormalTok{ result}\AttributeTok{.ok?}
792
- \FunctionTok{puts} \StringTok{"[completion] R finished; eval\_r{-}style value: }\SpecialCharTok{\#\{}\NormalTok{result}\AttributeTok{.value.inspect}\SpecialCharTok{\}}\StringTok{"}
952
+ \FunctionTok{puts} \StringTok{"[completion] R finished; value: "} \OperatorTok{+}
953
+ \StringTok{"}\SpecialCharTok{\#\{}\NormalTok{result}\AttributeTok{.value.inspect}\SpecialCharTok{\}}\StringTok{"}
793
954
  \ControlFlowTok{else}
794
- \FunctionTok{puts} \StringTok{"[completion] R/bridge error: }\SpecialCharTok{\#\{}\NormalTok{result}\AttributeTok{.error.class}\SpecialCharTok{\}}\StringTok{: }\SpecialCharTok{\#\{}\NormalTok{result}\AttributeTok{.error.message}\SpecialCharTok{\}}\StringTok{"}
955
+ \FunctionTok{puts} \StringTok{"[completion] R/bridge error: "} \OperatorTok{+}
956
+ \StringTok{"}\SpecialCharTok{\#\{}\NormalTok{result}\AttributeTok{.error.class}\SpecialCharTok{\}}\StringTok{: }\SpecialCharTok{\#\{}\NormalTok{result}\AttributeTok{.error.message}\SpecialCharTok{\}}\StringTok{"}
795
957
  \ControlFlowTok{end}
796
958
  \NormalTok{ completion}\AttributeTok{.push}\NormalTok{(}\WarningTok{:done}\NormalTok{)}
797
959
  \ControlFlowTok{end}
@@ -810,7 +972,7 @@ error.)
810
972
  ## [main] other Ruby work step 1
811
973
  ## [main] other Ruby work step 2
812
974
  ## [main] other Ruby work step 3
813
- ## [completion] R finished; eval_r-style value: "[1] 42"
975
+ ## [completion] R finished; value: "[1] 42"
814
976
  ## [main] R completion has run; exiting.
815
977
  \end{verbatim}
816
978
 
@@ -823,56 +985,403 @@ exists (inside the block, or after data written there is observed
823
985
  elsewhere). Runnable specs live in
824
986
  \textbf{\texttt{new\_bridge\_specs/eval\_r\_async\_spec.rb}}.
825
987
 
988
+ \subsection{\texorpdfstring{Background R jobs
989
+ (\texttt{R::Job})}{Background R jobs (R::Job)}}\label{background-r-jobs-rjob}
990
+
991
+ \texttt{R::Async} / \texttt{R.eval\_r\_async} free the \textbf{Ruby}
992
+ thread while the \textbf{same} bridge R process runs your code. That is
993
+ enough for Rails-style ``don't block the request thread,'' but not
994
+ enough for heavy \texttt{install.packages} or multi-minute model fits:
995
+ the gatekeeper R is still busy, other chunks time out, and abandoning
996
+ the wait can leave compile work burning RAM.
997
+
998
+ \textbf{\texttt{R::Job}} runs that work in a \textbf{child
999
+ \texttt{Rscript} process}. The bridge stays free. Logs and metadata live
1000
+ under \texttt{\textasciitilde{}/.local/share/galaaz/jobs/} (override
1001
+ with \texttt{GALAAZ\_JOBS\_DIR}).
1002
+
1003
+ \subsubsection{Package installs}\label{package-installs}
1004
+
1005
+ \texttt{R.install\_and\_loads} / \texttt{R.install\_rlibs} use
1006
+ \texttt{R::Job.install} and \textbf{await until the child finishes}
1007
+ (default: no wall-clock limit). Optional limit:
1008
+ \texttt{GALAAZ\_INSTALL\_TIMEOUT\_SEC} or
1009
+ \texttt{install\_timeout\_sec:}. On timeout the child process group is
1010
+ killed so leftover \texttt{make}/\texttt{gcc} cannot OOM the shell.
1011
+ Stale \texttt{00LOCK-*} dirs are cleared before the next install. Only
1012
+ one install runs at a time (\texttt{install.lock}).
1013
+
1014
+ \begin{Shaded}
1015
+ \begin{Highlighting}[]
1016
+ \CommentTok{\# May take a long time the first time (e.g. caret); the bridge is not}
1017
+ \CommentTok{\# used for compile.}
1018
+ \ConstantTok{R}\AttributeTok{.install\_and\_loads} \VerbatimStringTok{\textquotesingle{}caret\textquotesingle{}}
1019
+ \end{Highlighting}
1020
+ \end{Shaded}
1021
+
1022
+ \subsubsection{\texorpdfstring{Long arbitrary R (\texttt{eval} /
1023
+ \texttt{script})}{Long arbitrary R (eval / script)}}\label{long-arbitrary-r-eval-script}
1024
+
1025
+ Prefer the \textbf{block} form (like \texttt{File.open}): await the
1026
+ child, yield the job, return the block's value. Without a block, the
1027
+ methods still await by default and return the \texttt{Job}.
1028
+
1029
+ \begin{Shaded}
1030
+ \begin{Highlighting}[]
1031
+ \ControlFlowTok{begin}
1032
+ \NormalTok{ coef }\OperatorTok{=} \ConstantTok{R}\OperatorTok{::}\DataTypeTok{Job}\AttributeTok{.eval}\NormalTok{(}\OperatorTok{\textless{}\textless{}\textasciitilde{}}\ControlFlowTok{R}\DocumentationTok{) \{ |job| job.load\_rds \}}
1033
+ \DocumentationTok{ fit \textless{}{-} lm(mpg \textasciitilde{} wt, data = mtcars)}
1034
+ \DocumentationTok{ saveRDS(unname(coef(fit)), result\_path)}
1035
+ \DocumentationTok{ }\ControlFlowTok{R}
1036
+ \FunctionTok{puts}\NormalTok{ coef}
1037
+ \ControlFlowTok{rescue} \OperatorTok{=\textgreater{}}\NormalTok{ e}
1038
+ \FunctionTok{puts}\NormalTok{ e}\AttributeTok{.class.to\_s}
1039
+ \NormalTok{ e}\AttributeTok{.message.to\_s.scan}\NormalTok{(}\SpecialStringTok{/.\{1,68\}/}\NormalTok{)}\AttributeTok{.each} \OperatorTok{\{} \OperatorTok{|}\NormalTok{line}\OperatorTok{|} \FunctionTok{puts}\NormalTok{ line }\OperatorTok{\}}
1040
+ \ControlFlowTok{end}
1041
+ \end{Highlighting}
1042
+ \end{Shaded}
1043
+
1044
+ \begin{verbatim}
1045
+ ## [1] 37.285126 -5.344472
1046
+ \end{verbatim}
1047
+
1048
+ \begin{Shaded}
1049
+ \begin{Highlighting}[]
1050
+ \CommentTok{\# Without a block: awaits (wait: true is the default) and returns the}
1051
+ \CommentTok{\# Job}
1052
+ \NormalTok{job }\OperatorTok{=} \ConstantTok{R}\OperatorTok{::}\DataTypeTok{Job}\AttributeTok{.eval}\NormalTok{(code)}
1053
+ \NormalTok{job }\OperatorTok{=} \ConstantTok{R}\OperatorTok{::}\DataTypeTok{Job}\AttributeTok{.eval}\NormalTok{(code, }\WarningTok{wait:} \DecValTok{false}\NormalTok{) }\CommentTok{\# start only; call job.wait later}
1054
+
1055
+ \CommentTok{\# Script file; trailing args → commandArgs(trailingOnly=TRUE) in the}
1056
+ \CommentTok{\# child}
1057
+ \NormalTok{res }\OperatorTok{=} \ConstantTok{R}\OperatorTok{::}\DataTypeTok{Job}\AttributeTok{.script}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}train.R\textquotesingle{}}\NormalTok{, }\CharTok{\textquotesingle{}5\textquotesingle{}}\NormalTok{) }\OperatorTok{\{} \OperatorTok{|}\NormalTok{job}\OperatorTok{|}\NormalTok{ job}\AttributeTok{.load\_rds} \OperatorTok{\}}
1058
+ \end{Highlighting}
1059
+ \end{Shaded}
1060
+
1061
+ In the child: \texttt{setwd(job.dir)}, \texttt{.libPaths} includes the
1062
+ Galaaz user library, \texttt{GALAAZ\_JOB\_DIR} is set, and
1063
+ \textbf{\texttt{result\_path}} defaults to
1064
+ \texttt{file.path(GALAAZ\_JOB\_DIR,\ "result.rds")}. Persist with
1065
+ \texttt{saveRDS(...,\ result\_path)}, then load on the bridge with
1066
+ \textbf{\texttt{job.load\_rds}} (short sync \texttt{readRDS} → a normal
1067
+ Galaaz R object).
1068
+
1069
+ \subsubsection{Choosing async vs Job}\label{choosing-async-vs-job}
1070
+
1071
+ {\def\LTcaptype{none} % do not increment counter
1072
+ \begin{longtable}[]{@{}
1073
+ >{\raggedright\arraybackslash}p{(\linewidth - 2\tabcolsep) * \real{0.5455}}
1074
+ >{\raggedright\arraybackslash}p{(\linewidth - 2\tabcolsep) * \real{0.4545}}@{}}
1075
+ \toprule\noalign{}
1076
+ \begin{minipage}[b]{\linewidth}\raggedright
1077
+ Need
1078
+ \end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright
1079
+ Use
1080
+ \end{minipage} \\
1081
+ \midrule\noalign{}
1082
+ \endhead
1083
+ \bottomrule\noalign{}
1084
+ \endlastfoot
1085
+ Don't freeze a Ruby thread; short/medium R on the bridge is OK &
1086
+ \texttt{R::Async} / \texttt{R.eval\_r\_async} \\
1087
+ Install CRAN packages, or long R that must not block the bridge &
1088
+ \texttt{R::Job} / \texttt{R.install\_and\_loads} \\
1089
+ \end{longtable}
1090
+ }
1091
+
1092
+ \subsection{Galaaz + Rails (R-on-Rails) integration
1093
+ baseline}\label{galaaz-rails-r-on-rails-integration-baseline}
1094
+
1095
+ This is the practical \textbf{R-on-Rails} starter: an R scientist's
1096
+ analysis behind a small Rails app. The baseline we used in WSL aimed at:
1097
+
1098
+ \begin{enumerate}
1099
+ \def\labelenumi{\arabic{enumi}.}
1100
+ \tightlist
1101
+ \item
1102
+ Rails boots under \textbf{JRuby or CRuby} (same bridge; see
1103
+ Installation).
1104
+ \item
1105
+ Galaaz is loaded from a local checkout (before publishing to
1106
+ RubyGems).
1107
+ \item
1108
+ A request path can execute \textbf{\texttt{R.eval(...)}} and return a
1109
+ result.
1110
+ \end{enumerate}
1111
+
1112
+ \subsubsection{1) Create the app with Ruby-friendly
1113
+ options}\label{create-the-app-with-ruby-friendly-options}
1114
+
1115
+ Rails defaults can pull gems that are awkward on some setups (for
1116
+ example sqlite native extension paths on JRuby, or deployment extras you
1117
+ do not need). A minimal app avoids early friction:
1118
+
1119
+ \begin{Shaded}
1120
+ \begin{Highlighting}[]
1121
+ \BuiltInTok{cd}\NormalTok{ /home/rbotafogo/desenv\_linux}
1122
+ \ExtensionTok{jruby} \AttributeTok{{-}S}\NormalTok{ rails new hedi }\AttributeTok{{-}{-}skip{-}git} \AttributeTok{{-}{-}minimal} \DataTypeTok{\textbackslash{}}
1123
+ \AttributeTok{{-}{-}skip{-}kamal} \AttributeTok{{-}{-}skip{-}solid} \AttributeTok{{-}{-}skip{-}active{-}record}
1124
+ \end{Highlighting}
1125
+ \end{Shaded}
1126
+
1127
+ Then install gems:
1128
+
1129
+ \begin{Shaded}
1130
+ \begin{Highlighting}[]
1131
+ \BuiltInTok{cd}\NormalTok{ /home/rbotafogo/desenv\_linux/hedi}
1132
+ \ExtensionTok{jruby} \AttributeTok{{-}S}\NormalTok{ bundle install}
1133
+ \end{Highlighting}
1134
+ \end{Shaded}
1135
+
1136
+ \subsubsection{2) Use Galaaz as a local path
1137
+ gem}\label{use-galaaz-as-a-local-path-gem}
1138
+
1139
+ For local development we keep a stable path:
1140
+
1141
+ \begin{itemize}
1142
+ \tightlist
1143
+ \item
1144
+ \texttt{\textasciitilde{}/gems/galaaz} -\textgreater{} symlink to your
1145
+ Galaaz checkout
1146
+ \item
1147
+ optional built gem archive in \texttt{\textasciitilde{}/gems/pkg/}
1148
+ \end{itemize}
1149
+
1150
+ In Rails \texttt{Gemfile}:
1151
+
1152
+ \begin{Shaded}
1153
+ \begin{Highlighting}[]
1154
+ \NormalTok{gem }\StringTok{"galaaz"}\NormalTok{, }\WarningTok{path:} \StringTok{"/home/rbotafogo/gems/galaaz"}\NormalTok{, }\WarningTok{require:} \DecValTok{false}
1155
+ \end{Highlighting}
1156
+ \end{Shaded}
1157
+
1158
+ And load after Rails boot in \texttt{config/application.rb}:
1159
+
1160
+ \begin{Shaded}
1161
+ \begin{Highlighting}[]
1162
+ \NormalTok{config}\AttributeTok{.after\_initialize} \OperatorTok{\{} \FunctionTok{require} \StringTok{"galaaz"} \OperatorTok{\}}
1163
+ \end{Highlighting}
1164
+ \end{Shaded}
1165
+
1166
+ Why \texttt{require:\ false} + \texttt{after\_initialize}? In this
1167
+ integration, loading Galaaz too early via \texttt{Bundler.require}
1168
+ triggered Rails/JRuby initialization failures.
1169
+
1170
+ \subsubsection{3) Simple request-path smoke
1171
+ test}\label{simple-request-path-smoke-test}
1172
+
1173
+ A direct smoke test from Rails runner:
1174
+
1175
+ \begin{Shaded}
1176
+ \begin{Highlighting}[]
1177
+ \BuiltInTok{cd}\NormalTok{ /home/rbotafogo/desenv\_linux/hedi}
1178
+ \ExtensionTok{jruby} \AttributeTok{{-}S}\NormalTok{ bundle exec rails runner }\DataTypeTok{\textbackslash{}}
1179
+ \StringTok{"puts R.eval(\textquotesingle{}sum(c(1,2,3,4,5))\textquotesingle{}).inspect"}
1180
+ \end{Highlighting}
1181
+ \end{Shaded}
1182
+
1183
+ Expected output:
1184
+
1185
+ \begin{Shaded}
1186
+ \begin{Highlighting}[]
1187
+ \NormalTok{15.0}
1188
+ \end{Highlighting}
1189
+ \end{Shaded}
1190
+
1191
+ \subsubsection{4) HTTP endpoint pattern}\label{http-endpoint-pattern}
1192
+
1193
+ For this baseline, a small Rack endpoint was the most stable first step
1194
+ to prove request-time R evaluation. (A full ActionController stack can
1195
+ be enabled later as the app evolves.)
1196
+
1197
+ Minimal pattern:
1198
+
1199
+ \begin{enumerate}
1200
+ \def\labelenumi{\arabic{enumi}.}
1201
+ \tightlist
1202
+ \item
1203
+ Define a Rack app class under \texttt{lib/} that runs
1204
+ \texttt{R.eval(...)} and returns HTML/JSON.
1205
+ \item
1206
+ Point a route to that Rack app (\texttt{root\ to:\ MyRackApp}).
1207
+ \item
1208
+ Verify with browser/curl.
1209
+ \end{enumerate}
1210
+
1211
+ \subsubsection{5) Running from WSL and opening from
1212
+ Windows}\label{running-from-wsl-and-opening-from-windows}
1213
+
1214
+ Recommended bind:
1215
+
1216
+ \begin{Shaded}
1217
+ \begin{Highlighting}[]
1218
+ \ExtensionTok{jruby} \AttributeTok{{-}S}\NormalTok{ bundle exec rails server }\AttributeTok{{-}b}\NormalTok{ 0.0.0.0 }\AttributeTok{{-}p}\NormalTok{ 3000}
1219
+ \end{Highlighting}
1220
+ \end{Shaded}
1221
+
1222
+ Then open from Windows:
1223
+
1224
+ \begin{itemize}
1225
+ \tightlist
1226
+ \item
1227
+ \texttt{http://localhost:3000} (usually works with WSL localhost
1228
+ forwarding), or
1229
+ \item
1230
+ \texttt{http://\textless{}wsl-ip\textgreater{}:3000} if needed.
1231
+ \end{itemize}
1232
+
1233
+ In development, if Host Authorization blocks requests with unexpected
1234
+ Host headers, use:
1235
+
1236
+ \begin{Shaded}
1237
+ \begin{Highlighting}[]
1238
+ \CommentTok{\# config/environments/development.rb}
1239
+ \NormalTok{config}\AttributeTok{.hosts.clear}
1240
+ \end{Highlighting}
1241
+ \end{Shaded}
1242
+
1243
+ \subsubsection{6) Troubleshooting
1244
+ checklist}\label{troubleshooting-checklist}
1245
+
1246
+ \begin{itemize}
1247
+ \tightlist
1248
+ \item
1249
+ \texttt{Could\ not\ find\ ...\ in\ locally\ installed\ gems}: run
1250
+ \texttt{jruby\ -S\ bundle\ install} in the Rails app directory.
1251
+ \item
1252
+ Stale PID after crash: remove \texttt{tmp/pids/server.pid}.
1253
+ \item
1254
+ Local Galaaz path changed: verify \texttt{Gemfile} path target exists
1255
+ and rerun bundler.
1256
+ \item
1257
+ R runtime issues: confirm GNU R is installed and on \texttt{PATH} in
1258
+ the same shell where Rails runs.
1259
+ \end{itemize}
1260
+
1261
+ As new Rails features are added (controllers, jobs, websockets,
1262
+ background rendering, plot generation), extend this section with
1263
+ concrete, runnable snippets and the associated operational checks.
1264
+
826
1265
  \section{Accessing R from Ruby}\label{accessing-r-from-ruby}
827
1266
 
828
1267
  One of the nice aspects of Galaaz is that variables and functions
829
1268
  defined in R can be easily accessed from Ruby. For instance, to access
830
1269
  the \texttt{mtcars} data frame from R in Ruby, we use the symbol
831
1270
  \texttt{:mtcars} preceded by the \texttt{\textasciitilde{}} operator:
832
- \texttt{\textasciitilde{}:mtcars} retrieves the value of the
1271
+ \texttt{\textasciitilde{}R{[}:mtcars{]}} retrieves the value of the
833
1272
  \texttt{mtcars} object in R.
834
1273
 
835
1274
  \begin{Shaded}
836
1275
  \begin{Highlighting}[]
837
- \FunctionTok{puts} \OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}
838
- \end{Highlighting}
839
- \end{Shaded}
840
-
841
- \begin{verbatim}
842
- ## mpg cyl disp hp drat wt qsec vs am gear carb
843
- ## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
844
- ## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
845
- ## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
846
- ## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
847
- ## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
848
- ## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
849
- ## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
850
- ## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
851
- ## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
852
- ## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
853
- ## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
854
- ## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
855
- ## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
856
- ## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
857
- ## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
858
- ## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
859
- ## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
860
- ## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
861
- ## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
862
- ## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
863
- ## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
864
- ## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
865
- ## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
866
- ## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
867
- ## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
868
- ## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
869
- ## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
870
- ## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
871
- ## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
872
- ## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
873
- ## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
874
- ## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
875
- \end{verbatim}
1276
+ \FunctionTok{puts} \OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}
1277
+ \end{Highlighting}
1278
+ \end{Shaded}
1279
+
1280
+ \begin{verbatim}
1281
+ ## mpg cyl disp hp drat wt qsec vs am gear
1282
+ ## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4
1283
+ ## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4
1284
+ ## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4
1285
+ ## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3
1286
+ ## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3
1287
+ ## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3
1288
+ ## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3
1289
+ ## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4
1290
+ ## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4
1291
+ ## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4
1292
+ ## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4
1293
+ ## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3
1294
+ ## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3
1295
+ ## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3
1296
+ ## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3
1297
+ ## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3
1298
+ ## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3
1299
+ ## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4
1300
+ ## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4
1301
+ ## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4
1302
+ ## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3
1303
+ ## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3
1304
+ ## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3
1305
+ ## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3
1306
+ ## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3
1307
+ ## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4
1308
+ ## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5
1309
+ ## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5
1310
+ ## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5
1311
+ ## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5
1312
+ ## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5
1313
+ ## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4
1314
+ ## carb
1315
+ ## Mazda RX4 4
1316
+ ## Mazda RX4 Wag 4
1317
+ ## Datsun 710 1
1318
+ ## Hornet 4 Drive 1
1319
+ ## Hornet Sportabout 2
1320
+ ## Valiant 1
1321
+ ## Duster 360 4
1322
+ ## Merc 240D 2
1323
+ ## Merc 230 2
1324
+ ## Merc 280 4
1325
+ ## Merc 280C 4
1326
+ ## Merc 450SE 3
1327
+ ## Merc 450SL 3
1328
+ ## Merc 450SLC 3
1329
+ ## Cadillac Fleetwood 4
1330
+ ## Lincoln Continental 4
1331
+ ## Chrysler Imperial 4
1332
+ ## Fiat 128 1
1333
+ ## Honda Civic 2
1334
+ ## Toyota Corolla 1
1335
+ ## Toyota Corona 1
1336
+ ## Dodge Challenger 2
1337
+ ## AMC Javelin 2
1338
+ ## Camaro Z28 4
1339
+ ## Pontiac Firebird 2
1340
+ ## Fiat X1-9 1
1341
+ ## Porsche 914-2 2
1342
+ ## Lotus Europa 2
1343
+ ## Ford Pantera L 4
1344
+ ## Ferrari Dino 6
1345
+ ## Maserati Bora 8
1346
+ ## Volvo 142E 2
1347
+ \end{verbatim}
1348
+
1349
+ \subsection{Scoped symbols and lexical
1350
+ scoping}\label{scoped-symbols-and-lexical-scoping}
1351
+
1352
+ Galaaz 2.0 uses \textbf{scoped symbols} by default. The canonical style
1353
+ is \texttt{R{[}:name{]}}:
1354
+
1355
+ \begin{itemize}
1356
+ \tightlist
1357
+ \item
1358
+ \texttt{\textasciitilde{}R{[}:mtcars{]}} fetches an R object by name.
1359
+ \item
1360
+ \texttt{R{[}:a{]}\ +\ R{[}:b{]}} builds an expression.
1361
+ \item
1362
+ \texttt{R{[}:year{]}.up\_to(R{[}:day{]})} builds range expressions.
1363
+ \end{itemize}
1364
+
1365
+ If you prefer the terse \texttt{:x} syntax, you can opt in with lexical
1366
+ scoping using a Ruby refinement:
1367
+
1368
+ \begin{Shaded}
1369
+ \begin{Highlighting}[]
1370
+ \ControlFlowTok{module} \DataTypeTok{MyScript}
1371
+ \FunctionTok{using} \DataTypeTok{Galaaz}\OperatorTok{::}\DataTypeTok{SymbolDSL}
1372
+
1373
+ \ControlFlowTok{def} \DecValTok{self}\AttributeTok{.run}
1374
+ \NormalTok{ expr }\OperatorTok{=} \WarningTok{:a} \OperatorTok{+} \WarningTok{:b}
1375
+ \FunctionTok{puts}\NormalTok{ expr}
1376
+ \FunctionTok{puts} \OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}
1377
+ \ControlFlowTok{end}
1378
+ \ControlFlowTok{end}
1379
+ \end{Highlighting}
1380
+ \end{Shaded}
1381
+
1382
+ \texttt{using\ Galaaz::SymbolDSL} is \textbf{lexically scoped}: only
1383
+ code in that module/file scope gets \texttt{:x} DSL behavior. Outside
1384
+ that scope, plain Ruby \texttt{Symbol} behavior is unchanged.
876
1385
 
877
1386
  To access an R function from Ruby, the R function needs to be preceded
878
1387
  by \texttt{R.} scoping. Below we see an example of creating a R::Vector
@@ -1012,11 +1521,12 @@ element of our previously created vector:
1012
1521
 
1013
1522
  This manual has been formatted using gKnit. gKnit uses knitr and R
1014
1523
  Markdown to knit a document in Ruby or R and output it in any of the
1015
- available formats for R Markdown. gKnit runs with \textbf{JRuby},
1016
- \textbf{GNU R}, and Galaaz. In gKnit, Ruby variables are persisted
1017
- between chunks, making it an ideal solution for literate programming.
1018
- Also, since it is based on Galaaz, Ruby chunks can have access to R
1019
- variables and combining Ruby with R in one document is natural.
1524
+ available formats for R Markdown. gKnit runs with \textbf{JRuby or
1525
+ CRuby}, \textbf{GNU R}, and Galaaz. In gKnit, Ruby variables are
1526
+ persisted between chunks, making it an ideal solution for literate
1527
+ programming. Also, since it is based on Galaaz, Ruby chunks can have
1528
+ access to R variables and combining Ruby with R in one document is
1529
+ natural.
1020
1530
 
1021
1531
  The idea of ``literate programming'' was first introduced by Donald
1022
1532
  Knuth in the 1980's (Knuth 1984). The main intention of this approach
@@ -1150,7 +1660,7 @@ in a single document, text and code.
1150
1660
  In gKnit, Ruby variables are persisted between chunks, making it an
1151
1661
  ideal solution for literate programming in this language. Also, since it
1152
1662
  is based on Galaaz, Ruby chunks can access R variables
1153
- (\texttt{\textasciitilde{}:name}, \texttt{R.*}) through the
1663
+ (\texttt{\textasciitilde{}R{[}:name{]}}, \texttt{R.*}) through the
1154
1664
  \textbf{Galaaz bridge} while knitr drives \textbf{GNU R}---no GraalVM
1155
1665
  polyglot runtime is required.
1156
1666
 
@@ -1452,7 +1962,9 @@ linear regression line (method = ``lm'') for every manufacturer.
1452
1962
  \FunctionTok{library}\NormalTok{(ggplot2)}
1453
1963
  \FunctionTok{data}\NormalTok{(mpg, }\AttributeTok{package=}\StringTok{"ggplot2"}\NormalTok{)}
1454
1964
 
1455
- \NormalTok{mpg\_select }\OtherTok{\textless{}{-}}\NormalTok{ mpg[mpg}\SpecialCharTok{$}\NormalTok{manufacturer }\SpecialCharTok{\%in\%} \FunctionTok{c}\NormalTok{(}\StringTok{"audi"}\NormalTok{, }\StringTok{"ford"}\NormalTok{, }\StringTok{"honda"}\NormalTok{, }\StringTok{"hyundai"}\NormalTok{), ]}
1965
+ \NormalTok{mpg\_select }\OtherTok{\textless{}{-}}\NormalTok{ mpg[}
1966
+ \NormalTok{ mpg}\SpecialCharTok{$}\NormalTok{manufacturer }\SpecialCharTok{\%in\%} \FunctionTok{c}\NormalTok{(}\StringTok{"audi"}\NormalTok{, }\StringTok{"ford"}\NormalTok{, }\StringTok{"honda"}\NormalTok{, }\StringTok{"hyundai"}\NormalTok{),}
1967
+ \NormalTok{]}
1456
1968
 
1457
1969
  \CommentTok{\# Scatterplot}
1458
1970
  \FunctionTok{theme\_set}\NormalTok{(}\FunctionTok{theme\_bw}\NormalTok{()) }\CommentTok{\# pre{-}set the bw theme.}
@@ -1469,7 +1981,7 @@ linear regression line (method = ``lm'') for every manufacturer.
1469
1981
  ## `geom_smooth()` using formula = 'y ~ x'
1470
1982
  \end{verbatim}
1471
1983
 
1472
- \pandocbounded{\includegraphics[keepaspectratio]{manual_files/figure-latex/bubble-1.png}}
1984
+ \pandocbounded{\includegraphics[keepaspectratio]{manual_files/manual_files/figure-latex/bubble-1.png}}
1473
1985
 
1474
1986
  \subsection{Ruby chunks}\label{ruby-chunks}
1475
1987
 
@@ -1539,7 +2051,8 @@ phrase. gKnit allows adding inline Ruby code with the `rb' engine. The
1539
2051
  following chunk specification will create and inline Ruby text:
1540
2052
 
1541
2053
  \begin{verbatim}
1542
- This is some text with inline Ruby accessing variable 'b' which has value:
2054
+ This is some text with inline Ruby accessing
2055
+ variable 'b' which has value:
1543
2056
  ```{rb puts b}
1544
2057
  ```
1545
2058
  and is followed by some other text!
@@ -1638,12 +2151,12 @@ library is a great library for creating beautiful tables. Take a look at
1638
2151
 
1639
2152
  In the next chunk, we output the `mtcars' dataframe from R in a nicely
1640
2153
  formatted table. Note that we retrieve the mtcars dataframe by using
1641
- `\textasciitilde:mtcars'.
2154
+ `\textasciitilde R{[}:mtcars{]}'.
1642
2155
 
1643
2156
  \begin{Shaded}
1644
2157
  \begin{Highlighting}[]
1645
2158
  \ConstantTok{R}\AttributeTok{.install\_and\_loads}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}kableExtra\textquotesingle{}}\NormalTok{)}
1646
- \NormalTok{outputs (}\OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}\NormalTok{)}\AttributeTok{.kable.kable\_styling}
2159
+ \NormalTok{outputs (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}\NormalTok{)}\AttributeTok{.kable.kable\_styling}
1647
2160
  \end{Highlighting}
1648
2161
  \end{Shaded}
1649
2162
 
@@ -1733,7 +2246,8 @@ is trivial with Galaaz.
1733
2246
  A word of advice: the `caret' package has lots of dependencies and
1734
2247
  installing it in a Linux system is a time consuming operation. Method
1735
2248
  `R.install\_and\_loads' will install the package if it is not already
1736
- installed and can take a while.
2249
+ installed (via \textbf{\texttt{R::Job}}: a child \texttt{Rscript}, so
2250
+ the bridge stays free) and can take a while.
1737
2251
 
1738
2252
  \begin{verbatim}
1739
2253
  ```{include model}
@@ -1786,7 +2300,7 @@ installed and can take a while.
1786
2300
 
1787
2301
  \begin{Shaded}
1788
2302
  \begin{Highlighting}[]
1789
- \NormalTok{mtcars }\OperatorTok{=} \OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}
2303
+ \NormalTok{mtcars }\OperatorTok{=} \OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}
1790
2304
  \NormalTok{model }\OperatorTok{=} \DataTypeTok{Model}\AttributeTok{.new}\NormalTok{(mtcars, }\WarningTok{percent\_train:} \FloatTok{0.8}\NormalTok{)}
1791
2305
  \NormalTok{model}\AttributeTok{.partition}\NormalTok{(}\WarningTok{:mpg}\NormalTok{)}
1792
2306
  \FunctionTok{puts}\NormalTok{ model}\AttributeTok{.train.head}
@@ -2023,15 +2537,17 @@ In order to create a document with one of those templates, use the
2023
2537
  following command:
2024
2538
 
2025
2539
  \begin{verbatim}
2026
- gknit-draft --filename <my_document> --template <template> --package <package>
2027
- --create_dir
2540
+ gknit-draft --filename <my_document> \
2541
+ --template <template> --package <package> \
2542
+ --create_dir
2028
2543
  \end{verbatim}
2029
2544
 
2030
2545
  So, in order to create a template for writing an R Journal, use:
2031
2546
 
2032
2547
  \begin{verbatim}
2033
- gknit-draft --filename my_r_article --template rjournal_article --package rticles
2034
- --create_dir
2548
+ gknit-draft --filename my_r_article \
2549
+ --template rjournal_article --package rticles \
2550
+ --create_dir
2035
2551
  \end{verbatim}
2036
2552
 
2037
2553
  \section{Accessing R variables}\label{accessing-r-variables}
@@ -2046,7 +2562,7 @@ Method \texttt{outputs} is only available when used with gKnit.
2046
2562
 
2047
2563
  \begin{Shaded}
2048
2564
  \begin{Highlighting}[]
2049
- \NormalTok{outputs (}\OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}\NormalTok{)}\AttributeTok{.kable.kable\_styling}
2565
+ \NormalTok{outputs (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}\NormalTok{)}\AttributeTok{.kable.kable\_styling}
2050
2566
  \end{Highlighting}
2051
2567
  \end{Shaded}
2052
2568
 
@@ -2157,7 +2673,10 @@ used to create a data frame is `data.frame', in Galaaz we use
2157
2673
 
2158
2674
  \begin{Shaded}
2159
2675
  \begin{Highlighting}[]
2160
- \NormalTok{df }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.data\_\_frame}\NormalTok{(}\WarningTok{typeof:}\NormalTok{ vec}\AttributeTok{.typeof}\NormalTok{, }\WarningTok{mode:}\NormalTok{ vec}\AttributeTok{.mode}\NormalTok{, }\WarningTok{storage\_\_mode:}\NormalTok{ vec}\AttributeTok{.storage\_\_mode}\NormalTok{)}
2676
+ \NormalTok{df }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.data\_\_frame}\NormalTok{(}
2677
+ \WarningTok{typeof:}\NormalTok{ vec}\AttributeTok{.typeof}\NormalTok{,}
2678
+ \WarningTok{mode:}\NormalTok{ vec}\AttributeTok{.mode}\NormalTok{,}
2679
+ \WarningTok{storage\_\_mode:}\NormalTok{ vec}\AttributeTok{.storage\_\_mode}\NormalTok{)}
2161
2680
  \FunctionTok{puts}\NormalTok{ df}
2162
2681
  \end{Highlighting}
2163
2682
  \end{Shaded}
@@ -2186,7 +2705,10 @@ normal Ruby rules and the number 1 is an integer and 1.0 is a float.
2186
2705
 
2187
2706
  \begin{Shaded}
2188
2707
  \begin{Highlighting}[]
2189
- \NormalTok{df }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.data\_\_frame}\NormalTok{(}\WarningTok{typeof:}\NormalTok{ vec}\AttributeTok{.typeof}\NormalTok{, }\WarningTok{mode:}\NormalTok{ vec}\AttributeTok{.mode}\NormalTok{, }\WarningTok{storage\_\_mode:}\NormalTok{ vec}\AttributeTok{.storage\_\_mode}\NormalTok{)}
2708
+ \NormalTok{df }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.data\_\_frame}\NormalTok{(}
2709
+ \WarningTok{typeof:}\NormalTok{ vec}\AttributeTok{.typeof}\NormalTok{,}
2710
+ \WarningTok{mode:}\NormalTok{ vec}\AttributeTok{.mode}\NormalTok{,}
2711
+ \WarningTok{storage\_\_mode:}\NormalTok{ vec}\AttributeTok{.storage\_\_mode}\NormalTok{)}
2190
2712
  \NormalTok{outputs df}\AttributeTok{.kable.kable\_styling}
2191
2713
  \end{Highlighting}
2192
2714
  \end{Shaded}
@@ -2207,26 +2729,20 @@ error.
2207
2729
 
2208
2730
  \begin{Shaded}
2209
2731
  \begin{Highlighting}[]
2210
- \NormalTok{vec }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\DecValTok{1}\NormalTok{, hello, }\DecValTok{5}\NormalTok{)}
2732
+ \ControlFlowTok{begin}
2733
+ \NormalTok{ vec }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\DecValTok{1}\NormalTok{, hello, }\DecValTok{5}\NormalTok{)}
2734
+ \ControlFlowTok{rescue} \OperatorTok{=\textgreater{}}\NormalTok{ e}
2735
+ \FunctionTok{puts}\NormalTok{ e}\AttributeTok{.class.to\_s}
2736
+ \NormalTok{ e}\AttributeTok{.message.to\_s.scan}\NormalTok{(}\SpecialStringTok{/.\{1,68\}/}\NormalTok{)}\AttributeTok{.each} \OperatorTok{\{} \OperatorTok{|}\NormalTok{line}\OperatorTok{|} \FunctionTok{puts}\NormalTok{ line }\OperatorTok{\}}
2737
+ \ControlFlowTok{end}
2211
2738
  \end{Highlighting}
2212
2739
  \end{Shaded}
2213
2740
 
2214
2741
  \begin{verbatim}
2742
+ ## NameError
2215
2743
  ## undefined local variable or method 'hello' for an instance of RC
2216
2744
  \end{verbatim}
2217
2745
 
2218
- \begin{verbatim}
2219
- ## /home/rbotafogo/desenv_linux/galaaz/lib/util/exec_ruby.rb:170:in 'exec_ruby'
2220
- ## org/jruby/RubyKernel.java:1268:in 'eval'
2221
- ## /home/rbotafogo/desenv_linux/galaaz/lib/util/exec_ruby.rb:169:in 'exec_ruby'
2222
- ## /home/rbotafogo/desenv_linux/galaaz/lib/gknit/knitr_engine.rb:777:in 'block in initialize'
2223
- ## org/jruby/RubyBasicObject.java:2695:in 'instance_eval'
2224
- ## org/jruby/RubyBasicObject.java:2723:in 'instance_eval'
2225
- ## /home/rbotafogo/desenv_linux/galaaz/lib/gknit/knitr_engine.rb:748:in 'block in initialize'
2226
- ## /home/rbotafogo/desenv_linux/galaaz/lib/R_interface/new_bridge_adapter.rb:358:in 'block in register_callback_proc_stub'
2227
- ## /home/rbotafogo/desenv_linux/galaaz/lib/new_bridge/session_client.rb:413:in 'block in handle_call'
2228
- \end{verbatim}
2229
-
2230
2746
  Here is a vector with logical values
2231
2747
 
2232
2748
  \begin{Shaded}
@@ -2659,8 +3175,8 @@ native Ruby type.
2659
3175
  A data frame is a table like structure in which each column has the same
2660
3176
  number of rows. Data frames are the basic structure for storing data for
2661
3177
  data analysis. We have already seen a data frame previously when we
2662
- accessed variable `\textasciitilde:mtcars'. In order to create a data
2663
- frame, function 'data\_\_frame' is used:
3178
+ accessed variable `\textasciitilde R{[}:mtcars{]}'. In order to create a
3179
+ data frame, function 'data\_\_frame' is used:
2664
3180
 
2665
3181
  \begin{Shaded}
2666
3182
  \begin{Highlighting}[]
@@ -2687,9 +3203,9 @@ the row or column
2687
3203
 
2688
3204
  \begin{Shaded}
2689
3205
  \begin{Highlighting}[]
2690
- \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}\NormalTok{)}\AttributeTok{.head}
2691
- \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}\NormalTok{)}\KeywordTok{[}\DecValTok{1}\NormalTok{, }\DecValTok{2}\KeywordTok{]}
2692
- \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}\NormalTok{)}\KeywordTok{[}\VerbatimStringTok{\textquotesingle{}Datsun 710\textquotesingle{}}\NormalTok{, }\VerbatimStringTok{\textquotesingle{}mpg\textquotesingle{}}\KeywordTok{]}
3206
+ \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}\NormalTok{)}\AttributeTok{.head}
3207
+ \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}\NormalTok{)}\KeywordTok{[}\DecValTok{1}\NormalTok{, }\DecValTok{2}\KeywordTok{]}
3208
+ \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}\NormalTok{)}\KeywordTok{[}\VerbatimStringTok{\textquotesingle{}Datsun 710\textquotesingle{}}\NormalTok{, }\VerbatimStringTok{\textquotesingle{}mpg\textquotesingle{}}\KeywordTok{]}
2693
3209
  \end{Highlighting}
2694
3210
  \end{Shaded}
2695
3211
 
@@ -2710,14 +3226,14 @@ the double square bracket operator:
2710
3226
 
2711
3227
  \begin{Shaded}
2712
3228
  \begin{Highlighting}[]
2713
- \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}\NormalTok{)}\KeywordTok{[[}\VerbatimStringTok{\textquotesingle{}mpg\textquotesingle{}}\KeywordTok{]]}
3229
+ \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}\NormalTok{)}\KeywordTok{[[}\VerbatimStringTok{\textquotesingle{}mpg\textquotesingle{}}\KeywordTok{]]}
2714
3230
  \end{Highlighting}
2715
3231
  \end{Shaded}
2716
3232
 
2717
3233
  \begin{verbatim}
2718
- ## [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
2719
- ## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
2720
- ## [31] 15.0 21.4
3234
+ ## [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3
3235
+ ## [14] 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3
3236
+ ## [27] 26.0 30.4 15.8 19.7 15.0 21.4
2721
3237
  \end{verbatim}
2722
3238
 
2723
3239
  A data frame column can also be accessed as if it were an instance
@@ -2725,14 +3241,14 @@ variable of the data frame:
2725
3241
 
2726
3242
  \begin{Shaded}
2727
3243
  \begin{Highlighting}[]
2728
- \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}\NormalTok{)}\AttributeTok{.mpg}
3244
+ \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}\NormalTok{)}\AttributeTok{.mpg}
2729
3245
  \end{Highlighting}
2730
3246
  \end{Shaded}
2731
3247
 
2732
3248
  \begin{verbatim}
2733
- ## [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
2734
- ## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
2735
- ## [31] 15.0 21.4
3249
+ ## [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3
3250
+ ## [14] 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3
3251
+ ## [27] 26.0 30.4 15.8 19.7 15.0 21.4
2736
3252
  \end{verbatim}
2737
3253
 
2738
3254
  Slicing a data frame can be done by indexing it with a vector (we use
@@ -2740,7 +3256,7 @@ Slicing a data frame can be done by indexing it with a vector (we use
2740
3256
 
2741
3257
  \begin{Shaded}
2742
3258
  \begin{Highlighting}[]
2743
- \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}\NormalTok{)}\KeywordTok{[}\ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}mpg\textquotesingle{}}\NormalTok{, }\VerbatimStringTok{\textquotesingle{}hp\textquotesingle{}}\NormalTok{)}\KeywordTok{]}\AttributeTok{.head}
3259
+ \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}\NormalTok{)}\KeywordTok{[}\ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}mpg\textquotesingle{}}\NormalTok{, }\VerbatimStringTok{\textquotesingle{}hp\textquotesingle{}}\NormalTok{)}\KeywordTok{]}\AttributeTok{.head}
2744
3260
  \end{Highlighting}
2745
3261
  \end{Shaded}
2746
3262
 
@@ -2755,7 +3271,7 @@ keyword for the column:
2755
3271
 
2756
3272
  \begin{Shaded}
2757
3273
  \begin{Highlighting}[]
2758
- \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}\NormalTok{)}\KeywordTok{[}\ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}Datsun 710\textquotesingle{}}\NormalTok{, }\VerbatimStringTok{\textquotesingle{}Camaro Z28\textquotesingle{}}\NormalTok{), }\WarningTok{:all}\KeywordTok{]}
3274
+ \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}\NormalTok{)}\KeywordTok{[}\ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}Datsun 710\textquotesingle{}}\NormalTok{, }\VerbatimStringTok{\textquotesingle{}Camaro Z28\textquotesingle{}}\NormalTok{), }\WarningTok{:all}\KeywordTok{]}
2759
3275
  \end{Highlighting}
2760
3276
  \end{Shaded}
2761
3277
 
@@ -2768,21 +3284,21 @@ keyword for the column:
2768
3284
  Finally, a data frame can also be indexed with a logical vector. In this
2769
3285
  next example, the `am' column of :mtcars is compared with 0 (with method
2770
3286
  `eq'). When `am' is equal to 0 the car is automatic. So, by doing
2771
- `(\textasciitilde:mtcars).am.eq 0' a logical vector is created with
2772
- `true' whenever `am' is 0 and `false' otherwise.
3287
+ `(\textasciitilde R{[}:mtcars{]}).am.eq 0' a logical vector is created
3288
+ with `true' whenever `am' is 0 and `false' otherwise.
2773
3289
 
2774
3290
  \begin{Shaded}
2775
3291
  \begin{Highlighting}[]
2776
3292
  \CommentTok{\# obtain a vector with \textquotesingle{}true\textquotesingle{} for cars with automatic transmission}
2777
- \NormalTok{automatic }\OperatorTok{=}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}\NormalTok{)}\AttributeTok{.am.eq} \DecValTok{0}
3293
+ \NormalTok{automatic }\OperatorTok{=}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}\NormalTok{)}\AttributeTok{.am.eq} \DecValTok{0}
2778
3294
  \FunctionTok{puts}\NormalTok{ automatic}
2779
3295
  \end{Highlighting}
2780
3296
  \end{Shaded}
2781
3297
 
2782
3298
  \begin{verbatim}
2783
- ## [1] FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
2784
- ## [13] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE
2785
- ## [25] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
3299
+ ## [1] FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
3300
+ ## [12] TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE
3301
+ ## [23] TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
2786
3302
  \end{verbatim}
2787
3303
 
2788
3304
  Using this logical vector, the data frame is indexed, returning a new
@@ -2791,31 +3307,51 @@ data frame in which all cars have automatic transmission.
2791
3307
  \begin{Shaded}
2792
3308
  \begin{Highlighting}[]
2793
3309
  \CommentTok{\# slice the data frame by using this vector}
2794
- \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}\NormalTok{)}\KeywordTok{[}\NormalTok{automatic, }\WarningTok{:all}\KeywordTok{]}
2795
- \end{Highlighting}
2796
- \end{Shaded}
2797
-
2798
- \begin{verbatim}
2799
- ## mpg cyl disp hp drat wt qsec vs am gear carb
2800
- ## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
2801
- ## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
2802
- ## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
2803
- ## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
2804
- ## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
2805
- ## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
2806
- ## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
2807
- ## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
2808
- ## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
2809
- ## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
2810
- ## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
2811
- ## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
2812
- ## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
2813
- ## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
2814
- ## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
2815
- ## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
2816
- ## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
2817
- ## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
2818
- ## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
3310
+ \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}\NormalTok{)}\KeywordTok{[}\NormalTok{automatic, }\WarningTok{:all}\KeywordTok{]}
3311
+ \end{Highlighting}
3312
+ \end{Shaded}
3313
+
3314
+ \begin{verbatim}
3315
+ ## mpg cyl disp hp drat wt qsec vs am gear
3316
+ ## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3
3317
+ ## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3
3318
+ ## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3
3319
+ ## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3
3320
+ ## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4
3321
+ ## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4
3322
+ ## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4
3323
+ ## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4
3324
+ ## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3
3325
+ ## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3
3326
+ ## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3
3327
+ ## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3
3328
+ ## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3
3329
+ ## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3
3330
+ ## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3
3331
+ ## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3
3332
+ ## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3
3333
+ ## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3
3334
+ ## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3
3335
+ ## carb
3336
+ ## Hornet 4 Drive 1
3337
+ ## Hornet Sportabout 2
3338
+ ## Valiant 1
3339
+ ## Duster 360 4
3340
+ ## Merc 240D 2
3341
+ ## Merc 230 2
3342
+ ## Merc 280 4
3343
+ ## Merc 280C 4
3344
+ ## Merc 450SE 3
3345
+ ## Merc 450SL 3
3346
+ ## Merc 450SLC 3
3347
+ ## Cadillac Fleetwood 4
3348
+ ## Lincoln Continental 4
3349
+ ## Chrysler Imperial 4
3350
+ ## Toyota Corona 1
3351
+ ## Dodge Challenger 2
3352
+ ## AMC Javelin 2
3353
+ ## Camaro Z28 4
3354
+ ## Pontiac Firebird 2
2819
3355
  \end{verbatim}
2820
3356
 
2821
3357
  \section{Writing Expressions in
@@ -2832,7 +3368,7 @@ The code below creates an expression summing two symbols
2832
3368
 
2833
3369
  \begin{Shaded}
2834
3370
  \begin{Highlighting}[]
2835
- \NormalTok{exp1 }\OperatorTok{=} \WarningTok{:a} \OperatorTok{+} \WarningTok{:b}
3371
+ \NormalTok{exp1 }\OperatorTok{=} \ConstantTok{R}\KeywordTok{[}\WarningTok{:a}\KeywordTok{]} \OperatorTok{+} \ConstantTok{R}\KeywordTok{[}\WarningTok{:b}\KeywordTok{]}
2836
3372
  \FunctionTok{puts}\NormalTok{ exp1}
2837
3373
  \end{Highlighting}
2838
3374
  \end{Shaded}
@@ -2845,7 +3381,7 @@ We can build any complex mathematical expression
2845
3381
 
2846
3382
  \begin{Shaded}
2847
3383
  \begin{Highlighting}[]
2848
- \NormalTok{exp2 }\OperatorTok{=}\NormalTok{ (}\WarningTok{:a} \OperatorTok{+} \WarningTok{:b}\NormalTok{) }\OperatorTok{*} \FloatTok{2.0} \OperatorTok{+} \WarningTok{:c} \OperatorTok{**} \DecValTok{2} \OperatorTok{/} \WarningTok{:z}
3384
+ \NormalTok{exp2 }\OperatorTok{=}\NormalTok{ (}\ConstantTok{R}\KeywordTok{[}\WarningTok{:a}\KeywordTok{]} \OperatorTok{+} \ConstantTok{R}\KeywordTok{[}\WarningTok{:b}\KeywordTok{]}\NormalTok{) }\OperatorTok{*} \FloatTok{2.0} \OperatorTok{+} \ConstantTok{R}\KeywordTok{[}\WarningTok{:c}\KeywordTok{]} \OperatorTok{**} \DecValTok{2} \OperatorTok{/} \ConstantTok{R}\KeywordTok{[}\WarningTok{:z}\KeywordTok{]}
2849
3385
  \FunctionTok{puts}\NormalTok{ exp2}
2850
3386
  \end{Highlighting}
2851
3387
  \end{Shaded}
@@ -2858,7 +3394,7 @@ It is also possible to use inequality operators in building expressions
2858
3394
 
2859
3395
  \begin{Shaded}
2860
3396
  \begin{Highlighting}[]
2861
- \NormalTok{exp3 }\OperatorTok{=}\NormalTok{ (}\WarningTok{:a} \OperatorTok{+} \WarningTok{:b}\NormalTok{) }\OperatorTok{\textgreater{}=} \WarningTok{:z}
3397
+ \NormalTok{exp3 }\OperatorTok{=}\NormalTok{ (}\ConstantTok{R}\KeywordTok{[}\WarningTok{:a}\KeywordTok{]} \OperatorTok{+} \ConstantTok{R}\KeywordTok{[}\WarningTok{:b}\KeywordTok{]}\NormalTok{) }\OperatorTok{\textgreater{}=} \WarningTok{:z}
2862
3398
  \FunctionTok{puts}\NormalTok{ exp3}
2863
3399
  \end{Highlighting}
2864
3400
  \end{Shaded}
@@ -2874,7 +3410,7 @@ be written as
2874
3410
 
2875
3411
  \begin{Shaded}
2876
3412
  \begin{Highlighting}[]
2877
- \NormalTok{exp4 }\OperatorTok{=}\NormalTok{ (}\WarningTok{:a} \OperatorTok{+} \WarningTok{:b}\NormalTok{)}\AttributeTok{.ge} \OperatorTok{:}\NormalTok{z}
3413
+ \NormalTok{exp4 }\OperatorTok{=}\NormalTok{ (}\ConstantTok{R}\KeywordTok{[}\WarningTok{:a}\KeywordTok{]} \OperatorTok{+} \ConstantTok{R}\KeywordTok{[}\WarningTok{:b}\KeywordTok{]}\NormalTok{)}\AttributeTok{.ge} \OperatorTok{:}\NormalTok{z}
2878
3414
  \FunctionTok{puts}\NormalTok{ exp4}
2879
3415
  \end{Highlighting}
2880
3416
  \end{Shaded}
@@ -2890,7 +3426,7 @@ the method `.eq' and for `=' we need the function `.assign'
2890
3426
 
2891
3427
  \begin{Shaded}
2892
3428
  \begin{Highlighting}[]
2893
- \NormalTok{exp5 }\OperatorTok{=}\NormalTok{ (}\WarningTok{:a} \OperatorTok{+} \WarningTok{:b}\NormalTok{)}\AttributeTok{.eq} \OperatorTok{:}\NormalTok{z}
3429
+ \NormalTok{exp5 }\OperatorTok{=}\NormalTok{ (}\ConstantTok{R}\KeywordTok{[}\WarningTok{:a}\KeywordTok{]} \OperatorTok{+} \ConstantTok{R}\KeywordTok{[}\WarningTok{:b}\KeywordTok{]}\NormalTok{)}\AttributeTok{.eq} \OperatorTok{:}\NormalTok{z}
2894
3430
  \FunctionTok{puts}\NormalTok{ exp5}
2895
3431
  \end{Highlighting}
2896
3432
  \end{Shaded}
@@ -2901,7 +3437,7 @@ the method `.eq' and for `=' we need the function `.assign'
2901
3437
 
2902
3438
  \begin{Shaded}
2903
3439
  \begin{Highlighting}[]
2904
- \NormalTok{exp6 }\OperatorTok{=} \WarningTok{:y}\AttributeTok{.assign} \OperatorTok{:}\NormalTok{a }\OperatorTok{+} \WarningTok{:b}
3440
+ \NormalTok{exp6 }\OperatorTok{=} \ConstantTok{R}\KeywordTok{[}\WarningTok{:y}\KeywordTok{]}\AttributeTok{.assign} \ConstantTok{R}\KeywordTok{[}\WarningTok{:a}\KeywordTok{]} \OperatorTok{+} \ConstantTok{R}\KeywordTok{[}\WarningTok{:b}\KeywordTok{]}
2905
3441
  \FunctionTok{puts}\NormalTok{ exp6}
2906
3442
  \end{Highlighting}
2907
3443
  \end{Shaded}
@@ -2916,18 +3452,18 @@ expressions such as
2916
3452
 
2917
3453
  \begin{Shaded}
2918
3454
  \begin{Highlighting}[]
2919
- \NormalTok{exp\_wrong }\OperatorTok{=}\NormalTok{ (}\WarningTok{:a} \OperatorTok{+} \WarningTok{:b}\NormalTok{) }\OperatorTok{==} \WarningTok{:z}
3455
+ \NormalTok{exp\_wrong }\OperatorTok{=}\NormalTok{ (}\ConstantTok{R}\KeywordTok{[}\WarningTok{:a}\KeywordTok{]} \OperatorTok{+} \ConstantTok{R}\KeywordTok{[}\WarningTok{:b}\KeywordTok{]}\NormalTok{) }\OperatorTok{==} \WarningTok{:z}
2920
3456
  \FunctionTok{puts}\NormalTok{ exp\_wrong}
2921
3457
  \end{Highlighting}
2922
3458
  \end{Shaded}
2923
3459
 
2924
3460
  and it might be difficult to understand what is going on here. The
2925
3461
  problem lies with the fact that when using `==' we are comparing
2926
- expression (:a + :b) to expression :z with `=='. When the comparison is
2927
- executed, the system tries to evaluate :a, :b and :z, and those symbols
2928
- at this time are not bound to anything and we get a ``object `a' not
2929
- found'' message. If we only use functional notation, this type of error
2930
- will not occur.
3462
+ expression (R{[}:a{]} + R{[}:b{]}) to expression :z with `=='. When the
3463
+ comparison is executed, the system tries to evaluate :a, :b and :z, and
3464
+ those symbols at this time are not bound to anything and we get a
3465
+ ``object `a' not found'' message. If we only use functional notation,
3466
+ this type of error will not occur.
2931
3467
 
2932
3468
  \subsection{Expressions with R
2933
3469
  methods}\label{expressions-with-r-methods}
@@ -2943,7 +3479,7 @@ such as `E.sin(x)'
2943
3479
 
2944
3480
  \begin{Shaded}
2945
3481
  \begin{Highlighting}[]
2946
- \NormalTok{exp7 }\OperatorTok{=} \WarningTok{:y}\AttributeTok{.assign} \ConstantTok{E}\AttributeTok{.sin}\NormalTok{(}\WarningTok{:x}\NormalTok{)}
3482
+ \NormalTok{exp7 }\OperatorTok{=} \ConstantTok{R}\KeywordTok{[}\WarningTok{:y}\KeywordTok{]}\AttributeTok{.assign} \ConstantTok{E}\AttributeTok{.sin}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:x}\KeywordTok{]}\NormalTok{)}
2947
3483
  \FunctionTok{puts}\NormalTok{ exp7}
2948
3484
  \end{Highlighting}
2949
3485
  \end{Shaded}
@@ -2956,7 +3492,7 @@ Expressions can also be written using `.' notation:
2956
3492
 
2957
3493
  \begin{Shaded}
2958
3494
  \begin{Highlighting}[]
2959
- \NormalTok{exp8 }\OperatorTok{=} \WarningTok{:y}\AttributeTok{.assign} \OperatorTok{:}\NormalTok{x}\AttributeTok{.sin}
3495
+ \NormalTok{exp8 }\OperatorTok{=} \ConstantTok{R}\KeywordTok{[}\WarningTok{:y}\KeywordTok{]}\AttributeTok{.assign} \ConstantTok{R}\KeywordTok{[}\WarningTok{:x}\KeywordTok{]}\AttributeTok{.sin}
2960
3496
  \FunctionTok{puts}\NormalTok{ exp8}
2961
3497
  \end{Highlighting}
2962
3498
  \end{Shaded}
@@ -2970,7 +3506,7 @@ the `.':
2970
3506
 
2971
3507
  \begin{Shaded}
2972
3508
  \begin{Highlighting}[]
2973
- \NormalTok{exp9 }\OperatorTok{=} \WarningTok{:x}\AttributeTok{.c}\NormalTok{(}\WarningTok{:y}\NormalTok{)}
3509
+ \NormalTok{exp9 }\OperatorTok{=} \ConstantTok{R}\KeywordTok{[}\WarningTok{:x}\KeywordTok{]}\AttributeTok{.c}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:y}\KeywordTok{]}\NormalTok{)}
2974
3510
  \FunctionTok{puts}\NormalTok{ exp9}
2975
3511
  \end{Highlighting}
2976
3512
  \end{Shaded}
@@ -2986,7 +3522,7 @@ A binding can be provided with a list:
2986
3522
 
2987
3523
  \begin{Shaded}
2988
3524
  \begin{Highlighting}[]
2989
- \NormalTok{exp }\OperatorTok{=}\NormalTok{ (}\WarningTok{:a} \OperatorTok{+} \WarningTok{:b}\NormalTok{) }\OperatorTok{*} \FloatTok{2.0} \OperatorTok{+} \WarningTok{:c} \OperatorTok{**} \DecValTok{2} \OperatorTok{/} \WarningTok{:z}
3525
+ \NormalTok{exp }\OperatorTok{=}\NormalTok{ (}\ConstantTok{R}\KeywordTok{[}\WarningTok{:a}\KeywordTok{]} \OperatorTok{+} \ConstantTok{R}\KeywordTok{[}\WarningTok{:b}\KeywordTok{]}\NormalTok{) }\OperatorTok{*} \FloatTok{2.0} \OperatorTok{+} \ConstantTok{R}\KeywordTok{[}\WarningTok{:c}\KeywordTok{]} \OperatorTok{**} \DecValTok{2} \OperatorTok{/} \ConstantTok{R}\KeywordTok{[}\WarningTok{:z}\KeywordTok{]}
2990
3526
  \FunctionTok{puts}\NormalTok{ exp}\AttributeTok{.eval}\NormalTok{(}\ConstantTok{R}\AttributeTok{.list}\NormalTok{(}\WarningTok{a:} \DecValTok{10}\NormalTok{, }\WarningTok{b:} \DecValTok{20}\NormalTok{, }\WarningTok{c:} \DecValTok{30}\NormalTok{, }\WarningTok{z:} \DecValTok{40}\NormalTok{))}
2991
3527
  \end{Highlighting}
2992
3528
  \end{Shaded}
@@ -3043,51 +3579,53 @@ use \texttt{head} / \texttt{str} in R via \texttt{R} calls).
3043
3579
 
3044
3580
  \begin{Shaded}
3045
3581
  \begin{Highlighting}[]
3046
- \NormalTok{flights }\OperatorTok{=} \OperatorTok{\textasciitilde{}}\WarningTok{:flights}
3582
+ \NormalTok{flights }\OperatorTok{=} \OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:flights}\KeywordTok{]}
3047
3583
  \FunctionTok{puts}\NormalTok{ flights}\AttributeTok{.head}
3048
3584
  \end{Highlighting}
3049
3585
  \end{Shaded}
3050
3586
 
3051
3587
  \begin{verbatim}
3052
3588
  ## # A tibble: 6 x 19
3053
- ## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
3054
- ## <int> <int> <int> <int> <int> <dbl> <int> <int>
3055
- ## 1 2013 1 1 517 515 2 830 819
3056
- ## 2 2013 1 1 533 529 4 850 830
3057
- ## 3 2013 1 1 542 540 2 923 850
3058
- ## 4 2013 1 1 544 545 -1 1004 1022
3059
- ## 5 2013 1 1 554 600 -6 812 837
3060
- ## 6 2013 1 1 554 558 -4 740 728
3061
- ## # i 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
3062
- ## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
3063
- ## # hour <dbl>, minute <dbl>, time_hour <dttm>
3589
+ ## year month day dep_time sched_dep_time dep_delay arr_time
3590
+ ## <int> <int> <int> <int> <int> <dbl> <int>
3591
+ ## 1 2013 1 1 517 515 2 830
3592
+ ## 2 2013 1 1 533 529 4 850
3593
+ ## 3 2013 1 1 542 540 2 923
3594
+ ## 4 2013 1 1 544 545 -1 1004
3595
+ ## 5 2013 1 1 554 600 -6 812
3596
+ ## 6 2013 1 1 554 558 -4 740
3597
+ ## # i 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
3598
+ ## # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>,
3599
+ ## # dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
3600
+ ## # minute <dbl>, time_hour <dttm>
3064
3601
  \end{verbatim}
3065
3602
 
3066
3603
  \subsection{Filtering rows with
3067
3604
  Filter}\label{filtering-rows-with-filter}
3068
3605
 
3069
3606
  In this example we filter the flights data set by giving to the filter
3070
- function two expressions: the first :month.eq 1
3607
+ function two expressions: the first R{[}:month{]}.eq 1
3071
3608
 
3072
3609
  \begin{Shaded}
3073
3610
  \begin{Highlighting}[]
3074
- \FunctionTok{puts}\NormalTok{ flights}\AttributeTok{.filter}\NormalTok{((}\WarningTok{:month}\AttributeTok{.eq} \DecValTok{1}\NormalTok{), (}\WarningTok{:day}\AttributeTok{.eq} \DecValTok{1}\NormalTok{))}\AttributeTok{.head}
3611
+ \FunctionTok{puts}\NormalTok{ flights}\AttributeTok{.filter}\NormalTok{((}\ConstantTok{R}\KeywordTok{[}\WarningTok{:month}\KeywordTok{]}\AttributeTok{.eq} \DecValTok{1}\NormalTok{), (}\ConstantTok{R}\KeywordTok{[}\WarningTok{:day}\KeywordTok{]}\AttributeTok{.eq} \DecValTok{1}\NormalTok{))}\AttributeTok{.head}
3075
3612
  \end{Highlighting}
3076
3613
  \end{Shaded}
3077
3614
 
3078
3615
  \begin{verbatim}
3079
3616
  ## # A tibble: 6 x 19
3080
- ## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
3081
- ## <int> <int> <int> <int> <int> <dbl> <int> <int>
3082
- ## 1 2013 1 1 517 515 2 830 819
3083
- ## 2 2013 1 1 533 529 4 850 830
3084
- ## 3 2013 1 1 542 540 2 923 850
3085
- ## 4 2013 1 1 544 545 -1 1004 1022
3086
- ## 5 2013 1 1 554 600 -6 812 837
3087
- ## 6 2013 1 1 554 558 -4 740 728
3088
- ## # i 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
3089
- ## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
3090
- ## # hour <dbl>, minute <dbl>, time_hour <dttm>
3617
+ ## year month day dep_time sched_dep_time dep_delay arr_time
3618
+ ## <int> <int> <int> <int> <int> <dbl> <int>
3619
+ ## 1 2013 1 1 517 515 2 830
3620
+ ## 2 2013 1 1 533 529 4 850
3621
+ ## 3 2013 1 1 542 540 2 923
3622
+ ## 4 2013 1 1 544 545 -1 1004
3623
+ ## 5 2013 1 1 554 600 -6 812
3624
+ ## 6 2013 1 1 554 558 -4 740
3625
+ ## # i 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
3626
+ ## # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>,
3627
+ ## # dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
3628
+ ## # minute <dbl>, time_hour <dttm>
3091
3629
  \end{verbatim}
3092
3630
 
3093
3631
  \subsection{Logical Operators}\label{logical-operators}
@@ -3096,23 +3634,24 @@ All flights that departed in November of December
3096
3634
 
3097
3635
  \begin{Shaded}
3098
3636
  \begin{Highlighting}[]
3099
- \FunctionTok{puts}\NormalTok{ flights}\AttributeTok{.filter}\NormalTok{((}\WarningTok{:month}\AttributeTok{.eq} \DecValTok{11}\NormalTok{) }\OperatorTok{|}\NormalTok{ (}\WarningTok{:month}\AttributeTok{.eq} \DecValTok{12}\NormalTok{))}\AttributeTok{.head}
3637
+ \FunctionTok{puts}\NormalTok{ flights}\AttributeTok{.filter}\NormalTok{((}\ConstantTok{R}\KeywordTok{[}\WarningTok{:month}\KeywordTok{]}\AttributeTok{.eq} \DecValTok{11}\NormalTok{) }\OperatorTok{|}\NormalTok{ (}\ConstantTok{R}\KeywordTok{[}\WarningTok{:month}\KeywordTok{]}\AttributeTok{.eq} \DecValTok{12}\NormalTok{))}\AttributeTok{.head}
3100
3638
  \end{Highlighting}
3101
3639
  \end{Shaded}
3102
3640
 
3103
3641
  \begin{verbatim}
3104
3642
  ## # A tibble: 6 x 19
3105
- ## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
3106
- ## <int> <int> <int> <int> <int> <dbl> <int> <int>
3107
- ## 1 2013 11 1 5 2359 6 352 345
3108
- ## 2 2013 11 1 35 2250 105 123 2356
3109
- ## 3 2013 11 1 455 500 -5 641 651
3110
- ## 4 2013 11 1 539 545 -6 856 827
3111
- ## 5 2013 11 1 542 545 -3 831 855
3112
- ## 6 2013 11 1 549 600 -11 912 923
3113
- ## # i 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
3114
- ## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
3115
- ## # hour <dbl>, minute <dbl>, time_hour <dttm>
3643
+ ## year month day dep_time sched_dep_time dep_delay arr_time
3644
+ ## <int> <int> <int> <int> <int> <dbl> <int>
3645
+ ## 1 2013 11 1 5 2359 6 352
3646
+ ## 2 2013 11 1 35 2250 105 123
3647
+ ## 3 2013 11 1 455 500 -5 641
3648
+ ## 4 2013 11 1 539 545 -6 856
3649
+ ## 5 2013 11 1 542 545 -3 831
3650
+ ## 6 2013 11 1 549 600 -11 912
3651
+ ## # i 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
3652
+ ## # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>,
3653
+ ## # dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
3654
+ ## # minute <dbl>, time_hour <dttm>
3116
3655
  \end{verbatim}
3117
3656
 
3118
3657
  The same as above, but using the `in' operator. In R, it is possible to
@@ -3123,23 +3662,24 @@ in this case `:in' and the second argument is the vector:
3123
3662
 
3124
3663
  \begin{Shaded}
3125
3664
  \begin{Highlighting}[]
3126
- \FunctionTok{puts}\NormalTok{ flights}\AttributeTok{.filter}\NormalTok{(}\WarningTok{:month}\AttributeTok{.\_} \OperatorTok{:}\ControlFlowTok{in}\NormalTok{, }\ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\DecValTok{11}\NormalTok{, }\DecValTok{12}\NormalTok{))}\AttributeTok{.head}
3665
+ \FunctionTok{puts}\NormalTok{ flights}\AttributeTok{.filter}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:month}\KeywordTok{]}\AttributeTok{.\_} \OperatorTok{:}\ControlFlowTok{in}\NormalTok{, }\ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\DecValTok{11}\NormalTok{, }\DecValTok{12}\NormalTok{))}\AttributeTok{.head}
3127
3666
  \end{Highlighting}
3128
3667
  \end{Shaded}
3129
3668
 
3130
3669
  \begin{verbatim}
3131
3670
  ## # A tibble: 6 x 19
3132
- ## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
3133
- ## <int> <int> <int> <int> <int> <dbl> <int> <int>
3134
- ## 1 2013 11 1 5 2359 6 352 345
3135
- ## 2 2013 11 1 35 2250 105 123 2356
3136
- ## 3 2013 11 1 455 500 -5 641 651
3137
- ## 4 2013 11 1 539 545 -6 856 827
3138
- ## 5 2013 11 1 542 545 -3 831 855
3139
- ## 6 2013 11 1 549 600 -11 912 923
3140
- ## # i 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
3141
- ## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
3142
- ## # hour <dbl>, minute <dbl>, time_hour <dttm>
3671
+ ## year month day dep_time sched_dep_time dep_delay arr_time
3672
+ ## <int> <int> <int> <int> <int> <dbl> <int>
3673
+ ## 1 2013 11 1 5 2359 6 352
3674
+ ## 2 2013 11 1 35 2250 105 123
3675
+ ## 3 2013 11 1 455 500 -5 641
3676
+ ## 4 2013 11 1 539 545 -6 856
3677
+ ## 5 2013 11 1 542 545 -3 831
3678
+ ## 6 2013 11 1 549 600 -11 912
3679
+ ## # i 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
3680
+ ## # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>,
3681
+ ## # dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
3682
+ ## # minute <dbl>, time_hour <dttm>
3143
3683
  \end{verbatim}
3144
3684
 
3145
3685
  \subsection{Filtering with NA (Not
@@ -3172,7 +3712,7 @@ condition, where the row with R:NA does not.
3172
3712
 
3173
3713
  \begin{Shaded}
3174
3714
  \begin{Highlighting}[]
3175
- \FunctionTok{puts}\NormalTok{ df}\AttributeTok{.filter}\NormalTok{(}\WarningTok{:x} \OperatorTok{\textgreater{}} \DecValTok{1}\NormalTok{)}
3715
+ \FunctionTok{puts}\NormalTok{ df}\AttributeTok{.filter}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:x}\KeywordTok{]} \OperatorTok{\textgreater{}} \DecValTok{1}\NormalTok{)}
3176
3716
  \end{Highlighting}
3177
3717
  \end{Shaded}
3178
3718
 
@@ -3187,7 +3727,7 @@ To match an NA use method 'is\_\_na'
3187
3727
 
3188
3728
  \begin{Shaded}
3189
3729
  \begin{Highlighting}[]
3190
- \FunctionTok{puts}\NormalTok{ df}\AttributeTok{.filter}\NormalTok{((}\WarningTok{:x}\AttributeTok{.is\_\_na}\NormalTok{) }\OperatorTok{|}\NormalTok{ (}\WarningTok{:x} \OperatorTok{\textgreater{}} \DecValTok{1}\NormalTok{))}
3730
+ \FunctionTok{puts}\NormalTok{ df}\AttributeTok{.filter}\NormalTok{((}\ConstantTok{R}\KeywordTok{[}\WarningTok{:x}\KeywordTok{]}\AttributeTok{.is\_\_na}\NormalTok{) }\OperatorTok{|}\NormalTok{ (}\ConstantTok{R}\KeywordTok{[}\WarningTok{:x}\KeywordTok{]} \OperatorTok{\textgreater{}} \DecValTok{1}\NormalTok{))}
3191
3731
  \end{Highlighting}
3192
3732
  \end{Shaded}
3193
3733
 
@@ -3211,40 +3751,42 @@ Arrange reorders the rows of a data frame by the given arguments.
3211
3751
 
3212
3752
  \begin{verbatim}
3213
3753
  ## # A tibble: 6 x 19
3214
- ## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
3215
- ## <int> <int> <int> <int> <int> <dbl> <int> <int>
3216
- ## 1 2013 1 1 517 515 2 830 819
3217
- ## 2 2013 1 1 533 529 4 850 830
3218
- ## 3 2013 1 1 542 540 2 923 850
3219
- ## 4 2013 1 1 544 545 -1 1004 1022
3220
- ## 5 2013 1 1 554 600 -6 812 837
3221
- ## 6 2013 1 1 554 558 -4 740 728
3222
- ## # i 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
3223
- ## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
3224
- ## # hour <dbl>, minute <dbl>, time_hour <dttm>
3754
+ ## year month day dep_time sched_dep_time dep_delay arr_time
3755
+ ## <int> <int> <int> <int> <int> <dbl> <int>
3756
+ ## 1 2013 1 1 517 515 2 830
3757
+ ## 2 2013 1 1 533 529 4 850
3758
+ ## 3 2013 1 1 542 540 2 923
3759
+ ## 4 2013 1 1 544 545 -1 1004
3760
+ ## 5 2013 1 1 554 600 -6 812
3761
+ ## 6 2013 1 1 554 558 -4 740
3762
+ ## # i 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
3763
+ ## # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>,
3764
+ ## # dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
3765
+ ## # minute <dbl>, time_hour <dttm>
3225
3766
  \end{verbatim}
3226
3767
 
3227
3768
  To arrange in descending order, use function `desc'
3228
3769
 
3229
3770
  \begin{Shaded}
3230
3771
  \begin{Highlighting}[]
3231
- \FunctionTok{puts}\NormalTok{ flights}\AttributeTok{.arrange}\NormalTok{(}\WarningTok{:dep\_delay}\AttributeTok{.desc}\NormalTok{)}\AttributeTok{.head}
3772
+ \FunctionTok{puts}\NormalTok{ flights}\AttributeTok{.arrange}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:dep\_delay}\KeywordTok{]}\AttributeTok{.desc}\NormalTok{)}\AttributeTok{.head}
3232
3773
  \end{Highlighting}
3233
3774
  \end{Shaded}
3234
3775
 
3235
3776
  \begin{verbatim}
3236
3777
  ## # A tibble: 6 x 19
3237
- ## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
3238
- ## <int> <int> <int> <int> <int> <dbl> <int> <int>
3239
- ## 1 2013 1 9 641 900 1301 1242 1530
3240
- ## 2 2013 6 15 1432 1935 1137 1607 2120
3241
- ## 3 2013 1 10 1121 1635 1126 1239 1810
3242
- ## 4 2013 9 20 1139 1845 1014 1457 2210
3243
- ## 5 2013 7 22 845 1600 1005 1044 1815
3244
- ## 6 2013 4 10 1100 1900 960 1342 2211
3245
- ## # i 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
3246
- ## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
3247
- ## # hour <dbl>, minute <dbl>, time_hour <dttm>
3778
+ ## year month day dep_time sched_dep_time dep_delay arr_time
3779
+ ## <int> <int> <int> <int> <int> <dbl> <int>
3780
+ ## 1 2013 1 9 641 900 1301 1242
3781
+ ## 2 2013 6 15 1432 1935 1137 1607
3782
+ ## 3 2013 1 10 1121 1635 1126 1239
3783
+ ## 4 2013 9 20 1139 1845 1014 1457
3784
+ ## 5 2013 7 22 845 1600 1005 1044
3785
+ ## 6 2013 4 10 1100 1900 960 1342
3786
+ ## # i 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
3787
+ ## # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>,
3788
+ ## # dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
3789
+ ## # minute <dbl>, time_hour <dttm>
3248
3790
  \end{verbatim}
3249
3791
 
3250
3792
  \subsection{Selecting columns}\label{selecting-columns}
@@ -3273,7 +3815,7 @@ It is also possible to select column in a given range
3273
3815
 
3274
3816
  \begin{Shaded}
3275
3817
  \begin{Highlighting}[]
3276
- \FunctionTok{puts}\NormalTok{ flights}\AttributeTok{.select}\NormalTok{(}\WarningTok{:year}\AttributeTok{.up\_to} \OperatorTok{:}\NormalTok{day)}\AttributeTok{.head}
3818
+ \FunctionTok{puts}\NormalTok{ flights}\AttributeTok{.select}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:year}\KeywordTok{]}\AttributeTok{.up\_to}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:day}\KeywordTok{]}\NormalTok{))}\AttributeTok{.head}
3277
3819
  \end{Highlighting}
3278
3820
  \end{Shaded}
3279
3821
 
@@ -3335,17 +3877,18 @@ column order is `Everything':
3335
3877
 
3336
3878
  \begin{verbatim}
3337
3879
  ## # A tibble: 6 x 19
3338
- ## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
3339
- ## <int> <int> <int> <int> <int> <dbl> <int> <int>
3340
- ## 1 2013 1 1 517 515 2 830 819
3341
- ## 2 2013 1 1 533 529 4 850 830
3342
- ## 3 2013 1 1 542 540 2 923 850
3343
- ## 4 2013 1 1 544 545 -1 1004 1022
3344
- ## 5 2013 1 1 554 600 -6 812 837
3345
- ## 6 2013 1 1 554 558 -4 740 728
3346
- ## # i 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
3347
- ## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
3348
- ## # hour <dbl>, minute <dbl>, time_hour <dttm>
3880
+ ## year month day dep_time sched_dep_time dep_delay arr_time
3881
+ ## <int> <int> <int> <int> <int> <dbl> <int>
3882
+ ## 1 2013 1 1 517 515 2 830
3883
+ ## 2 2013 1 1 533 529 4 850
3884
+ ## 3 2013 1 1 542 540 2 923
3885
+ ## 4 2013 1 1 544 545 -1 1004
3886
+ ## 5 2013 1 1 554 600 -6 812
3887
+ ## 6 2013 1 1 554 558 -4 740
3888
+ ## # i 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
3889
+ ## # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>,
3890
+ ## # dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
3891
+ ## # minute <dbl>, time_hour <dttm>
3349
3892
  \end{verbatim}
3350
3893
 
3351
3894
  \subsection{Add variables to a dataframe with
@@ -3354,7 +3897,7 @@ column order is `Everything':
3354
3897
  \begin{Shaded}
3355
3898
  \begin{Highlighting}[]
3356
3899
  \NormalTok{flights\_sm }\OperatorTok{=}\NormalTok{ flights}\AttributeTok{.}
3357
- \AttributeTok{select}\NormalTok{((}\WarningTok{:year}\AttributeTok{.up\_to} \OperatorTok{:}\NormalTok{day),}
3900
+ \AttributeTok{select}\NormalTok{((}\ConstantTok{R}\KeywordTok{[}\WarningTok{:year}\KeywordTok{]}\AttributeTok{.up\_to}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:day}\KeywordTok{]}\NormalTok{)),}
3358
3901
  \ConstantTok{E}\AttributeTok{.ends\_with}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}delay\textquotesingle{}}\NormalTok{),}
3359
3902
  \WarningTok{:distance}\NormalTok{,}
3360
3903
  \WarningTok{:air\_time}\NormalTok{)}
@@ -3378,8 +3921,8 @@ column order is `Everything':
3378
3921
  \begin{Shaded}
3379
3922
  \begin{Highlighting}[]
3380
3923
  \NormalTok{flights\_sm }\OperatorTok{=}\NormalTok{ flights\_sm}\AttributeTok{.}
3381
- \AttributeTok{mutate}\NormalTok{(}\WarningTok{gain:} \WarningTok{:dep\_delay} \OperatorTok{{-}} \WarningTok{:arr\_delay}\NormalTok{,}
3382
- \WarningTok{speed:} \WarningTok{:distance} \OperatorTok{/} \WarningTok{:air\_time} \OperatorTok{*} \DecValTok{60}\NormalTok{)}
3924
+ \AttributeTok{mutate}\NormalTok{(}\WarningTok{gain:} \ConstantTok{R}\KeywordTok{[}\WarningTok{:dep\_delay}\KeywordTok{]} \OperatorTok{{-}} \ConstantTok{R}\KeywordTok{[}\WarningTok{:arr\_delay}\KeywordTok{]}\NormalTok{,}
3925
+ \WarningTok{speed:} \ConstantTok{R}\KeywordTok{[}\WarningTok{:distance}\KeywordTok{]} \OperatorTok{/} \ConstantTok{R}\KeywordTok{[}\WarningTok{:air\_time}\KeywordTok{]} \OperatorTok{*} \DecValTok{60}\NormalTok{)}
3383
3926
  \FunctionTok{puts}\NormalTok{ flights\_sm}\AttributeTok{.head}
3384
3927
  \end{Highlighting}
3385
3928
  \end{Shaded}
@@ -3420,7 +3963,7 @@ given group:
3420
3963
  \begin{Shaded}
3421
3964
  \begin{Highlighting}[]
3422
3965
  \NormalTok{by\_day }\OperatorTok{=}\NormalTok{ flights}\AttributeTok{.group\_by}\NormalTok{(}\WarningTok{:year}\NormalTok{, }\WarningTok{:month}\NormalTok{, }\WarningTok{:day}\NormalTok{)}
3423
- \FunctionTok{puts}\NormalTok{ by\_day}\AttributeTok{.summarise}\NormalTok{(}\WarningTok{delay:} \WarningTok{:dep\_delay}\AttributeTok{.mean}\NormalTok{(}\WarningTok{na\_\_rm:} \DecValTok{true}\NormalTok{))}\AttributeTok{.head}
3966
+ \FunctionTok{puts}\NormalTok{ by\_day}\AttributeTok{.summarise}\NormalTok{(}\WarningTok{delay:} \ConstantTok{R}\KeywordTok{[}\WarningTok{:dep\_delay}\KeywordTok{]}\AttributeTok{.mean}\NormalTok{(}\WarningTok{na\_\_rm:} \DecValTok{true}\NormalTok{))}\AttributeTok{.head}
3424
3967
  \end{Highlighting}
3425
3968
  \end{Shaded}
3426
3969
 
@@ -3446,9 +3989,9 @@ other:
3446
3989
  \AttributeTok{group\_by}\NormalTok{(}\WarningTok{:dest}\NormalTok{)}\AttributeTok{.}
3447
3990
  \AttributeTok{summarise}\NormalTok{(}
3448
3991
  \WarningTok{count:} \ConstantTok{E}\AttributeTok{.n}\NormalTok{,}
3449
- \WarningTok{dist:} \WarningTok{:distance}\AttributeTok{.mean}\NormalTok{(}\WarningTok{na\_\_rm:} \DecValTok{true}\NormalTok{),}
3450
- \WarningTok{delay:} \WarningTok{:arr\_delay}\AttributeTok{.mean}\NormalTok{(}\WarningTok{na\_\_rm:} \DecValTok{true}\NormalTok{))}\AttributeTok{.}
3451
- \AttributeTok{filter}\NormalTok{(}\WarningTok{:count} \OperatorTok{\textgreater{}} \DecValTok{20}\NormalTok{, }\WarningTok{:dest} \OperatorTok{!=} \StringTok{"NHL"}\NormalTok{)}
3992
+ \WarningTok{dist:} \ConstantTok{R}\KeywordTok{[}\WarningTok{:distance}\KeywordTok{]}\AttributeTok{.mean}\NormalTok{(}\WarningTok{na\_\_rm:} \DecValTok{true}\NormalTok{),}
3993
+ \WarningTok{delay:} \ConstantTok{R}\KeywordTok{[}\WarningTok{:arr\_delay}\KeywordTok{]}\AttributeTok{.mean}\NormalTok{(}\WarningTok{na\_\_rm:} \DecValTok{true}\NormalTok{))}\AttributeTok{.}
3994
+ \AttributeTok{filter}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:count}\KeywordTok{]} \OperatorTok{\textgreater{}} \DecValTok{20}\NormalTok{, }\ConstantTok{R}\KeywordTok{[}\WarningTok{:dest}\KeywordTok{]} \OperatorTok{!=} \StringTok{"NHL"}\NormalTok{)}
3452
3995
 
3453
3996
  \FunctionTok{puts}\NormalTok{ delays}\AttributeTok{.head}
3454
3997
  \end{Highlighting}
@@ -3468,77 +4011,66 @@ other:
3468
4011
 
3469
4012
  \section{Using Data Table}\label{using-data-table}
3470
4013
 
3471
- The next chunk reads the \textbf{flights14} sample (same file as the
3472
- data.table vignette). Prefer \textbf{downloading in Ruby} and
3473
- \textbf{\texttt{R.fread} on a local path}:
3474
- \texttt{data.table::fread("https://…")} runs the HTTP transfer
3475
- \textbf{inside R} while the Galaaz bridge waits on a \textbf{single}
3476
- eval; if the remote server stalls, that looks like an intermittent
3477
- ``hang'' and eventually hits the \textbf{per-eval} bridge timeout.
3478
- Ruby's \textbf{\texttt{Net::HTTP}} \texttt{open\_timeout} /
3479
- \texttt{read\_timeout} fail fast with a clear error instead. The chunk
3480
- passes \textbf{\texttt{nrows:}} to \textbf{\texttt{fread}} so a single
3481
- bridge eval does not have to materialize the full
3482
- \textasciitilde1.2M-row table during a gknit run.
4014
+ The next chunk converts the \textbf{nycflights13} \texttt{flights}
4015
+ tibble already loaded above into a \textbf{\texttt{data.table}}. That
4016
+ keeps the manual offline and avoids downloading a remote CSV during
4017
+ gknit (network stalls look like bridge hangs when the transfer runs
4018
+ inside a single R eval).
3483
4019
 
3484
4020
  \begin{Shaded}
3485
4021
  \begin{Highlighting}[]
3486
- \FunctionTok{require} \VerbatimStringTok{\textquotesingle{}net/http\textquotesingle{}}
3487
- \FunctionTok{require} \VerbatimStringTok{\textquotesingle{}uri\textquotesingle{}}
3488
- \FunctionTok{require} \VerbatimStringTok{\textquotesingle{}tmpdir\textquotesingle{}}
3489
-
3490
4022
  \ConstantTok{R}\AttributeTok{.library}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}data.table\textquotesingle{}}\NormalTok{)}
3491
4023
 
3492
- \NormalTok{url }\OperatorTok{=} \ConstantTok{URI}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}https://raw.githubusercontent.com/Rdatatable/data.table/master/vignettes/flights14.csv\textquotesingle{}}\NormalTok{)}
3493
- \NormalTok{csv }\OperatorTok{=} \DataTypeTok{File}\AttributeTok{.join}\NormalTok{(}\DataTypeTok{Dir}\AttributeTok{.tmpdir}\NormalTok{, }\StringTok{"galaaz\_manual\_flights14\_}\SpecialCharTok{\#\{}\DataTypeTok{Process}\AttributeTok{.pid}\SpecialCharTok{\}}\StringTok{.csv"}\NormalTok{)}
3494
- \ControlFlowTok{begin}
3495
- \DataTypeTok{Net}\OperatorTok{::}\ConstantTok{HTTP}\AttributeTok{.start}\NormalTok{(url}\AttributeTok{.host}\NormalTok{, url}\AttributeTok{.port}\NormalTok{, }\WarningTok{use\_ssl:} \DecValTok{true}\NormalTok{, }\WarningTok{open\_timeout:} \DecValTok{25}\NormalTok{, }\WarningTok{read\_timeout:} \DecValTok{120}\NormalTok{) }\ControlFlowTok{do} \OperatorTok{|}\NormalTok{http}\OperatorTok{|}
3496
- \NormalTok{ resp }\OperatorTok{=}\NormalTok{ http}\AttributeTok{.request\_get}\NormalTok{(url}\AttributeTok{.request\_uri}\NormalTok{)}
3497
- \FunctionTok{raise} \StringTok{"HTTP }\SpecialCharTok{\#\{}\NormalTok{resp}\AttributeTok{.code}\SpecialCharTok{\}}\StringTok{"} \ControlFlowTok{unless}\NormalTok{ resp}\AttributeTok{.is\_a?}\NormalTok{(}\DataTypeTok{Net}\OperatorTok{::}\DataTypeTok{HTTPSuccess}\NormalTok{)}
3498
-
3499
- \DataTypeTok{File}\AttributeTok{.binwrite}\NormalTok{(csv, resp}\AttributeTok{.body}\NormalTok{)}
3500
- \ControlFlowTok{end}
3501
-
3502
- \CommentTok{\# Cap rows so one bridge eval stays short on slow disks (full file is \textasciitilde{}1.2M rows).}
3503
- \NormalTok{ flights }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.fread}\NormalTok{(csv, }\WarningTok{nrows:} \DecValTok{120\_000}\NormalTok{)}
3504
- \FunctionTok{puts}\NormalTok{ flights}\AttributeTok{.dim}
3505
- \FunctionTok{puts} \ConstantTok{R}\AttributeTok{.head}\NormalTok{(flights, }\DecValTok{12}\NormalTok{)}
3506
- \ControlFlowTok{ensure}
3507
- \DataTypeTok{File}\AttributeTok{.unlink}\NormalTok{(csv) }\ControlFlowTok{if}\NormalTok{ csv }\OperatorTok{\&\&} \DataTypeTok{File}\AttributeTok{.exist?}\NormalTok{(csv)}
3508
- \ControlFlowTok{end}
3509
- \end{Highlighting}
3510
- \end{Shaded}
3511
-
3512
- \begin{verbatim}
3513
- ## [1] 120000 11
3514
- ## year month day dep_delay arr_delay carrier origin dest air_time
3515
- ## <int> <int> <int> <int> <int> <char> <char> <char> <int>
3516
- ## 1: 2014 1 1 14 13 AA JFK LAX 359
3517
- ## 2: 2014 1 1 -3 13 AA JFK LAX 363
3518
- ## 3: 2014 1 1 2 9 AA JFK LAX 351
3519
- ## 4: 2014 1 1 -8 -26 AA LGA PBI 157
3520
- ## 5: 2014 1 1 2 1 AA JFK LAX 350
3521
- ## 6: 2014 1 1 4 0 AA EWR LAX 339
3522
- ## 7: 2014 1 1 -2 -18 AA JFK LAX 338
3523
- ## 8: 2014 1 1 -3 -14 AA JFK LAX 356
3524
- ## 9: 2014 1 1 -1 -17 AA JFK MIA 161
3525
- ## 10: 2014 1 1 -2 -14 AA JFK SEA 349
3526
- ## 11: 2014 1 1 -5 -17 AA EWR MIA 161
3527
- ## 12: 2014 1 1 7 -5 AA JFK SFO 365
3528
- ## distance hour
3529
- ## <int> <int>
3530
- ## 1: 2475 9
3531
- ## 2: 2475 11
3532
- ## 3: 2475 19
3533
- ## 4: 1035 7
3534
- ## 5: 2475 13
3535
- ## 6: 2454 18
3536
- ## 7: 2475 21
3537
- ## 8: 2475 15
3538
- ## 9: 1089 15
3539
- ## 10: 2422 18
3540
- ## 11: 1085 16
3541
- ## 12: 2586 17
4024
+ \NormalTok{flights }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.as\_\_data\_\_table}\NormalTok{(}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:flights}\KeywordTok{]}\NormalTok{)}
4025
+ \FunctionTok{puts}\NormalTok{ flights}\AttributeTok{.dim}
4026
+ \FunctionTok{puts} \ConstantTok{R}\AttributeTok{.head}\NormalTok{(flights, }\DecValTok{12}\NormalTok{)}
4027
+ \end{Highlighting}
4028
+ \end{Shaded}
4029
+
4030
+ \begin{verbatim}
4031
+ ## [1] 336776 19
4032
+ ## year month day dep_time sched_dep_time dep_delay arr_time
4033
+ ## <int> <int> <int> <int> <int> <num> <int>
4034
+ ## 1: 2013 1 1 517 515 2 830
4035
+ ## 2: 2013 1 1 533 529 4 850
4036
+ ## 3: 2013 1 1 542 540 2 923
4037
+ ## 4: 2013 1 1 544 545 -1 1004
4038
+ ## 5: 2013 1 1 554 600 -6 812
4039
+ ## 6: 2013 1 1 554 558 -4 740
4040
+ ## 7: 2013 1 1 555 600 -5 913
4041
+ ## 8: 2013 1 1 557 600 -3 709
4042
+ ## 9: 2013 1 1 557 600 -3 838
4043
+ ## 10: 2013 1 1 558 600 -2 753
4044
+ ## 11: 2013 1 1 558 600 -2 849
4045
+ ## 12: 2013 1 1 558 600 -2 853
4046
+ ## sched_arr_time arr_delay carrier flight tailnum origin dest
4047
+ ## <int> <num> <char> <int> <char> <char> <char>
4048
+ ## 1: 819 11 UA 1545 N14228 EWR IAH
4049
+ ## 2: 830 20 UA 1714 N24211 LGA IAH
4050
+ ## 3: 850 33 AA 1141 N619AA JFK MIA
4051
+ ## 4: 1022 -18 B6 725 N804JB JFK BQN
4052
+ ## 5: 837 -25 DL 461 N668DN LGA ATL
4053
+ ## 6: 728 12 UA 1696 N39463 EWR ORD
4054
+ ## 7: 854 19 B6 507 N516JB EWR FLL
4055
+ ## 8: 723 -14 EV 5708 N829AS LGA IAD
4056
+ ## 9: 846 -8 B6 79 N593JB JFK MCO
4057
+ ## 10: 745 8 AA 301 N3ALAA LGA ORD
4058
+ ## 11: 851 -2 B6 49 N793JB JFK PBI
4059
+ ## 12: 856 -3 B6 71 N657JB JFK TPA
4060
+ ## air_time distance hour minute time_hour
4061
+ ## <num> <num> <num> <num> <POSc>
4062
+ ## 1: 227 1400 5 15 2013-01-01 05:00:00
4063
+ ## 2: 227 1416 5 29 2013-01-01 05:00:00
4064
+ ## 3: 160 1089 5 40 2013-01-01 05:00:00
4065
+ ## 4: 183 1576 5 45 2013-01-01 05:00:00
4066
+ ## 5: 116 762 6 0 2013-01-01 06:00:00
4067
+ ## 6: 150 719 5 58 2013-01-01 05:00:00
4068
+ ## 7: 158 1065 6 0 2013-01-01 06:00:00
4069
+ ## 8: 53 229 6 0 2013-01-01 06:00:00
4070
+ ## 9: 140 944 6 0 2013-01-01 06:00:00
4071
+ ## 10: 138 733 6 0 2013-01-01 06:00:00
4072
+ ## 11: 149 1028 6 0 2013-01-01 06:00:00
4073
+ ## 12: 158 1005 6 0 2013-01-01 06:00:00
3542
4074
  \end{verbatim}
3543
4075
 
3544
4076
  \begin{Shaded}
@@ -3570,7 +4102,7 @@ bridge eval does not have to materialize the full
3570
4102
  \begin{Shaded}
3571
4103
  \begin{Highlighting}[]
3572
4104
  \CommentTok{\# subset rows in i}
3573
- \NormalTok{ans }\OperatorTok{=}\NormalTok{ flights}\KeywordTok{[}\NormalTok{(}\WarningTok{:origin}\AttributeTok{.eq} \StringTok{"JFK"}\NormalTok{) }\OperatorTok{\&}\NormalTok{ (}\WarningTok{:month}\AttributeTok{.eq} \DecValTok{6}\NormalTok{)}\KeywordTok{]}
4105
+ \NormalTok{ans }\OperatorTok{=}\NormalTok{ flights}\KeywordTok{[}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:origin}\KeywordTok{]}\AttributeTok{.eq} \StringTok{"JFK"}\NormalTok{) }\OperatorTok{\&}\NormalTok{ (}\ConstantTok{R}\KeywordTok{[}\WarningTok{:month}\KeywordTok{]}\AttributeTok{.eq} \DecValTok{6}\NormalTok{)}\KeywordTok{]}
3574
4106
  \FunctionTok{puts}\NormalTok{ ans}\AttributeTok{.head}
3575
4107
 
3576
4108
  \CommentTok{\# Get the first two rows from flights.}
@@ -3585,15 +4117,42 @@ bridge eval does not have to materialize the full
3585
4117
  \end{Shaded}
3586
4118
 
3587
4119
  \begin{verbatim}
3588
- ## Empty data.table (0 rows and 11 cols): year,month,day,dep_delay,arr_delay,carrier...
3589
- ## year month day dep_delay arr_delay carrier origin dest air_time
3590
- ## <int> <int> <int> <int> <int> <char> <char> <char> <int>
3591
- ## 1: 2014 1 1 14 13 AA JFK LAX 359
3592
- ## 2: 2014 1 1 -3 13 AA JFK LAX 363
3593
- ## distance hour
3594
- ## <int> <int>
3595
- ## 1: 2475 9
3596
- ## 2: 2475 11
4120
+ ## year month day dep_time sched_dep_time dep_delay arr_time
4121
+ ## <int> <int> <int> <int> <int> <num> <int>
4122
+ ## 1: 2013 6 1 2 2359 3 341
4123
+ ## 2: 2013 6 1 538 545 -7 925
4124
+ ## 3: 2013 6 1 539 540 -1 832
4125
+ ## 4: 2013 6 1 553 600 -7 700
4126
+ ## 5: 2013 6 1 554 600 -6 851
4127
+ ## 6: 2013 6 1 557 600 -3 934
4128
+ ## sched_arr_time arr_delay carrier flight tailnum origin dest
4129
+ ## <int> <num> <char> <int> <char> <char> <char>
4130
+ ## 1: 350 -9 B6 739 N618JB JFK PSE
4131
+ ## 2: 922 3 B6 725 N806JB JFK BQN
4132
+ ## 3: 840 -8 AA 701 N5EAAA JFK MIA
4133
+ ## 4: 711 -11 EV 5716 N835AS JFK IAD
4134
+ ## 5: 908 -17 UA 1159 N33132 JFK LAX
4135
+ ## 6: 942 -8 B6 715 N766JB JFK SJU
4136
+ ## air_time distance hour minute time_hour
4137
+ ## <num> <num> <num> <num> <POSc>
4138
+ ## 1: 200 1617 23 59 2013-06-01 23:00:00
4139
+ ## 2: 203 1576 5 45 2013-06-01 05:00:00
4140
+ ## 3: 140 1089 5 40 2013-06-01 05:00:00
4141
+ ## 4: 42 228 6 0 2013-06-01 06:00:00
4142
+ ## 5: 330 2475 6 0 2013-06-01 06:00:00
4143
+ ## 6: 198 1598 6 0 2013-06-01 06:00:00
4144
+ ## year month day dep_time sched_dep_time dep_delay arr_time
4145
+ ## <int> <int> <int> <int> <int> <num> <int>
4146
+ ## 1: 2013 1 1 517 515 2 830
4147
+ ## 2: 2013 1 1 533 529 4 850
4148
+ ## sched_arr_time arr_delay carrier flight tailnum origin dest
4149
+ ## <int> <num> <char> <int> <char> <char> <char>
4150
+ ## 1: 819 11 UA 1545 N14228 EWR IAH
4151
+ ## 2: 830 20 UA 1714 N24211 LGA IAH
4152
+ ## air_time distance hour minute time_hour
4153
+ ## <num> <num> <num> <num> <POSc>
4154
+ ## 1: 227 1400 5 15 2013-01-01 05:00:00
4155
+ ## 2: 227 1416 5 29 2013-01-01 05:00:00
3597
4156
  \end{verbatim}
3598
4157
 
3599
4158
  \begin{Shaded}
@@ -3606,23 +4165,23 @@ bridge eval does not have to materialize the full
3606
4165
 
3607
4166
  \CommentTok{\# arr\_delay as data.table (not plain vector).}
3608
4167
 
3609
- \NormalTok{ans }\OperatorTok{=}\NormalTok{ flights}\KeywordTok{[}\WarningTok{:all}\NormalTok{, }\WarningTok{:arr\_delay}\AttributeTok{.list}\KeywordTok{]}
4168
+ \NormalTok{ans }\OperatorTok{=}\NormalTok{ flights}\KeywordTok{[}\WarningTok{:all}\NormalTok{, }\ConstantTok{R}\KeywordTok{[}\WarningTok{:arr\_delay}\KeywordTok{]}\AttributeTok{.list}\KeywordTok{]}
3610
4169
  \FunctionTok{puts}\NormalTok{ ans}\AttributeTok{.head}
3611
4170
 
3612
- \NormalTok{ans }\OperatorTok{=}\NormalTok{ flights}\KeywordTok{[}\WarningTok{:all}\NormalTok{, }\ConstantTok{E}\AttributeTok{.list}\NormalTok{(}\WarningTok{:arr\_delay}\NormalTok{, }\WarningTok{:dep\_delay}\NormalTok{)}\KeywordTok{]}
4171
+ \NormalTok{ans }\OperatorTok{=}\NormalTok{ flights}\KeywordTok{[}\WarningTok{:all}\NormalTok{, }\ConstantTok{E}\AttributeTok{.list}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:arr\_delay}\KeywordTok{]}\NormalTok{, }\ConstantTok{R}\KeywordTok{[}\WarningTok{:dep\_delay}\KeywordTok{]}\NormalTok{)}\KeywordTok{]}
3613
4172
  \end{Highlighting}
3614
4173
  \end{Shaded}
3615
4174
 
3616
4175
  \begin{verbatim}
3617
- ## [1] 13 13 9 -26 1 0
4176
+ ## [1] 11 20 33 -18 -25 12
3618
4177
  ## arr_delay
3619
- ## <int>
3620
- ## 1: 13
3621
- ## 2: 13
3622
- ## 3: 9
3623
- ## 4: -26
3624
- ## 5: 1
3625
- ## 6: 0
4178
+ ## <num>
4179
+ ## 1: 11
4180
+ ## 2: 20
4181
+ ## 3: 33
4182
+ ## 4: -18
4183
+ ## 5: -25
4184
+ ## 6: 12
3626
4185
  \end{verbatim}
3627
4186
 
3628
4187
  \section{Apache Arrow}\label{apache-arrow}
@@ -3639,10 +4198,10 @@ ordinary Ruby structures (arrays of row hashes), and
3639
4198
  \textbf{\texttt{collect()}} to materialize a tibble when you need
3640
4199
  in-memory R rows.
3641
4200
 
3642
- That pattern matches production use: \textbf{JRuby threads} (or
3643
- sequential code) assemble many rows in Ruby; you pay \textbf{one}
3644
- bridge-heavy handoff to R; \textbf{dplyr} runs vectorised work on the
3645
- Arrow table in R.
4201
+ That pattern matches production use: \textbf{JRuby threads} or a
4202
+ \textbf{multi-process CRuby} app (or sequential code) assemble many rows
4203
+ in Ruby; you pay \textbf{one} bridge-heavy handoff to R; \textbf{dplyr}
4204
+ runs vectorised work on the Arrow table in R.
3646
4205
 
3647
4206
  \textbf{Prerequisites:} install R packages \textbf{\texttt{arrow}} and
3648
4207
  \textbf{\texttt{dplyr}}. Run scripts with
@@ -3681,8 +4240,12 @@ can knit quickly; increase \texttt{thread\_count} and
3681
4240
  \begin{Shaded}
3682
4241
  \begin{Highlighting}[]
3683
4242
  \CommentTok{\# Scaled{-}down version of slow{-}specs/arrow\_large\_pipeline\_spec.rb.}
3684
- \ControlFlowTok{unless} \ConstantTok{R}\OperatorTok{::}\DataTypeTok{Support}\AttributeTok{.eval}\NormalTok{(}\StringTok{"requireNamespace(\textquotesingle{}arrow\textquotesingle{}, quietly=TRUE) \&\& requireNamespace(\textquotesingle{}dplyr\textquotesingle{}, quietly=TRUE)"}\NormalTok{) }\OperatorTok{==} \DecValTok{true}
3685
- \FunctionTok{puts} \VerbatimStringTok{\textquotesingle{}(Skip: need arrow + dplyr in R; use bin/galaaz{-}jruby outside gKnit.)\textquotesingle{}}
4243
+ \NormalTok{arrow\_ok }\OperatorTok{=} \ConstantTok{R}\OperatorTok{::}\DataTypeTok{Support}\AttributeTok{.eval}\NormalTok{(}
4244
+ \StringTok{"requireNamespace(\textquotesingle{}arrow\textquotesingle{}, quietly=TRUE) \&\& "} \OperatorTok{+}
4245
+ \StringTok{"requireNamespace(\textquotesingle{}dplyr\textquotesingle{}, quietly=TRUE)"}\NormalTok{)}
4246
+ \ControlFlowTok{unless}\NormalTok{ arrow\_ok }\OperatorTok{==} \DecValTok{true}
4247
+ \FunctionTok{puts} \VerbatimStringTok{\textquotesingle{}(Skip: need arrow + dplyr in R; \textquotesingle{}} \OperatorTok{+}
4248
+ \VerbatimStringTok{\textquotesingle{}use bin/galaaz{-}jruby outside gKnit.)\textquotesingle{}}
3686
4249
  \ControlFlowTok{else}
3687
4250
  \NormalTok{ thread\_count }\OperatorTok{=} \DecValTok{4}
3688
4251
  \NormalTok{ rows\_per\_thread }\OperatorTok{=} \DecValTok{500}
@@ -3716,7 +4279,7 @@ can knit quickly; increase \texttt{thread\_count} and
3716
4279
  \NormalTok{ grouped,}
3717
4280
  \WarningTok{n:} \ConstantTok{E}\AttributeTok{.n}\NormalTok{(),}
3718
4281
  \WarningTok{total:} \ConstantTok{E}\AttributeTok{.sum}\NormalTok{(}\WarningTok{:value}\NormalTok{),}
3719
- \WarningTok{wsum:} \ConstantTok{E}\AttributeTok{.sum}\NormalTok{(}\WarningTok{:value} \OperatorTok{*} \WarningTok{:weight}\NormalTok{)}
4282
+ \WarningTok{wsum:} \ConstantTok{E}\AttributeTok{.sum}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:value}\KeywordTok{]} \OperatorTok{*} \ConstantTok{R}\KeywordTok{[}\WarningTok{:weight}\KeywordTok{]}\NormalTok{)}
3720
4283
  \NormalTok{ )}
3721
4284
  \NormalTok{ out }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.dplyr\_\_\_collect}\NormalTok{(summarised)}
3722
4285
 
@@ -3725,7 +4288,9 @@ can knit quickly; increase \texttt{thread\_count} and
3725
4288
 
3726
4289
  \NormalTok{ total\_n }\OperatorTok{=} \DecValTok{0}
3727
4290
  \NormalTok{ (}\DecValTok{1}\OperatorTok{..}\NormalTok{(out}\AttributeTok{.nrow} \OperatorTok{\textgreater{}\textgreater{}} \DecValTok{0}\NormalTok{))}\AttributeTok{.each} \OperatorTok{\{} \OperatorTok{|}\NormalTok{i}\OperatorTok{|}\NormalTok{ total\_n }\OperatorTok{+=}\NormalTok{ (out}\KeywordTok{[[}\CharTok{\textquotesingle{}n\textquotesingle{}}\KeywordTok{]][}\NormalTok{i}\KeywordTok{]} \OperatorTok{\textgreater{}\textgreater{}} \DecValTok{0}\NormalTok{) }\OperatorTok{\}}
3728
- \FunctionTok{puts} \StringTok{"Sum of group counts n (should equal }\SpecialCharTok{\#\{}\NormalTok{thread\_count }\OperatorTok{*}\NormalTok{ rows\_per\_thread}\SpecialCharTok{\}}\StringTok{): }\SpecialCharTok{\#\{}\NormalTok{total\_n}\SpecialCharTok{\}}\StringTok{"}
4291
+ \FunctionTok{puts} \StringTok{"Sum of group counts n "} \OperatorTok{+}
4292
+ \StringTok{"(should equal }\SpecialCharTok{\#\{}\NormalTok{thread\_count }\OperatorTok{*}\NormalTok{ rows\_per\_thread}\SpecialCharTok{\}}\StringTok{): "} \OperatorTok{+}
4293
+ \StringTok{"}\SpecialCharTok{\#\{}\NormalTok{total\_n}\SpecialCharTok{\}}\StringTok{"}
3729
4294
  \ControlFlowTok{end}
3730
4295
  \end{Highlighting}
3731
4296
  \end{Shaded}
@@ -3788,11 +4353,14 @@ dataset}\label{example-deseq2-on-the-airway-dataset}
3788
4353
  The script
3789
4354
  \textbf{\texttt{examples/bioconductor\_deseq2\_airway/deseq2\_airway\_galaaz.rb}}
3790
4355
  is the canonical version in the repository. Run it from the
3791
- \textbf{Galaaz repository root} with JRuby, for example:
4356
+ \textbf{Galaaz repository root} with either engine, for example:
3792
4357
 
3793
4358
  \begin{Shaded}
3794
4359
  \begin{Highlighting}[]
3795
- \NormalTok{bin/galaaz{-}jruby examples/bioconductor\_deseq2\_airway/deseq2\_airway\_galaaz.rb}
4360
+ \NormalTok{bin/galaaz{-}ruby \textbackslash{}}
4361
+ \NormalTok{ examples/bioconductor\_deseq2\_airway/deseq2\_airway\_galaaz.rb}
4362
+ \NormalTok{\# or: bin/galaaz{-}jruby \textbackslash{}}
4363
+ \NormalTok{\# examples/bioconductor\_deseq2\_airway/deseq2\_airway\_galaaz.rb}
3796
4364
  \end{Highlighting}
3797
4365
  \end{Shaded}
3798
4366
 
@@ -3809,14 +4377,14 @@ The workflow in Ruby mirrors a standard DESeq2 vignette:
3809
4377
  \textbf{\texttt{R.data(\textquotesingle{}airway\textquotesingle{})}}
3810
4378
  so the object exists in R's global environment.
3811
4379
  \item
3812
- \textbf{\texttt{airway\ =\ \textasciitilde{}:airway}} pulls the
4380
+ \textbf{\texttt{airway\ =\ \textasciitilde{}R{[}:airway{]}}} pulls the
3813
4381
  experiment into a Galaaz wrapper so you can pass it to R functions as
3814
4382
  a Ruby value.
3815
4383
  \item
3816
- \textbf{\texttt{R.DESeqDataSet(...,\ design:\ (:all.til\ :cell\ +\ :dex))}}
4384
+ \textbf{\texttt{R.DESeqDataSet(...,\ design:\ (R{[}:all{]}.til\ R{[}:cell{]}\ +\ R{[}:dex{]}))}}
3817
4385
  builds the \textbf{\texttt{DESeqDataSet}}. The
3818
- \textbf{\texttt{(:all.til\ :cell\ +\ :dex)}} form is Galaaz's way of
3819
- passing the one-sided formula
4386
+ \textbf{\texttt{(R{[}:all{]}.til\ R{[}:cell{]}\ +\ R{[}:dex{]})}} form
4387
+ is Galaaz's way of passing the one-sided formula
3820
4388
  \textbf{\texttt{\textasciitilde{}\ cell\ +\ dex}} (adjust for the
3821
4389
  design you need).
3822
4390
  \item
@@ -3848,8 +4416,9 @@ Below is the full listing (same as the file in the repository). It is
3848
4416
 
3849
4417
  \begin{Shaded}
3850
4418
  \begin{Highlighting}[]
3851
- \CommentTok{\# Canonical script: examples/bioconductor\_deseq2\_airway/deseq2\_airway\_galaaz.rb}
3852
- \CommentTok{\# Run: bin/galaaz{-}jruby examples/.../deseq2\_airway\_galaaz.rb (repo root).}
4419
+ \CommentTok{\# Canonical script:}
4420
+ \CommentTok{\# examples/bioconductor\_deseq2\_airway/deseq2\_airway\_galaaz.rb}
4421
+ \CommentTok{\# Run: bin/galaaz{-}ruby examples/.../deseq2\_airway\_galaaz.rb (repo root).}
3853
4422
 
3854
4423
  \FunctionTok{require} \VerbatimStringTok{\textquotesingle{}galaaz\textquotesingle{}}
3855
4424
 
@@ -3857,10 +4426,10 @@ Below is the full listing (same as the file in the repository). It is
3857
4426
  \ConstantTok{R}\AttributeTok{.library}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}airway\textquotesingle{}}\NormalTok{)}
3858
4427
  \ConstantTok{R}\AttributeTok{.data}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}airway\textquotesingle{}}\NormalTok{)}
3859
4428
 
3860
- \NormalTok{airway }\OperatorTok{=} \OperatorTok{\textasciitilde{}}\WarningTok{:airway}
4429
+ \NormalTok{airway }\OperatorTok{=} \OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:airway}\KeywordTok{]}
3861
4430
 
3862
4431
  \CommentTok{\# Build DESeq2 dataset with one{-}sided formula: \textasciitilde{} cell + dex.}
3863
- \NormalTok{dds }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.}\DataTypeTok{DESeqDataSet}\NormalTok{(airway, }\WarningTok{design:}\NormalTok{ (}\WarningTok{:all}\AttributeTok{.til} \OperatorTok{:}\NormalTok{cell }\OperatorTok{+} \WarningTok{:dex}\NormalTok{))}
4432
+ \NormalTok{dds }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.}\DataTypeTok{DESeqDataSet}\NormalTok{(airway, }\WarningTok{design:}\NormalTok{ (}\ConstantTok{R}\KeywordTok{[}\WarningTok{:all}\KeywordTok{]}\AttributeTok{.til} \ConstantTok{R}\KeywordTok{[}\WarningTok{:cell}\KeywordTok{]} \OperatorTok{+} \ConstantTok{R}\KeywordTok{[}\WarningTok{:dex}\KeywordTok{]}\NormalTok{))}
3864
4433
 
3865
4434
  \CommentTok{\# Prefilter genes with almost no counts.}
3866
4435
  \NormalTok{keep }\OperatorTok{=} \ConstantTok{R}\AttributeTok{.rowSums}\NormalTok{(}\ConstantTok{R}\AttributeTok{.counts}\NormalTok{(dds)) }\OperatorTok{\textgreater{}=} \DecValTok{10}
@@ -3875,7 +4444,8 @@ Below is the full listing (same as the file in the repository). It is
3875
4444
  \FunctionTok{puts} \StringTok{"Genes after prefilter: }\SpecialCharTok{\#\{}\ConstantTok{R}\AttributeTok{.nrow}\NormalTok{(dds)}\SpecialCharTok{\}}\StringTok{"}
3876
4445
  \FunctionTok{puts} \StringTok{"Result rows: }\SpecialCharTok{\#\{}\ConstantTok{R}\AttributeTok{.nrow}\NormalTok{(res)}\SpecialCharTok{\}}\StringTok{"}
3877
4446
  \FunctionTok{puts} \StringTok{"Result columns: }\SpecialCharTok{\#\{}\ConstantTok{R}\AttributeTok{.colnames}\NormalTok{(res)}\SpecialCharTok{\}}\StringTok{"}
3878
- \FunctionTok{puts} \StringTok{"Significant genes (padj \textless{} 0.05): }\SpecialCharTok{\#\{}\ConstantTok{R}\AttributeTok{.sum}\NormalTok{(res}\AttributeTok{.padj} \OperatorTok{\textless{}} \FloatTok{0.05}\NormalTok{, }\WarningTok{na\_\_rm:} \DecValTok{true}\NormalTok{)}\SpecialCharTok{\}}\StringTok{"}
4447
+ \FunctionTok{puts} \StringTok{"Significant genes (padj \textless{} 0.05): "} \OperatorTok{+}
4448
+ \StringTok{"}\SpecialCharTok{\#\{}\ConstantTok{R}\AttributeTok{.sum}\NormalTok{(res}\AttributeTok{.padj} \OperatorTok{\textless{}} \FloatTok{0.05}\NormalTok{, }\WarningTok{na\_\_rm:} \DecValTok{true}\NormalTok{)}\SpecialCharTok{\}}\StringTok{"}
3879
4449
 
3880
4450
  \NormalTok{res\_ordered }\OperatorTok{=}\NormalTok{ res}\KeywordTok{[}\ConstantTok{R}\AttributeTok{.order}\NormalTok{(res}\AttributeTok{.padj}\NormalTok{), }\WarningTok{:all}\KeywordTok{]}
3881
4451
  \FunctionTok{puts} \ConstantTok{R}\AttributeTok{.head}\NormalTok{(}\ConstantTok{R}\AttributeTok{.as\_\_data\_\_frame}\NormalTok{(res\_ordered), }\DecValTok{10}\NormalTok{)}
@@ -3893,13 +4463,17 @@ loads the data and prints a short preview (it does \textbf{not} run
3893
4463
 
3894
4464
  \begin{Shaded}
3895
4465
  \begin{Highlighting}[]
3896
- \ControlFlowTok{unless} \ConstantTok{R}\OperatorTok{::}\DataTypeTok{Support}\AttributeTok{.eval}\NormalTok{(}\StringTok{"requireNamespace(\textquotesingle{}DESeq2\textquotesingle{}, quietly=TRUE) \&\& requireNamespace(\textquotesingle{}airway\textquotesingle{}, quietly=TRUE)"}\NormalTok{)}
3897
- \FunctionTok{puts} \VerbatimStringTok{\textquotesingle{}(Skip: install DESeq2 and airway via BiocManager in R to run the full example.)\textquotesingle{}}
4466
+ \NormalTok{deseq\_ok }\OperatorTok{=} \ConstantTok{R}\OperatorTok{::}\DataTypeTok{Support}\AttributeTok{.eval}\NormalTok{(}
4467
+ \StringTok{"requireNamespace(\textquotesingle{}DESeq2\textquotesingle{}, quietly=TRUE) \&\& "} \OperatorTok{+}
4468
+ \StringTok{"requireNamespace(\textquotesingle{}airway\textquotesingle{}, quietly=TRUE)"}\NormalTok{)}
4469
+ \ControlFlowTok{unless}\NormalTok{ deseq\_ok}
4470
+ \FunctionTok{puts} \VerbatimStringTok{\textquotesingle{}(Skip: install DESeq2 and airway via \textquotesingle{}} \OperatorTok{+}
4471
+ \VerbatimStringTok{\textquotesingle{}BiocManager in R for the full example.)\textquotesingle{}}
3898
4472
  \ControlFlowTok{else}
3899
4473
  \ConstantTok{R}\AttributeTok{.library}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}DESeq2\textquotesingle{}}\NormalTok{)}
3900
4474
  \ConstantTok{R}\AttributeTok{.library}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}airway\textquotesingle{}}\NormalTok{)}
3901
4475
  \ConstantTok{R}\AttributeTok{.data}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}airway\textquotesingle{}}\NormalTok{)}
3902
- \NormalTok{ airway }\OperatorTok{=} \OperatorTok{\textasciitilde{}}\WarningTok{:airway}
4476
+ \NormalTok{ airway }\OperatorTok{=} \OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:airway}\KeywordTok{]}
3903
4477
  \FunctionTok{puts} \VerbatimStringTok{\textquotesingle{}airway object (head of assay / dims via R):\textquotesingle{}}
3904
4478
  \FunctionTok{puts} \StringTok{"ncol(samples): }\SpecialCharTok{\#\{}\ConstantTok{R}\AttributeTok{.ncol}\NormalTok{(airway)}\SpecialCharTok{\}}\StringTok{"}
3905
4479
  \FunctionTok{puts} \ConstantTok{R}\AttributeTok{.head}\NormalTok{(}\ConstantTok{R}\AttributeTok{.assay}\NormalTok{(airway), }\DecValTok{3}\NormalTok{)}
@@ -3910,14 +4484,14 @@ loads the data and prints a short preview (it does \textbf{not} run
3910
4484
  \begin{verbatim}
3911
4485
  ## airway object (head of assay / dims via R):
3912
4486
  ## ncol(samples): [1] 8
3913
- ## SRR1039508 SRR1039509 SRR1039512 SRR1039513 SRR1039516
3914
- ## ENSG00000000003 679 448 873 408 1138
3915
- ## ENSG00000000005 0 0 0 0 0
3916
- ## ENSG00000000419 467 515 621 365 587
3917
- ## SRR1039517 SRR1039520 SRR1039521
3918
- ## ENSG00000000003 1047 770 572
3919
- ## ENSG00000000005 0 0 0
3920
- ## ENSG00000000419 799 417 508
4487
+ ## SRR1039508 SRR1039509 SRR1039512 SRR1039513
4488
+ ## ENSG00000000003 679 448 873 408
4489
+ ## ENSG00000000005 0 0 0 0
4490
+ ## ENSG00000000419 467 515 621 365
4491
+ ## SRR1039516 SRR1039517 SRR1039520 SRR1039521
4492
+ ## ENSG00000000003 1138 1047 770 572
4493
+ ## ENSG00000000005 0 0 0 0
4494
+ ## ENSG00000000419 587 799 417 508
3921
4495
  \end{verbatim}
3922
4496
 
3923
4497
  \section{Performance}\label{performance}
@@ -3962,10 +4536,10 @@ first prepare the data frame with the necessary data:
3962
4536
  \begin{Shaded}
3963
4537
  \begin{Highlighting}[]
3964
4538
  \CommentTok{\# :mtcars {-}\textgreater{} Ruby handle}
3965
- \NormalTok{mtcars }\OperatorTok{=} \OperatorTok{\textasciitilde{}}\WarningTok{:mtcars}
4539
+ \NormalTok{mtcars }\OperatorTok{=} \OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}
3966
4540
 
3967
4541
  \CommentTok{\# Row labels are not a plot column; copy them to car\_name.}
3968
- \NormalTok{mtcars}\AttributeTok{.car\_name} \OperatorTok{=} \ConstantTok{R}\AttributeTok{.rownames}\NormalTok{(}\WarningTok{:mtcars}\NormalTok{)}
4542
+ \NormalTok{mtcars}\AttributeTok{.car\_name} \OperatorTok{=} \ConstantTok{R}\AttributeTok{.rownames}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mtcars}\KeywordTok{]}\NormalTok{)}
3969
4543
 
3970
4544
  \CommentTok{\# Z{-}score mpg (mean/sd on mtcars.mpg); round to 2 decimals.}
3971
4545
  \NormalTok{mtcars}\AttributeTok{.mpg\_z} \OperatorTok{=}\NormalTok{ ((mtcars}\AttributeTok{.mpg} \OperatorTok{{-}}\NormalTok{ mtcars}\AttributeTok{.mpg.mean}\NormalTok{)}\OperatorTok{/}\NormalTok{mtcars}\AttributeTok{.mpg.sd}\NormalTok{)}\AttributeTok{.round} \DecValTok{2}
@@ -3984,20 +4558,20 @@ first prepare the data frame with the necessary data:
3984
4558
  \end{Shaded}
3985
4559
 
3986
4560
  \begin{verbatim}
3987
- ## mpg cyl disp hp drat wt qsec vs am gear carb
3988
- ## Cadillac Fleetwood 10.4 8 472 205 2.93 5.250 17.98 0 0 3 4
3989
- ## Lincoln Continental 10.4 8 460 215 3.00 5.424 17.82 0 0 3 4
3990
- ## Camaro Z28 13.3 8 350 245 3.73 3.840 15.41 0 0 3 4
3991
- ## Duster 360 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4
3992
- ## Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4
3993
- ## Maserati Bora 15.0 8 301 335 3.54 3.570 14.60 0 1 5 8
3994
- ## car_name mpg_z mpg_type
3995
- ## Cadillac Fleetwood Cadillac Fleetwood -1.61 below
3996
- ## Lincoln Continental Lincoln Continental -1.61 below
3997
- ## Camaro Z28 Camaro Z28 -1.13 below
3998
- ## Duster 360 Duster 360 -0.96 below
3999
- ## Chrysler Imperial Chrysler Imperial -0.89 below
4000
- ## Maserati Bora Maserati Bora -0.84 below
4561
+ ## mpg cyl disp hp drat wt qsec vs am gear
4562
+ ## Cadillac Fleetwood 10.4 8 472 205 2.93 5.250 17.98 0 0 3
4563
+ ## Lincoln Continental 10.4 8 460 215 3.00 5.424 17.82 0 0 3
4564
+ ## Camaro Z28 13.3 8 350 245 3.73 3.840 15.41 0 0 3
4565
+ ## Duster 360 14.3 8 360 245 3.21 3.570 15.84 0 0 3
4566
+ ## Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3
4567
+ ## Maserati Bora 15.0 8 301 335 3.54 3.570 14.60 0 1 5
4568
+ ## carb car_name mpg_z mpg_type
4569
+ ## Cadillac Fleetwood 4 Cadillac Fleetwood -1.61 below
4570
+ ## Lincoln Continental 4 Lincoln Continental -1.61 below
4571
+ ## Camaro Z28 4 Camaro Z28 -1.13 below
4572
+ ## Duster 360 4 Duster 360 -0.96 below
4573
+ ## Chrysler Imperial 4 Chrysler Imperial -0.89 below
4574
+ ## Maserati Bora 8 Maserati Bora -0.84 below
4001
4575
  \end{verbatim}
4002
4576
 
4003
4577
  Now, let's plot the diverging bar plot. When using gKnit, you normally
@@ -4024,18 +4598,22 @@ we add `coord\_flip'.
4024
4598
  \begin{Highlighting}[]
4025
4599
  \FunctionTok{require} \VerbatimStringTok{\textquotesingle{}ggplot\textquotesingle{}}
4026
4600
 
4027
- \FunctionTok{puts}\NormalTok{ mtcars}\AttributeTok{.ggplot}\NormalTok{(}\ConstantTok{E}\AttributeTok{.aes}\NormalTok{(}\WarningTok{x:} \WarningTok{:car\_name}\NormalTok{, }\WarningTok{y:} \WarningTok{:mpg\_z}\NormalTok{, }\WarningTok{label:} \WarningTok{:mpg\_z}\NormalTok{)) }\OperatorTok{+}
4028
- \ConstantTok{R}\AttributeTok{.geom\_bar}\NormalTok{(}\ConstantTok{E}\AttributeTok{.aes}\NormalTok{(}\WarningTok{fill:} \WarningTok{:mpg\_type}\NormalTok{), }\WarningTok{stat:} \VerbatimStringTok{\textquotesingle{}identity\textquotesingle{}}\NormalTok{, }\WarningTok{width:} \FloatTok{0.5}\NormalTok{) }\OperatorTok{+}
4029
- \ConstantTok{R}\AttributeTok{.scale\_fill\_manual}\NormalTok{(}\WarningTok{name:} \VerbatimStringTok{\textquotesingle{}Mileage\textquotesingle{}}\NormalTok{,}
4030
- \WarningTok{labels:} \ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}Above Average\textquotesingle{}}\NormalTok{, }\VerbatimStringTok{\textquotesingle{}Below Average\textquotesingle{}}\NormalTok{),}
4031
- \WarningTok{values:} \ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}above\textquotesingle{}}\WarningTok{:} \VerbatimStringTok{\textquotesingle{}\#00ba38\textquotesingle{}}\NormalTok{, }\VerbatimStringTok{\textquotesingle{}below\textquotesingle{}}\WarningTok{:} \VerbatimStringTok{\textquotesingle{}\#f8766d\textquotesingle{}}\NormalTok{)) }\OperatorTok{+}
4601
+ \FunctionTok{puts}\NormalTok{ mtcars}\AttributeTok{.ggplot}\NormalTok{(}
4602
+ \ConstantTok{E}\AttributeTok{.aes}\NormalTok{(}\WarningTok{x:} \WarningTok{:car\_name}\NormalTok{, }\WarningTok{y:} \WarningTok{:mpg\_z}\NormalTok{, }\WarningTok{label:} \WarningTok{:mpg\_z}\NormalTok{)) }\OperatorTok{+}
4603
+ \ConstantTok{R}\AttributeTok{.geom\_bar}\NormalTok{(}\ConstantTok{E}\AttributeTok{.aes}\NormalTok{(}\WarningTok{fill:} \WarningTok{:mpg\_type}\NormalTok{),}
4604
+ \WarningTok{stat:} \VerbatimStringTok{\textquotesingle{}identity\textquotesingle{}}\NormalTok{, }\WarningTok{width:} \FloatTok{0.5}\NormalTok{) }\OperatorTok{+}
4605
+ \ConstantTok{R}\AttributeTok{.scale\_fill\_manual}\NormalTok{(}
4606
+ \WarningTok{name:} \VerbatimStringTok{\textquotesingle{}Mileage\textquotesingle{}}\NormalTok{,}
4607
+ \WarningTok{labels:} \ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}Above Average\textquotesingle{}}\NormalTok{, }\VerbatimStringTok{\textquotesingle{}Below Average\textquotesingle{}}\NormalTok{),}
4608
+ \WarningTok{values:} \ConstantTok{R}\AttributeTok{.c}\NormalTok{(}\VerbatimStringTok{\textquotesingle{}above\textquotesingle{}}\WarningTok{:} \VerbatimStringTok{\textquotesingle{}\#00ba38\textquotesingle{}}\NormalTok{,}
4609
+ \VerbatimStringTok{\textquotesingle{}below\textquotesingle{}}\WarningTok{:} \VerbatimStringTok{\textquotesingle{}\#f8766d\textquotesingle{}}\NormalTok{)) }\OperatorTok{+}
4032
4610
  \ConstantTok{R}\AttributeTok{.labs}\NormalTok{(}\WarningTok{subtitle:} \StringTok{"Normalised mileage from \textquotesingle{}mtcars\textquotesingle{}"}\NormalTok{,}
4033
- \WarningTok{title:} \StringTok{"Diverging Bars"}\NormalTok{) }\OperatorTok{+}
4611
+ \WarningTok{title:} \StringTok{"Diverging Bars"}\NormalTok{) }\OperatorTok{+}
4034
4612
  \ConstantTok{R}\AttributeTok{.coord\_flip}
4035
4613
  \end{Highlighting}
4036
4614
  \end{Shaded}
4037
4615
 
4038
- \pandocbounded{\includegraphics[keepaspectratio]{manual_files/figure-latex/diverging_bar.pdf}}
4616
+ \pandocbounded{\includegraphics[keepaspectratio]{manual_files/manual_files/figure-latex/diverging_bar.pdf}}
4039
4617
 
4040
4618
  \section{Coding with Tidyverse}\label{coding-with-tidyverse}
4041
4619
 
@@ -4070,15 +4648,15 @@ and now, let's look at this code:
4070
4648
  It generates the following error: ``object `x' not found.
4071
4649
 
4072
4650
  However, in Galaaz, arguments are referentially transparent as can be
4073
- seen by the code below. Note initially that `my\_var = :x' will not give
4074
- the error ``object `x' not found'' since `:x' is treated as an
4651
+ seen by the code below. Note initially that `my\_var = R{[}:x{]}' will
4652
+ not give the error ``object `x' not found'' since `:x' is treated as an
4075
4653
  expression and assigned to my\_var. Then when doing (my\_var.eq 1),
4076
4654
  my\_var is a variable that resolves to `:x' and it becomes equivalent to
4077
- (:x.eq 1) which is what we want.
4655
+ (R{[}:x{]}.eq 1) which is what we want.
4078
4656
 
4079
4657
  \begin{Shaded}
4080
4658
  \begin{Highlighting}[]
4081
- \NormalTok{my\_var }\OperatorTok{=} \WarningTok{:x}
4659
+ \NormalTok{my\_var }\OperatorTok{=} \ConstantTok{R}\KeywordTok{[}\WarningTok{:x}\KeywordTok{]}
4082
4660
  \FunctionTok{puts}\NormalTok{ df}\AttributeTok{.filter}\NormalTok{(my\_var}\AttributeTok{.eq} \DecValTok{1}\NormalTok{)}
4083
4661
  \end{Highlighting}
4084
4662
  \end{Shaded}
@@ -4104,11 +4682,11 @@ df[x == y, ]
4104
4682
 
4105
4683
  In galaaz this ambiguity does not exist, filter(df, x.eq y) is not a
4106
4684
  valid expression as expressions are build with symbols. In doing
4107
- filter(df, :x.eq y) we are looking for elements of the `x' column that
4108
- are equal to a previously defined y variable. Finally in filter(df,
4109
- :x.eq :y) we are looking for elements in which the `x' column value is
4110
- equal to the `y' column value. This can be seen in the following two
4111
- chunks of code:
4685
+ filter(df, R{[}:x{]}.eq y) we are looking for elements of the `x' column
4686
+ that are equal to a previously defined y variable. Finally in filter(df,
4687
+ R{[}:x{]}.eq R{[}:y{]}) we are looking for elements in which the `x'
4688
+ column value is equal to the `y' column value. This can be seen in the
4689
+ following two chunks of code:
4112
4690
 
4113
4691
  \begin{Shaded}
4114
4692
  \begin{Highlighting}[]
@@ -4116,7 +4694,7 @@ chunks of code:
4116
4694
  \NormalTok{x }\OperatorTok{=} \DecValTok{2}
4117
4695
 
4118
4696
  \CommentTok{\# looking for values where the \textquotesingle{}x\textquotesingle{} column is equal to the \textquotesingle{}y\textquotesingle{} column}
4119
- \FunctionTok{puts}\NormalTok{ df}\AttributeTok{.filter}\NormalTok{(}\WarningTok{:x}\AttributeTok{.eq} \OperatorTok{:}\NormalTok{y)}
4697
+ \FunctionTok{puts}\NormalTok{ df}\AttributeTok{.filter}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:x}\KeywordTok{]}\AttributeTok{.eq} \ConstantTok{R}\KeywordTok{[}\WarningTok{:y}\KeywordTok{]}\NormalTok{)}
4120
4698
  \end{Highlighting}
4121
4699
  \end{Shaded}
4122
4700
 
@@ -4129,7 +4707,7 @@ chunks of code:
4129
4707
  \begin{Highlighting}[]
4130
4708
  \CommentTok{\# looking for values where the \textquotesingle{}x\textquotesingle{} column is equal to the \textquotesingle{}y\textquotesingle{} variable}
4131
4709
  \CommentTok{\# in this case, the number 1}
4132
- \FunctionTok{puts}\NormalTok{ df}\AttributeTok{.filter}\NormalTok{(}\WarningTok{:x}\AttributeTok{.eq}\NormalTok{ y)}
4710
+ \FunctionTok{puts}\NormalTok{ df}\AttributeTok{.filter}\NormalTok{(}\ConstantTok{R}\KeywordTok{[}\WarningTok{:x}\KeywordTok{]}\AttributeTok{.eq}\NormalTok{ y)}
4133
4711
  \end{Highlighting}
4134
4712
  \end{Shaded}
4135
4713
 
@@ -4173,7 +4751,9 @@ silently.
4173
4751
  \begin{Shaded}
4174
4752
  \begin{Highlighting}[]
4175
4753
  \ControlFlowTok{def}\NormalTok{ mutate\_y(df)}
4176
- \NormalTok{ df}\AttributeTok{.mutate}\NormalTok{(}\WarningTok{:y}\AttributeTok{.assign} \OperatorTok{:}\NormalTok{a }\OperatorTok{+} \WarningTok{:x}\NormalTok{)}
4754
+ \CommentTok{\# Column names are Ruby kwargs (y: …).}
4755
+ \CommentTok{\# Use .assign only for R \textasciigrave{}\textless{}{-}\textasciigrave{} expressions.}
4756
+ \NormalTok{ df}\AttributeTok{.mutate}\NormalTok{(}\WarningTok{y:} \ConstantTok{R}\KeywordTok{[}\WarningTok{:a}\KeywordTok{]} \OperatorTok{+} \ConstantTok{R}\KeywordTok{[}\WarningTok{:x}\KeywordTok{]}\NormalTok{)}
4177
4757
  \ControlFlowTok{end}
4178
4758
  \end{Highlighting}
4179
4759
  \end{Shaded}
@@ -4196,19 +4776,26 @@ Here we create a data frame that has only one column named `x':
4196
4776
 
4197
4777
  Note that method mutate\_y will fail independently from the fact that
4198
4778
  variable `a' is defined and in the scope of the method. Variable `a' has
4199
- no relationship with the symbol `:a' used in the definition of
4200
- `mutate\_y' above:
4779
+ no relationship with the symbol \texttt{R{[}:a{]}} used in the
4780
+ definition of `mutate\_y' above:
4201
4781
 
4202
4782
  \begin{Shaded}
4203
4783
  \begin{Highlighting}[]
4204
4784
  \NormalTok{a }\OperatorTok{=} \DecValTok{10}
4205
- \NormalTok{mutate\_y(df1)}
4785
+ \ControlFlowTok{begin}
4786
+ \NormalTok{ mutate\_y(df1)}
4787
+ \ControlFlowTok{rescue} \OperatorTok{=\textgreater{}}\NormalTok{ e}
4788
+ \FunctionTok{puts}\NormalTok{ e}\AttributeTok{.class.to\_s}
4789
+ \NormalTok{ e}\AttributeTok{.message.to\_s.scan}\NormalTok{(}\SpecialStringTok{/.\{1,68\}/}\NormalTok{)}\AttributeTok{.each} \OperatorTok{\{} \OperatorTok{|}\NormalTok{line}\OperatorTok{|} \FunctionTok{puts}\NormalTok{ line }\OperatorTok{\}}
4790
+ \ControlFlowTok{end}
4206
4791
  \end{Highlighting}
4207
4792
  \end{Shaded}
4208
4793
 
4209
4794
  \begin{verbatim}
4210
- ## Error in strsplit(path, "/") : non-character argument
4211
- ## Calls: mutate ... .rlang_purrr_map_mold -> vapply -> FUN -> path_trim_prefix -> strsplit
4795
+ ## NewBridge::SessionClient::RProcessError
4796
+ ## Error: i In argument: `y = a + x`.
4797
+ ## Caused by error:
4798
+ ## ! object 'a' not found
4212
4799
  \end{verbatim}
4213
4800
 
4214
4801
  \subsection{Different expressions}\label{different-expressions}
@@ -4304,7 +4891,7 @@ e.g.~\texttt{:df}).
4304
4891
 
4305
4892
  \begin{Shaded}
4306
4893
  \begin{Highlighting}[]
4307
- \FunctionTok{puts} \OperatorTok{\textasciitilde{}}\WarningTok{:df}
4894
+ \FunctionTok{puts} \OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:df}\KeywordTok{]}
4308
4895
  \end{Highlighting}
4309
4896
  \end{Shaded}
4310
4897
 
@@ -4318,16 +4905,16 @@ e.g.~\texttt{:df}).
4318
4905
  \end{verbatim}
4319
4906
 
4320
4907
  We then create the `my\_summarize' method and call it passing the R data
4321
- frame and the group by variable `:g1':
4908
+ frame and the group by variable `R{[}:g1{]}':
4322
4909
 
4323
4910
  \begin{Shaded}
4324
4911
  \begin{Highlighting}[]
4325
4912
  \ControlFlowTok{def}\NormalTok{ my\_summarize(df, group\_var)}
4326
4913
  \NormalTok{ df}\AttributeTok{.group\_by}\NormalTok{(group\_var)}\AttributeTok{.}
4327
- \AttributeTok{summarize}\NormalTok{(}\WarningTok{a:} \WarningTok{:a}\AttributeTok{.mean}\NormalTok{)}
4914
+ \AttributeTok{summarize}\NormalTok{(}\WarningTok{a:} \ConstantTok{R}\KeywordTok{[}\WarningTok{:a}\KeywordTok{]}\AttributeTok{.mean}\NormalTok{)}
4328
4915
  \ControlFlowTok{end}
4329
4916
 
4330
- \FunctionTok{puts}\NormalTok{ my\_summarize(}\WarningTok{:df}\NormalTok{, }\WarningTok{:g1}\NormalTok{)}
4917
+ \FunctionTok{puts}\NormalTok{ my\_summarize(}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:df}\KeywordTok{]}\NormalTok{, }\ConstantTok{R}\KeywordTok{[}\WarningTok{:g1}\KeywordTok{]}\NormalTok{)}
4331
4918
  \end{Highlighting}
4332
4919
  \end{Shaded}
4333
4920
 
@@ -4343,7 +4930,7 @@ It works!!! Well, let's make sure this was not just some coincidence
4343
4930
 
4344
4931
  \begin{Shaded}
4345
4932
  \begin{Highlighting}[]
4346
- \FunctionTok{puts}\NormalTok{ my\_summarize(}\WarningTok{:df}\NormalTok{, }\WarningTok{:g2}\NormalTok{)}
4933
+ \FunctionTok{puts}\NormalTok{ my\_summarize(}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:df}\KeywordTok{]}\NormalTok{, }\ConstantTok{R}\KeywordTok{[}\WarningTok{:g2}\KeywordTok{]}\NormalTok{)}
4347
4934
  \end{Highlighting}
4348
4935
  \end{Shaded}
4349
4936
 
@@ -4396,10 +4983,10 @@ Let's try it in galaaz:
4396
4983
  \NormalTok{ )}
4397
4984
  \ControlFlowTok{end}
4398
4985
 
4399
- \FunctionTok{puts}\NormalTok{ my\_summarise2((}\OperatorTok{\textasciitilde{}}\WarningTok{:df}\NormalTok{), }\WarningTok{:a}\NormalTok{)}
4986
+ \FunctionTok{puts}\NormalTok{ my\_summarise2((}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:df}\KeywordTok{]}\NormalTok{), }\WarningTok{:a}\NormalTok{)}
4400
4987
  \FunctionTok{puts} \StringTok{"}
4401
4988
  \StringTok{"}
4402
- \FunctionTok{puts}\NormalTok{ my\_summarise2((}\OperatorTok{\textasciitilde{}}\WarningTok{:df}\NormalTok{), }\WarningTok{:a} \OperatorTok{*} \WarningTok{:b}\NormalTok{)}
4989
+ \FunctionTok{puts}\NormalTok{ my\_summarise2((}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:df}\KeywordTok{]}\NormalTok{), }\ConstantTok{R}\KeywordTok{[}\WarningTok{:a}\KeywordTok{]} \OperatorTok{*} \ConstantTok{R}\KeywordTok{[}\WarningTok{:b}\KeywordTok{]}\NormalTok{)}
4403
4990
  \end{Highlighting}
4404
4991
  \end{Shaded}
4405
4992
 
@@ -4462,10 +5049,10 @@ Here is our Ruby code:
4462
5049
  \NormalTok{ sum\_name }\OperatorTok{=\textgreater{}} \ConstantTok{E}\AttributeTok{.sum}\NormalTok{(expr))}
4463
5050
  \ControlFlowTok{end}
4464
5051
 
4465
- \FunctionTok{puts}\NormalTok{ my\_mutate((}\OperatorTok{\textasciitilde{}}\WarningTok{:df}\NormalTok{), }\WarningTok{:a}\NormalTok{)}
5052
+ \FunctionTok{puts}\NormalTok{ my\_mutate((}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:df}\KeywordTok{]}\NormalTok{), }\WarningTok{:a}\NormalTok{)}
4466
5053
  \FunctionTok{puts} \StringTok{"}
4467
5054
  \StringTok{"}
4468
- \FunctionTok{puts}\NormalTok{ my\_mutate((}\OperatorTok{\textasciitilde{}}\WarningTok{:df}\NormalTok{), }\WarningTok{:b}\NormalTok{)}
5055
+ \FunctionTok{puts}\NormalTok{ my\_mutate((}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:df}\KeywordTok{]}\NormalTok{), }\WarningTok{:b}\NormalTok{)}
4469
5056
  \end{Highlighting}
4470
5057
  \end{Shaded}
4471
5058
 
@@ -4513,7 +5100,7 @@ number of paramenters the paramenter is preceded by '*':
4513
5100
  \AttributeTok{summarise}\NormalTok{(}\WarningTok{a:} \ConstantTok{E}\AttributeTok{.mean}\NormalTok{(}\WarningTok{:a}\NormalTok{))}
4514
5101
  \ControlFlowTok{end}
4515
5102
 
4516
- \FunctionTok{puts}\NormalTok{ my\_summarise3((}\OperatorTok{\textasciitilde{}}\WarningTok{:df}\NormalTok{), }\WarningTok{:g1}\NormalTok{, }\WarningTok{:g2}\NormalTok{)}
5103
+ \FunctionTok{puts}\NormalTok{ my\_summarise3((}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:df}\KeywordTok{]}\NormalTok{), }\ConstantTok{R}\KeywordTok{[}\WarningTok{:g1}\KeywordTok{]}\NormalTok{, }\ConstantTok{R}\KeywordTok{[}\WarningTok{:g2}\KeywordTok{]}\NormalTok{)}
4517
5104
  \end{Highlighting}
4518
5105
  \end{Shaded}
4519
5106
 
@@ -4548,10 +5135,10 @@ used, so `x = a' is immediately evaluate and variable `x' will receive
4548
5135
  the value of variable `a' as soon as the Ruby statement is executed.
4549
5136
  Ruby also provides the notion of a symbol; `:a' is a symbol and does not
4550
5137
  evaluate to anything. Galaaz uses Ruby symbols to build expressions that
4551
- are not bound to anything: `:a.eq :b' is clearly an expression and has
4552
- no relationship whatsoever with the statment `a = b'. By using symbols,
4553
- variables and expressions all the possible ambiguities that are found in
4554
- R are eliminated in Galaaz.
5138
+ are not bound to anything: `R{[}:a{]}.eq R{[}:b{]}' is clearly an
5139
+ expression and has no relationship whatsoever with the statment `a = b'.
5140
+ By using symbols, variables and expressions all the possible ambiguities
5141
+ that are found in R are eliminated in Galaaz.
4555
5142
 
4556
5143
  The main problem that remains, is that in R, functions are not clearly
4557
5144
  documented as what type of input they are expecting, they might be
@@ -4560,7 +5147,7 @@ the R function will know how to deal with an input of the form `a = b',
4560
5147
  now for the Ruby developer it might not be immediately clear if it
4561
5148
  should call the function passing the value `true' if variable `a' is
4562
5149
  equal to variable `b' or if it should call the function passing the
4563
- expression `:a.eq :b'.
5150
+ expression `R{[}:a{]}.eq R{[}:b{]}'.
4564
5151
 
4565
5152
  \subsection{Advanced dplyr features}\label{advanced-dplyr-features}
4566
5153
 
@@ -4589,22 +5176,22 @@ the Starwars movies:
4589
5176
 
4590
5177
  \begin{Shaded}
4591
5178
  \begin{Highlighting}[]
4592
- \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\WarningTok{:starwars}\NormalTok{)}\AttributeTok{.head}
5179
+ \FunctionTok{puts}\NormalTok{ (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:starwars}\KeywordTok{]}\NormalTok{)}\AttributeTok{.head}
4593
5180
  \end{Highlighting}
4594
5181
  \end{Shaded}
4595
5182
 
4596
5183
  \begin{verbatim}
4597
5184
  ## # A tibble: 6 x 14
4598
- ## name height mass hair_color skin_color eye_color birth_year sex gender
4599
- ## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
4600
- ## 1 Luke Sky~ 172 77 blond fair blue 19 male mascu~
4601
- ## 2 C-3PO 167 75 <NA> gold yellow 112 none mascu~
4602
- ## 3 R2-D2 96 32 <NA> white, bl~ red 33 none mascu~
4603
- ## 4 Darth Va~ 202 136 none white yellow 41.9 male mascu~
4604
- ## 5 Leia Org~ 150 49 brown light brown 19 fema~ femin~
4605
- ## 6 Owen Lars 178 120 brown, gr~ light blue 52 male mascu~
4606
- ## # i 5 more variables: homeworld <chr>, species <chr>, films <list>,
4607
- ## # vehicles <list>, starships <list>
5185
+ ## name height mass hair_color skin_color eye_color birth_year sex
5186
+ ## <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr>
5187
+ ## 1 Luke ~ 172 77 blond fair blue 19 male
5188
+ ## 2 C-3PO 167 75 <NA> gold yellow 112 none
5189
+ ## 3 R2-D2 96 32 <NA> white, bl~ red 33 none
5190
+ ## 4 Darth~ 202 136 none white yellow 41.9 male
5191
+ ## 5 Leia ~ 150 49 brown light brown 19 fema~
5192
+ ## 6 Owen ~ 178 120 brown, gr~ light blue 52 male
5193
+ ## # i 6 more variables: gender <chr>, homeworld <chr>, species <chr>,
5194
+ ## # films <list>, vehicles <list>, starships <list>
4608
5195
  \end{verbatim}
4609
5196
 
4610
5197
  The grouped\_mean function below will receive a grouping variable and
@@ -4635,8 +5222,8 @@ calculate summaries for the value\_variables given:
4635
5222
  ## # Auto named with `tibble::lst()`: tibble::lst(mean, median)
4636
5223
  ##
4637
5224
  ## # Using lambdas list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
4638
- ## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
4639
- ## generated.
5225
+ ## Call `lifecycle::last_lifecycle_warnings()` to see where this warning
5226
+ ## was generated.
4640
5227
  \end{verbatim}
4641
5228
 
4642
5229
  \begin{Shaded}
@@ -4672,11 +5259,19 @@ The same code with Galaaz, becomes:
4672
5259
  \NormalTok{ data}\AttributeTok{.}
4673
5260
  \AttributeTok{group\_by\_at}\NormalTok{(grouping\_variables)}\AttributeTok{.}
4674
5261
  \AttributeTok{mutate}\NormalTok{(}\WarningTok{count:} \ConstantTok{E}\AttributeTok{.n}\NormalTok{)}\AttributeTok{.}
4675
- \AttributeTok{summarise\_at}\NormalTok{(}\ConstantTok{E}\AttributeTok{.c}\NormalTok{(value\_variables, }\StringTok{"count"}\NormalTok{), }\OperatorTok{\textasciitilde{}}\WarningTok{:mean}\NormalTok{, }\WarningTok{na\_\_rm:} \DecValTok{true}\NormalTok{)}\AttributeTok{.}
4676
- \AttributeTok{rename\_at}\NormalTok{(value\_variables, }\ConstantTok{E}\AttributeTok{.funs}\NormalTok{(}\ConstantTok{E}\AttributeTok{.paste0}\NormalTok{(}\StringTok{"mean\_"}\NormalTok{, value\_variables)))}
5262
+ \AttributeTok{summarise\_at}\NormalTok{(}
5263
+ \ConstantTok{E}\AttributeTok{.c}\NormalTok{(value\_variables, }\StringTok{"count"}\NormalTok{),}
5264
+ \OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:mean}\KeywordTok{]}\NormalTok{,}
5265
+ \WarningTok{na\_\_rm:} \DecValTok{true}\NormalTok{)}\AttributeTok{.}
5266
+ \AttributeTok{rename\_at}\NormalTok{(}
5267
+ \NormalTok{ value\_variables,}
5268
+ \ConstantTok{E}\AttributeTok{.funs}\NormalTok{(}\ConstantTok{E}\AttributeTok{.paste0}\NormalTok{(}\StringTok{"mean\_"}\NormalTok{, value\_variables)))}
4677
5269
  \ControlFlowTok{end}
4678
5270
 
4679
- \FunctionTok{puts}\NormalTok{ grouped\_mean((}\OperatorTok{\textasciitilde{}}\WarningTok{:starwars}\NormalTok{), }\StringTok{"eye\_color"}\NormalTok{, }\ConstantTok{E}\AttributeTok{.c}\NormalTok{(}\StringTok{"mass"}\NormalTok{, }\StringTok{"birth\_year"}\NormalTok{))}
5271
+ \FunctionTok{puts}\NormalTok{ grouped\_mean(}
5272
+ \NormalTok{ (}\OperatorTok{\textasciitilde{}}\ConstantTok{R}\KeywordTok{[}\WarningTok{:starwars}\KeywordTok{]}\NormalTok{),}
5273
+ \StringTok{"eye\_color"}\NormalTok{,}
5274
+ \ConstantTok{E}\AttributeTok{.c}\NormalTok{(}\StringTok{"mass"}\NormalTok{, }\StringTok{"birth\_year"}\NormalTok{))}
4680
5275
  \end{Highlighting}
4681
5276
  \end{Shaded}
4682
5277
 
@@ -4718,8 +5313,9 @@ function-specific arguments.
4718
5313
  (\texttt{git\ checkout\ -b\ my-new-feature})
4719
5314
  \item
4720
5315
  Write tests --- use \textbf{\texttt{bin/run\_rspec}} or
4721
- \textbf{\texttt{bin/run\_all\_rspec}} with \textbf{JRuby} so JVM flags
4722
- and the load path match \textbf{\texttt{docs/testing.md}}
5316
+ \textbf{\texttt{bin/run\_all\_rspec}} (JRuby or CRuby via
5317
+ \textbf{\texttt{GALAAZ\_RUBY}}) so the load path matches
5318
+ \textbf{\texttt{docs/testing.md}}
4723
5319
  \item
4724
5320
  Commit your changes
4725
5321
  (\texttt{git\ commit\ -am\ \textquotesingle{}Add\ some\ feature\textquotesingle{}})