galaaz 0.4.9 → 0.4.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +798 -285
  3. data/blogs/galaaz_ggplot/galaaz_ggplot.Rmd +3 -12
  4. data/blogs/galaaz_ggplot/galaaz_ggplot.aux +5 -7
  5. data/blogs/galaaz_ggplot/galaaz_ggplot.html +69 -29
  6. data/blogs/galaaz_ggplot/galaaz_ggplot.pdf +0 -0
  7. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/midwest_rb.png +0 -0
  8. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/scatter_plot_rb.png +0 -0
  9. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-latex/midwest_rb.pdf +0 -0
  10. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-latex/scatter_plot_rb.pdf +0 -0
  11. data/blogs/galaaz_ggplot/midwest.Rmd +1 -9
  12. data/blogs/gknit/gknit.Rmd +37 -40
  13. data/blogs/gknit/gknit.html +32 -30
  14. data/blogs/gknit/gknit.md +36 -37
  15. data/blogs/gknit/gknit.pdf +0 -0
  16. data/blogs/gknit/gknit.tex +35 -37
  17. data/blogs/manual/manual.Rmd +548 -125
  18. data/blogs/manual/manual.html +509 -286
  19. data/blogs/manual/manual.md +798 -285
  20. data/blogs/manual/manual.pdf +0 -0
  21. data/blogs/manual/manual.tex +2816 -0
  22. data/blogs/manual/manual_files/figure-latex/diverging_bar.pdf +0 -0
  23. data/blogs/nse_dplyr/nse_dplyr.Rmd +240 -74
  24. data/blogs/nse_dplyr/nse_dplyr.html +191 -87
  25. data/blogs/nse_dplyr/nse_dplyr.md +361 -107
  26. data/blogs/nse_dplyr/nse_dplyr.pdf +0 -0
  27. data/blogs/nse_dplyr/nse_dplyr.tex +1373 -0
  28. data/blogs/ruby_plot/ruby_plot.Rmd +61 -81
  29. data/blogs/ruby_plot/ruby_plot.html +54 -57
  30. data/blogs/ruby_plot/ruby_plot.md +48 -67
  31. data/blogs/ruby_plot/ruby_plot.pdf +0 -0
  32. data/blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.png +0 -0
  33. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_delivery.png +0 -0
  34. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_dose.png +0 -0
  35. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color.png +0 -0
  36. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color2.png +0 -0
  37. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_jitter.png +0 -0
  38. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_points.png +0 -0
  39. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_box_plot.png +0 -0
  40. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_violin_plot.png +0 -0
  41. data/blogs/ruby_plot/ruby_plot_files/figure-html/violin_with_jitter.png +0 -0
  42. data/blogs/ruby_plot/ruby_plot_files/figure-latex/dose_len.png +0 -0
  43. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_delivery.png +0 -0
  44. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_dose.png +0 -0
  45. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color.png +0 -0
  46. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color2.png +0 -0
  47. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_decorations.png +0 -0
  48. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_jitter.png +0 -0
  49. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_points.png +0 -0
  50. data/blogs/ruby_plot/ruby_plot_files/figure-latex/final_box_plot.png +0 -0
  51. data/blogs/ruby_plot/ruby_plot_files/figure-latex/final_violin_plot.png +0 -0
  52. data/blogs/ruby_plot/ruby_plot_files/figure-latex/violin_with_jitter.png +0 -0
  53. data/lib/R_interface/rdata_frame.rb +0 -12
  54. data/lib/R_interface/robject.rb +14 -14
  55. data/lib/R_interface/ruby_extensions.rb +3 -31
  56. data/lib/R_interface/rvector.rb +0 -12
  57. data/lib/gknit/knitr_engine.rb +5 -3
  58. data/lib/util/exec_ruby.rb +22 -61
  59. data/specs/tmp.rb +26 -12
  60. data/version.rb +1 -1
  61. metadata +22 -17
  62. data/bin/gknit_old_r +0 -236
  63. data/blogs/dev/dev.Rmd +0 -23
  64. data/blogs/dev/dev.md +0 -58
  65. data/blogs/dev/dev2.Rmd +0 -65
  66. data/blogs/dev/model.rb +0 -41
  67. data/blogs/dplyr/dplyr.Rmd +0 -29
  68. data/blogs/dplyr/dplyr.html +0 -433
  69. data/blogs/dplyr/dplyr.md +0 -58
  70. data/blogs/dplyr/dplyr.rb +0 -63
  71. data/blogs/galaaz_ggplot/galaaz_ggplot.log +0 -640
  72. data/blogs/galaaz_ggplot/galaaz_ggplot.md +0 -431
  73. data/blogs/galaaz_ggplot/galaaz_ggplot.tex +0 -481
  74. data/blogs/galaaz_ggplot/midwest.png +0 -0
  75. data/blogs/galaaz_ggplot/scatter_plot.png +0 -0
  76. data/blogs/ruby_plot/ruby_plot.tex +0 -1077
@@ -121,10 +121,7 @@ Ruby and dozens of other languages. Yet, while R and Python chunks can share da
121
121
  languages, chunks are independent. This means that a variable defined in one chunk
122
122
  cannot be used in another chunk.
123
123
 
124
- With _gKnit_ Ruby code chunks can share data. In gKnit each
125
- Ruby chunk executes in its own scope and thus, local variable defined in a chunk are
126
- not accessible by other chunks. Yet, All chunks execute in the scope of a 'chunk'
127
- class and instance variables ('@'), are available in all chunks.
124
+ With _gKnit_ Ruby code chunks can share data.
128
125
 
129
126
  # Exploring the Dataset
130
127
 
@@ -139,17 +136,17 @@ in 60 guinea pigs, where each animal received one of three dose levels of Vitami
139
136
  The ToothGrowth dataset contains three columns: 'len', 'supp' and 'dose'. Let's
140
137
  take a look at a few rows of this dataset. In Galaaz, R variables are accessed
141
138
  by using the corresponding Ruby symbol preceeded by the tilda ('~') function. Note in the
142
- following chunk that 'ToothGrowth' is the R variable and Ruby's '@tooth_growth' is
139
+ following chunk that 'ToothGrowth' is the R variable and Ruby's 'tooth_growth' is
143
140
  assigned the value of '~:ToothGrowth'.
144
141
 
145
142
 
146
143
  ```ruby
147
144
  # Read the R ToothGrowth variable and assign it to the
148
- # Ruby instance variable @tooth_growth that will be
145
+ # Ruby instance variable tooth_growth that will be
149
146
  # available to all Ruby chunks in this document.
150
- @tooth_growth = ~:ToothGrowth
147
+ tooth_growth = ~:ToothGrowth
151
148
  # print the first few elements of the dataset
152
- puts @tooth_growth.head
149
+ puts tooth_growth.head
153
150
  ```
154
151
 
155
152
  ```
@@ -171,7 +168,7 @@ in usual Ruby style.
171
168
  ```ruby
172
169
  # Access the tooth_growth 'len' column and print the first few
173
170
  # elements of this column with the 'head' method.
174
- puts @tooth_growth.len.head
171
+ puts tooth_growth.len.head
175
172
  ```
176
173
 
177
174
  ```
@@ -188,7 +185,7 @@ The function 'as.factor' becomes 'R.as__factor' or just 'as__factor' when chaini
188
185
 
189
186
  ```ruby
190
187
  # convert the dose to a factor
191
- @tooth_growth.dose = @tooth_growth.dose.as__factor
188
+ tooth_growth.dose = tooth_growth.dose.as__factor
192
189
  ```
193
190
 
194
191
  Let's explore some more details of this dataset. In particular, let's look at its dimensions,
@@ -196,7 +193,7 @@ structure and summary statistics.
196
193
 
197
194
 
198
195
  ```ruby
199
- puts @tooth_growth.dim
196
+ puts tooth_growth.dim
200
197
  ```
201
198
 
202
199
  ```
@@ -211,7 +208,7 @@ as a side effect.
211
208
 
212
209
 
213
210
  ```ruby
214
- @tooth_growth.str
211
+ tooth_growth.str
215
212
  ```
216
213
 
217
214
  ```
@@ -227,7 +224,7 @@ Finally, using the summary method, we get the statistical summary for the datase
227
224
 
228
225
 
229
226
  ```ruby
230
- puts @tooth_growth.summary
227
+ puts tooth_growth.summary
231
228
  ```
232
229
 
233
230
  ```
@@ -259,36 +256,23 @@ to form the final graphics.
259
256
 
260
257
  In order to make a plot, we use the 'ggplot' function to the dataset. In R, this would be
261
258
  written as ```ggplot(<dataset>, ...)```. Galaaz gives you the flexibility to use
262
- either ```R.ggplot(<dataset>, ...)``` or ```<dataset>.ggplot(...)```. In the graph s
263
- pecification bellow, we use the second notation
264
- that looks more like Ruby. ggplot uses the ‘aes’ method to specify
259
+ either ```R.ggplot(<dataset>, ...)``` or ```<dataset>.ggplot(...)```. In the graph
260
+ specification bellow, we use the second notation that looks more like Ruby.
261
+ ggplot uses the ‘aes’ method to specify
265
262
  x and y axes; in this case, the 'dose' on the $x$ axis and the 'length' on
266
263
  the $y$ axis: 'E.aes(x: :dose, y: :len)'. To specify the type of plot add a geom to
267
264
  the plot. For a boxplot, the geom is R.geom_boxplot.
268
265
 
269
- Note also that we have a call to 'R.png' before plotting and 'R.dev__off' after the print
270
- statement. 'R.png' opens a 'png device' for outputting the plot. If we do no pass a
271
- name to the 'png' function, the
272
- image gets a default name of 'Rplot\<nnn\>' where \<nnn\> is the number of the plot.
273
- 'R.dev__off'
274
- closes the device and creates the 'png' file. We can
275
- then include the generated 'png' file in the document by adding an rmarkdown directive.
276
-
277
266
 
278
267
  ```ruby
279
268
  require 'ggplot'
280
269
 
281
- e = @tooth_growth.ggplot(E.aes(x: :dose, y: :len))
270
+ e = tooth_growth.ggplot(E.aes(x: :dose, y: :len))
282
271
  print e + R.geom_boxplot
283
272
  ```
284
273
 
285
274
  ![](/home/rbotafogo/desenv/galaaz/blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.png)<!-- -->
286
275
 
287
- [//]: # (Including the 'png' file generated above. In future releases)
288
- [//]: # (of gKnit, the figures should be automatically saved and the name)
289
- [//]: # (taken from the chunk 'label' and possibly chunk parameters)
290
-
291
-
292
276
  Great! We've just managed to create and save our first plot in Ruby with only
293
277
  four lines of code. We can now easily see with this plot a clear trend: as the
294
278
  dose of the supplement
@@ -308,32 +292,28 @@ automatically creates the facets based on the delivery method factors. The para
308
292
  the 'facet_grid' method is a [_formula_](https://thomasleeper.com/Rcourse/Tutorials/formulae.html).
309
293
 
310
294
  In Galaaz we give programmers the flexibility to use two different ways to write formulas.
311
- In the first way, the following changes from writing formulas (for example 'x ~ y')
312
- in R are necessary:
295
+ In the first way, we use Ruby expressions and the '.til' function. The formula 'x ~ y', becomes
296
+ ':x.til :y'. More information on expressions can be found in [Galaaz Manual](https://www.rubydoc.info/gems/galaaz/).
313
297
 
314
- * R symbols are represented by the same Ruby symbol prefixed with the '+' method. The
315
- symbol ```x``` in R becomes ```+:x``` in Ruby;
316
- * The '~' operator in R becomes '=~' in Ruby. The formula ```x ~ y``` in R is written as
317
- ```+:x =~ +:y``` in Ruby;
318
- * The '.' symbol in R becomes '+:all'
319
298
 
320
299
  Another way of writing a formula is to use the 'formula' function with the actual formula as
321
300
  a string. The formula ```x ~ y``` in R can be written as ```R.formula("x ~ y")```. For more
322
301
  complex formulas, the use of the 'formula' function is preferred.
323
302
 
324
- The formula ```+:all =~ +:supp``` indicates to the 'facet_grid' function that it needs to
303
+ The formula ```:all.til :supp``` indicates to the 'facet_grid' function that it needs to
325
304
  facet the plot based on the ```supp``` variable and split the plot vertically. Changing
326
- the formula to ```+:supp =~ +:all``` would split the plot horizontally.
305
+ the formula to ```:supp.til :all``` would split the plot horizontally.
327
306
 
328
307
 
329
308
  ```ruby
330
- @base_tooth = @tooth_growth.ggplot(E.aes(x: :dose, y: :len, group: :dose))
331
309
 
332
- @bp = @base_tooth + R.geom_boxplot +
310
+ base_tooth = tooth_growth.ggplot(E.aes(x: :dose, y: :len, group: :dose))
311
+
312
+ bp = base_tooth + R.geom_boxplot +
333
313
  # Split in vertical direction
334
314
  R.facet_grid(:all.til :supp)
335
315
 
336
- puts @bp
316
+ puts bp
337
317
  ```
338
318
 
339
319
 
@@ -355,8 +335,8 @@ factor gets its own color.
355
335
 
356
336
 
357
337
  ```ruby
358
- @bp = @bp + R.geom_boxplot(E.aes(fill: :dose))
359
- puts @bp
338
+ bp = bp + R.geom_boxplot(E.aes(fill: :dose))
339
+ puts bp
360
340
  ```
361
341
 
362
342
 
@@ -394,9 +374,9 @@ plot.
394
374
 
395
375
  ```ruby
396
376
  # Split in vertical direction
397
- @bp = @bp + R.geom_point
377
+ bp = bp + R.geom_point
398
378
 
399
- puts @bp
379
+ puts bp
400
380
  ```
401
381
 
402
382
 
@@ -416,7 +396,7 @@ color and change the shape of the points, making them even easier to see.
416
396
 
417
397
  ```ruby
418
398
  # Split in vertical direction
419
- puts @bp + R.geom_jitter(shape: 23, color: "cyan3", size: 1)
399
+ puts bp + R.geom_jitter(shape: 23, color: "cyan3", size: 1)
420
400
  ```
421
401
 
422
402
 
@@ -457,11 +437,11 @@ matches with the actual order of the colors in the plot.
457
437
 
458
438
 
459
439
  ```ruby
460
- @bp = @bp +
461
- R.scale_fill_manual(values: R.c("cyan", "deepskyblue", "deepskyblue4"),
440
+ bp = bp +
441
+ R.scale_fill_manual(values: R.c("cyan", "deepskyblue", "deepskyblue4"),
462
442
  breaks: R.c("2","1","0.5"))
463
443
 
464
- puts @bp
444
+ puts bp
465
445
  ```
466
446
 
467
447
 
@@ -486,13 +466,13 @@ a boxplot known as a _violin plot_ with jittered data.
486
466
 
487
467
 
488
468
  ```ruby
489
- @violin = @base_tooth + R.geom_violin(E.aes(fill: :dose)) +
469
+ violin = base_tooth + R.geom_violin(E.aes(fill: :dose)) +
490
470
  R.facet_grid(:all.til :supp) +
491
471
  R.geom_jitter(shape: 23, color: "cyan3", size: 1) +
492
472
  R.scale_fill_manual(values: R.c("cyan", "deepskyblue", "deepskyblue4"),
493
473
  breaks: R.c("2","1","0.5"))
494
474
 
495
- puts @violin
475
+ puts violin
496
476
  ```
497
477
 
498
478
 
@@ -523,13 +503,13 @@ Length of odontoblasts in 60 guinea pigs.
523
503
  Each animal received one of three dose levels of vitamin C.
524
504
  EOT
525
505
 
526
- @decorations =
506
+ decorations =
527
507
  R.labs(title: "Tooth Growth: Length vs Vitamin C Dose",
528
508
  subtitle: "Faceted by delivery method, OJ or VC",
529
509
  x: "Dose (mg)", y: "Teeth length",
530
510
  caption: caption)
531
511
 
532
- puts @bp + @decorations
512
+ puts bp + decorations
533
513
  ```
534
514
 
535
515
 
@@ -609,14 +589,14 @@ We can now easily make our final boxplot and violin plot. All the layers for th
609
589
  added in order to expose our understanding of the data and the need to present the result
610
590
  to our audience.
611
591
 
612
- The final specification is just the addition of all layers build up to this point (@bp), plus
613
- the decorations (@decorations), plus the corporate theme.
592
+ The final specification is just the addition of all layers build up to this point ('bp'), plus
593
+ the decorations ('decorations'), plus the corporate theme.
614
594
 
615
595
  Here is our final boxplot, without jitter.
616
596
 
617
597
 
618
598
  ```ruby
619
- puts @bp + @decorations + CorpTheme.global_theme(faceted: true)
599
+ puts bp + decorations + CorpTheme.global_theme(faceted: true)
620
600
  ```
621
601
 
622
602
 
@@ -627,7 +607,7 @@ boxplot.
627
607
 
628
608
 
629
609
  ```ruby
630
- puts @violin + @decorations + CorpTheme.global_theme(faceted: true)
610
+ puts violin + decorations + CorpTheme.global_theme(faceted: true)
631
611
  ```
632
612
 
633
613
 
@@ -646,15 +626,16 @@ Length of odontoblasts in 60 guinea pigs.
646
626
  Each animal received one of three dose levels of vitamin C.
647
627
  EOT
648
628
 
649
- @bp = @tooth_growth.ggplot(E.aes(x: :supp, y: :len, group: :supp)) +
650
- R.geom_boxplot(E.aes(fill: :supp)) + R.facet_grid(:all.til :dose) +
651
- R.scale_fill_manual(values: R.c("cyan", "deepskyblue4")) +
652
- R.labs(title: "Tooth Growth: Length by Dose",
653
- subtitle: "Faceted by dose",
654
- x: "Delivery method", y: "Teeth length",
655
- caption: caption) +
656
- CorpTheme.global_theme(faceted: true)
657
- puts @bp
629
+ bp = tooth_growth.ggplot(E.aes(x: :supp, y: :len, group: :supp)) +
630
+ R.geom_boxplot(E.aes(fill: :supp)) + R.facet_grid(:all.til :dose) +
631
+ R.scale_fill_manual(values: R.c("cyan", "deepskyblue4")) +
632
+ R.labs(title: "Tooth Growth: Length by Dose",
633
+ subtitle: "Faceted by dose",
634
+ x: "Delivery method", y: "Teeth length",
635
+ caption: caption) +
636
+ CorpTheme.global_theme(faceted: true)
637
+
638
+ puts bp
658
639
  ```
659
640
 
660
641
 
@@ -97,18 +97,6 @@ module R
97
97
 
98
98
  end
99
99
 
100
- #=begin
101
- #--------------------------------------------------------------------------------------
102
- # @TODO Need to understand why to_ary is being called here and what the effect is of
103
- # returning the empty array. For now, doing this returns better error messages in
104
- # rspec
105
- #--------------------------------------------------------------------------------------
106
-
107
- def to_ary
108
- []
109
- end
110
- #=end
111
-
112
100
  end
113
101
 
114
102
  end
@@ -65,16 +65,6 @@ module R
65
65
  R::Support.exec_function_name("`==`", @r_interop,
66
66
  R::Support.parse_arg(other_object))
67
67
  end
68
-
69
- #--------------------------------------------------------------------------------------
70
- # @TODO: rspec sometimes calls to_ary when an expected value is false. I still don't
71
- # understand this call. Returning an array with a number inside seems to make
72
- # rspec happy, however this could have other consequences I don't know of.
73
- #--------------------------------------------------------------------------------------
74
-
75
- def to_ary
76
- [1]
77
- end
78
68
 
79
69
  #--------------------------------------------------------------------------------------
80
70
  # @param r_interop [Interop] pointer to an R object
@@ -133,18 +123,28 @@ module R
133
123
  end
134
124
 
135
125
  end
136
-
126
+
137
127
  #--------------------------------------------------------------------------------------
138
128
  #
139
129
  #--------------------------------------------------------------------------------------
140
130
 
141
131
  def method_missing(symbol, *args, &block)
142
132
  name = R::Support.convert_symbol2r(symbol)
143
- =begin
133
+
144
134
  # Need to raise a NoMethodError when method_missing is called by an implicit
145
- # call to "to_ary". I'm not sure why "to_ary" is being called, but it is
135
+ # call to "to_ary". The explanation for that is:
136
+ # Okay, I've found the source of the behaviour. IOOperations.puts will attempt to
137
+ # coerce an argument to an array and print its contents, and R::Vector responds to
138
+ # to_ary but returns the empty array which results in no output.
139
+ #
140
+ # Previously IOOperations.puts only checked the type of the argument, and did not
141
+ # attempt coercion, but that meant we didn't match MRI's behaviour and had some test
142
+ # failures in other gems. I'd suggest either removing the to_ary methods on
143
+ # R::Object and R::Vector or implementing them more fully. I see from the comments on
144
+ # those methods that you needed them for RSpec, it might worth seeing if those problems
145
+ # still occur and we can look a better way to resolve those.
146
146
  raise NoMethodError if name == "to_ary"
147
- =end
147
+
148
148
  case
149
149
  when block_given?
150
150
  R::Support.new_scope(symbol, self, *args, &block)
@@ -21,17 +21,6 @@
21
21
  # OR MODIFICATIONS.
22
22
  ##########################################################################################
23
23
 
24
- #=begin
25
- module Kernel
26
-
27
- def puts(*args)
28
- print(*args)
29
- print "\n"
30
- end
31
-
32
- end
33
- #=end
34
-
35
24
  #==========================================================================================
36
25
  #
37
26
  #==========================================================================================
@@ -173,29 +162,12 @@ class Symbol
173
162
  exec_bin_oper("`=`", expression)
174
163
  end
175
164
 
176
- =begin
177
- #--------------------------------------------------------------------------------------
178
- #
179
- #--------------------------------------------------------------------------------------
180
-
181
- def [](index)
182
- exec_bin_oper("`[[`", index)
183
- end
184
- =end
185
165
  #--------------------------------------------------------------------------------------
186
166
  #
187
167
  #--------------------------------------------------------------------------------------
188
168
 
189
169
  def method_missing(symbol, *args, &block)
190
170
 
191
- =begin
192
- if (symbol =~ /(.*)=$/)
193
- # method_missing_assign($1, args[0])
194
- elsif (args.length == 0 && ((R.c(symbol.to_s)._ :in, R.names(self)) >> 0))
195
- return self[symbol.to_s]
196
- end
197
- =end
198
-
199
171
  E.send(symbol.to_s, self, *args)
200
172
 
201
173
  end
@@ -217,7 +189,7 @@ class Symbol
217
189
  def inter(var2)
218
190
  R::Support.exec_function(R::Support.range, self, var2)
219
191
  end
220
-
192
+
221
193
  #--------------------------------------------------------------------------------------
222
194
  # If method_missing is implemented, then we also need to implement method 'to_ary'.
223
195
  # This is because starting from ruby 1.9 the code for Array#flatten has changed,
@@ -228,7 +200,7 @@ class Symbol
228
200
  #--------------------------------------------------------------------------------------
229
201
 
230
202
  def to_ary
231
- [self.to_s]
203
+ # [self.to_s]
232
204
  end
233
-
205
+
234
206
  end