galaaz 0.4.2 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +25 -0
  3. data/Rakefile +8 -0
  4. data/bin/gknit +9 -5
  5. data/bin/gstudio +4 -2
  6. data/bin/gstudio.rb +32 -2
  7. data/blogs/dev/dev.html +219 -34
  8. data/blogs/dev/dev.md +26 -26
  9. data/blogs/dev/dev_files/figure-html/bubble-1.png +0 -0
  10. data/blogs/dev/dev_files/figure-html/diverging_bar.png +0 -0
  11. data/blogs/dplyr/dplyr.rb +63 -0
  12. data/blogs/galaaz_ggplot/galaaz_ggplot.Rmd +38 -26
  13. data/blogs/galaaz_ggplot/galaaz_ggplot.aux +16 -17
  14. data/blogs/galaaz_ggplot/galaaz_ggplot.pdf +0 -0
  15. data/blogs/galaaz_ggplot/galaaz_ggplot.tex +65 -31
  16. data/blogs/oh_my/not_so.rb +2342 -0
  17. data/blogs/oh_my/oh_my.Rmd +493 -0
  18. data/blogs/oh_my/oh_my.html +680 -0
  19. data/blogs/oh_my/oh_my.md +597 -0
  20. data/blogs/oh_my/old.Rmd +2100 -0
  21. data/blogs/ruby_plot/figures/facets_with_decorations.png +0 -0
  22. data/blogs/ruby_plot/figures/facets_with_jitter.png +0 -0
  23. data/blogs/ruby_plot/figures/final_box_plot.png +0 -0
  24. data/blogs/ruby_plot/figures/final_violin_plot.png +0 -0
  25. data/blogs/ruby_plot/figures/violin_with_jitter.png +0 -0
  26. data/blogs/ruby_plot/ruby_plot.Rmd +147 -122
  27. data/blogs/ruby_plot/ruby_plot.Rmd_external_figs +662 -0
  28. data/blogs/ruby_plot/ruby_plot.html +49 -54
  29. data/blogs/ruby_plot/ruby_plot.md +147 -122
  30. data/blogs/ruby_plot/ruby_plot.pdf +0 -0
  31. data/blogs/ruby_plot/ruby_plot.tex +776 -157
  32. data/blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.svg +57 -0
  33. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_delivery.svg +106 -0
  34. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_dose.svg +110 -0
  35. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color.svg +174 -0
  36. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color2.svg +236 -0
  37. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_decorations.png +0 -0
  38. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_jitter.svg +296 -0
  39. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_points.svg +236 -0
  40. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_box_plot.svg +218 -0
  41. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_violin_plot.svg +128 -0
  42. data/blogs/ruby_plot/ruby_plot_files/figure-html/violin_with_jitter.svg +150 -0
  43. data/examples/islr/ch2.spec.rb +21 -18
  44. data/examples/islr/ch3_boston.rb +14 -5
  45. data/examples/islr/ch3_multiple_regression.rb +2 -3
  46. data/examples/islr/ch6.spec.rb +1 -1
  47. data/examples/islr/x_y_rnorm.jpg +0 -0
  48. data/lib/R_interface/r.rb +14 -10
  49. data/lib/R_interface/r_libs.R +9 -0
  50. data/lib/R_interface/r_methods.rb +77 -6
  51. data/lib/R_interface/{expression.rb → r_module_s.rb} +13 -14
  52. data/lib/R_interface/rbinary_operators.rb +58 -71
  53. data/lib/R_interface/rdata_frame.rb +2 -1
  54. data/lib/R_interface/rdevices.R +4 -0
  55. data/lib/R_interface/rdevices.rb +1 -1
  56. data/lib/R_interface/renvironment.rb +34 -1
  57. data/lib/R_interface/rexpression.rb +108 -2
  58. data/lib/R_interface/rindexed_object.rb +3 -1
  59. data/lib/R_interface/rlanguage.rb +18 -2
  60. data/lib/R_interface/rmatrix.rb +14 -0
  61. data/lib/R_interface/rmd_indexed_object.rb +5 -1
  62. data/lib/R_interface/robject.rb +61 -23
  63. data/lib/R_interface/rsupport.rb +111 -53
  64. data/lib/R_interface/rsymbol.rb +6 -5
  65. data/lib/R_interface/ruby_extensions.rb +130 -4
  66. data/lib/R_interface/runary_operators.rb +35 -3
  67. data/lib/R_interface/rvector.rb +1 -0
  68. data/lib/galaaz.rb +0 -2
  69. data/lib/gknit/knitr_engine.rb +58 -4
  70. data/lib/gknit/ruby_engine.rb +5 -6
  71. data/lib/util/exec_ruby.rb +55 -9
  72. data/specs/all.rb +13 -3
  73. data/specs/figures/dose_len.png +0 -0
  74. data/specs/r_dataframe.spec.rb +49 -26
  75. data/specs/r_environment.spec.rb +140 -0
  76. data/specs/r_eval.spec.rb +0 -15
  77. data/specs/r_formula.spec.rb +232 -0
  78. data/specs/r_function.spec.rb +7 -8
  79. data/specs/r_list.spec.rb +4 -0
  80. data/specs/r_list_apply.spec.rb +11 -11
  81. data/specs/r_matrix.spec.rb +3 -3
  82. data/specs/{r_plots.spec.rb~ → r_nse.spec.rb} +29 -6
  83. data/specs/r_vector_creation.spec.rb +6 -0
  84. data/specs/r_vector_object.spec.rb +2 -2
  85. data/specs/r_vector_operators.spec.rb +3 -3
  86. data/specs/r_vector_subsetting.spec.rb +4 -4
  87. data/specs/ruby_expression.spec.rb +324 -0
  88. data/specs/tmp.rb +12 -524
  89. data/sty/galaaz.sty +71 -0
  90. data/version.rb +1 -1
  91. metadata +31 -41
  92. data/bin/gknit2~ +0 -6
  93. data/bin/ogk~ +0 -4
  94. data/bin/prepareR.rb~ +0 -1
  95. data/blogs/dev/dev.Rmd~ +0 -104
  96. data/blogs/galaaz_ggplot/galaaz_ggplot.dvi +0 -0
  97. data/blogs/galaaz_ggplot/midwest_external_png~ +0 -1
  98. data/blogs/gknit/gknit.Rmd~ +0 -184
  99. data/blogs/gknit/gknit.Rnd~ +0 -17
  100. data/blogs/gknit/model.rb~ +0 -46
  101. data/blogs/ruby_plot/ruby_plot.Rmd~ +0 -215
  102. data/examples/islr/Figure.jpg +0 -0
  103. data/examples/misc/moneyball.rb~ +0 -16
  104. data/examples/misc/subsetting.rb~ +0 -372
  105. data/lib/R/eng_ruby.R~ +0 -63
  106. data/lib/R_interface/capture_plot.rb~ +0 -23
  107. data/lib/R_interface/r.rb~ +0 -121
  108. data/lib/R_interface/rdevices.rb~ +0 -27
  109. data/lib/gknit.rb~ +0 -26
  110. data/lib/gknit/knitr_engine.rb~ +0 -102
  111. data/lib/gknit/ruby_engine.rb~ +0 -72
  112. data/lib/util/inline_file.rb~ +0 -23
  113. data/r_requires/knitr.rb~ +0 -4
  114. data/specs/r_language.spec.rb +0 -157
@@ -21,534 +21,22 @@
21
21
  # OR MODIFICATIONS.
22
22
  ##########################################################################################
23
23
 
24
- require 'galaaz'
25
- #require 'ggplot'
26
-
27
- # Read the R ToothGrowth variable and assign it to the
28
- # Ruby tooth_growth variable
29
- tooth_growth = ~:ToothGrowth
30
- # convert the dose to a factor
31
- tooth_growth.dose = tooth_growth.dose.as__factor
32
-
33
- # print the first few elements of the dataset
34
- puts tooth_growth.head
35
- puts tooth_growth.dim
36
- puts tooth_growth.str
37
- puts tooth_growth.summary
38
-
39
-
40
- =begin
41
- module CorpTheme
42
-
43
- # face can be (1=plain, 2=bold, 3=italic, 4=bold-italic)
44
- def self.text_element(size, face = "plain")
45
- E.element_text(color: "#000080",
46
- face: face,
47
- size: size,
48
- hjust: 1)
49
- end
50
-
51
- #---------------------------------------------------------------------------------
52
- # Defines the plot theme (visualization). In this theme we remove major and minor
53
- # grids, borders and background. We also turn-off scientific notation.
54
- #---------------------------------------------------------------------------------
55
-
56
- def self.global_theme(faceted = false)
57
-
58
- R.options(scipen: 999) # turn-off scientific notation like 1e+48
59
-
60
- # remove major grids
61
- gb = R.theme(panel__grid__major: E.element_blank())
62
- # remove minor grids
63
- gb = gb + R.theme(panel__grid__minor: E.element_blank)
64
- # gb = R.theme(panel__grid__minor: E.element_blank)
65
- # remove border
66
- gb = gb + R.theme(panel__border: E.element_blank)
67
- # remove background. When working with faceted graphs, the background makes
68
- # it easier to see each facet, so leave it
69
- gb = gb + R.theme(panel__background: E.element_blank) if !faceted
70
- # Change axis font
71
- gb = gb + R.theme(axis__text: text_element(8))
72
- # change axis title font
73
- gb = gb + R.theme(axis__title: text_element(10, "plain"))
74
- # change font of title
75
- gb = gb + R.theme(title: text_element(12, "bold"))
76
- # change font of captions
77
- gb = gb + R.theme(plot__caption: text_element(8))
78
-
79
- end
80
-
81
- end
82
-
83
- #R.png("final.png")
84
- R.awt
85
-
86
- # Read the R ToothGrowth variable and assign it to the
87
- # Ruby tooth_growth variable
88
- tooth_growth = ~:ToothGrowth
89
- # convert the dose to a factor
90
- tooth_growth.dose = tooth_growth.dose.as__factor
91
-
92
- # print the first few elements of the dataset
93
- puts tooth_growth.head
94
-
95
- # Create the basic plot
96
- bp = tooth_growth.ggplot(E.aes(x: :dose, y: :len, group: :dose)) +
97
- R.geom_boxplot(E.aes(fill: :dose)) + R.geom_point
98
-
99
- caption = <<-EOT
100
- Length of odontoblasts in 60 guinea pigs.
101
- Each animal received one of three dose levels of vitamin C.
102
- EOT
103
-
104
- # Facet the plot by the s
105
- bp = bp + R.facet_grid(+:all =~ +:supp) +
106
- R.labs(title: "Tooth Growth: Length by Dose",
107
- subtitle: "Faceted by delivery method, Orange Juince (OJ) or Ascorbic Acid (VC)",
108
- x: "Dose (mg)", y: "Teeth length",
109
- caption: caption) +
110
- CorpTheme.global_theme(faceted: true)
111
-
112
- puts bp
113
-
114
- # CorpTheme.layout("Plot of length by dose", "subtitle", bp)
115
-
116
- a = gets.chomp
117
- sleep(20)
118
-
119
- R.dev__off
120
- =end
121
-
122
- =begin
123
-
124
- awt1 = R::Device.new('awt')
125
- awt1.open
126
-
127
- awt2 = R::Device.new('awt')
128
- awt2.open
129
-
130
- puts R.dev__list
131
-
132
- sleep(10)
133
-
134
- awt1.close
135
-
136
- puts R.dev__list
137
-
138
- # puts R::Devices.list
139
- =end
140
-
141
- =begin
142
- #awt1 = R::Device.new('awt')
143
- #awt1.open
144
-
145
- b = R.ggplot(:mpg, E.aes(:fl))
146
-
147
- # Basic plot
148
- print b + R.geom_bar
149
-
150
- awt2 = R::Device.new('awt')
151
- awt2.open
152
-
153
- # Change fill color
154
- print b + R.geom_bar(fill: "steelblue", color: "steelblue") +
155
- R.theme_minimal
156
-
157
- sleep(5)
158
-
159
- awt1.close
160
-
161
- sleep(5)
162
- =end
163
-
164
-
165
-
166
- =begin
167
- R.theme_set R.theme_bw
168
-
169
- # Data Prep
170
- mtcars = ~:mtcars
171
- mtcars.car_name = R.rownames(:mtcars)
172
- # compute normalized mpg
173
- mtcars.mpg_z = ((mtcars.mpg - mtcars.mpg.mean)/mtcars.mpg.sd).round 2
174
- mtcars.mpg_type = mtcars.mpg_z < 0 ? "below" : "above"
175
- mtcars = mtcars[mtcars.mpg_z.order, :all]
176
- # convert to factor to retain sorted order in plot
177
- mtcars.car_name = mtcars.car_name.factor levels: mtcars.car_name
178
-
179
- # Diverging Barcharts
180
- gg = mtcars.ggplot(E.aes(x: :car_name, y: :mpg_z, label: :mpg_z)) +
181
- R.geom_bar(E.aes(fill: :mpg_type), stat: 'identity', width: 0.5) +
182
- R.scale_fill_manual(name: "Mileage",
183
- labels: R.c("Above Average", "Below Average"),
184
- values: R.c("above": "#00ba38", "below": "#f8766d")) +
185
- R.labs(subtitle: "Normalised mileage from 'mtcars'",
186
- title: "Diverging Bars") +
187
- R.coord_flip()
188
-
189
- p gg
190
- puts gg.rclass
191
- gg.rclass = "list"
192
- puts gg
193
- =end
194
-
195
- #lst = gg.as__list
196
- #lst.rclass = "list"
197
- #puts lst.rclass
198
- #puts lst
199
-
200
- =begin
201
- R::Support.eval(<<-R)
202
-
203
- library(ggplot2)
204
- library(ggcorrplot)
205
-
206
- # Correlation matrix
207
- data(mtcars)
208
- corr <- round(cor(mtcars), 1)
209
-
210
- # Plot
211
- gg = ggcorrplot(corr, hc.order = TRUE,
212
- type = "lower",
213
- lab = TRUE,
214
- lab_size = 3,
215
- method="circle",
216
- colors = c("tomato2", "white", "springgreen3"),
217
- title="Correlogram of mtcars",
218
- ggtheme=theme_bw)
219
-
220
- lst = as.list(gg)
221
- print(lst[[1]])
222
- R
223
- =end
224
- =begin
225
- # mtcars$`car name` <- rownames(mtcars) # create new column for car names
226
- # mtcars$mpg_z <- round((mtcars$mpg - mean(mtcars$mpg))/sd(mtcars$mpg), 2) # compute normalized mpg
227
- # mtcars$mpg_type <- ifelse(mtcars$mpg_z < 0, "below", "above") # above / below avg flag
228
- # mtcars <- mtcars[order(mtcars$mpg_z), ] # sort
229
- mtcars$`car name` <- factor(mtcars$`car name`, levels = mtcars$`car name`) # convert to factor to retain sorted order in plot.
230
-
231
- # Diverging Barcharts
232
- ggplot(mtcars, aes(x=`car name`, y=mpg_z, label=mpg_z)) +
233
- geom_bar(stat='identity', aes(fill=mpg_type), width=.5) +
234
- scale_fill_manual(name="Mileage",
235
- labels = c("Above Average", "Below Average"),
236
- values = c("above"="#00ba38", "below"="#f8766d")) +
237
- labs(subtitle="Normalised mileage from 'mtcars'",
238
- title= "Diverging Bars") +
239
- coord_flip()
240
-
241
- =end
242
-
243
- # dir = File.dirname(File.expand_path('.', __FILE__))
244
- # GalaazUtil.inline_file("awsome_print", "require ")
245
-
246
-
247
-
248
-
249
-
250
- =begin
251
- R::Support.eval(<<-R)
252
- old_dispatch = UseMethod;
253
- print(old_dispatch);
254
-
255
- print(typeof(UseMethod));
256
-
257
- UseMethod = function(x, ...) {
258
- print("in local UseMethod");
259
- print(x);
260
- print(...);
261
- old_dispatch(x, ...);
262
- }
263
-
264
- vec = c(1, 2, 3);
265
- print(vec);
266
- print(mean(vec));
267
- # UseMethod("mean", vec);
268
-
269
- R
270
- =end
271
-
24
+ # naturgy soluções
272
25
 
26
+ require 'galaaz'
27
+ # require 'ggplot'
273
28
 
29
+ formula = :y.til :x + E.I(:x**2)
30
+ puts formula
31
+ puts formula.ast
274
32
 
33
+ formula = :y.til :x + :x**2
34
+ puts formula
35
+ puts formula.ast
275
36
 
276
-
277
-
278
-
279
-
280
-
281
-
282
- =begin
283
- R::Support.eval(<<-R)
284
- print.polyglot.value = function(poly) {
285
- print("printing external");
286
- print(poly$puts());
287
- }
288
-
289
- R
290
-
291
- ext = R::Support.eval(<<-R)
292
- function(data) {
293
- print(data);
294
- }
295
- R
296
-
297
- class User
298
-
299
- attr_reader :id
300
- attr_reader :name
301
-
302
- def initialize
303
- @id = 1
304
- @name = ["a", "b", "c"]
305
- end
306
-
307
- def method_missing(symbol, *args)
308
- p symbol
309
- p args
310
- end
311
-
312
- def puts(*args)
313
- @id
314
- end
315
-
316
- end
317
-
318
- ext.call(User.new)
319
- =end
320
-
321
- =begin
322
- class ArrayEmul
323
- attr_reader :array
324
-
325
- def initialize
326
- @array = []
327
- end
328
-
329
- def method_missing(symbol, *args)
330
- @array.send(symbol, *args)
331
- end
332
-
333
- def to_s
334
- @array.to_s
335
- end
336
-
337
- def pp
338
- puts @array.to_s
339
- end
340
-
341
- def fetch(index)
342
- @array[index[0]]
343
- end
344
-
345
- end
346
-
347
- make_obj = Polyglot.eval("R", <<-R)
348
- function(ruby_obj) {
349
- x = list(ruby_obj);
350
- attr(x, "class") = "ruby_obj";
351
- x;
352
- }
353
- R
354
-
355
- rf = Polyglot.eval("R", <<-R)
356
- function(ruby_obj) {
357
- # print(ruby_obj);
358
- attr(ruby_obj, "class") = "ruby_obj"
359
- print(ruby_obj@to_s());
360
- print(class(ruby_obj));
361
- # df = data.frame(ruby_obj);
362
- }
363
-
364
- print.ruby_obj = function(x, index, ...) {
365
- print(index);
366
- x[[1]]@fetch(index);
367
- }
368
-
369
- R
370
-
371
- h = ArrayEmul.new
372
- h << 1 << 2 << 3
373
- puts h
374
-
375
- r_obj = make_obj.call(h)
376
- p Polyglot.eval("R", "print").call(r_obj, 0)[0]
377
-
378
- =end
379
-
380
-
381
- =begin
382
- sym = +:sym
383
- # construct a formula without the lhs: ~sym1 + sym2
384
- puts +:sym1 + +:sym2
385
- f1 = +:sym1 + :sym2
386
- puts +:sym1 + +:sym2 - +:sym3
387
- puts +:sym1 * +:sym2
388
- =end
389
-
390
- #puts :sym3 ** 2
391
- # p (:sym1 + :sym2 * :sym3 ** 2).expression
392
- #puts E.log(:y + 1).to_s
393
-
394
- # puts :sym1 ** 2
395
- # puts +:sym1 + (:sym1 ** 2).i
396
-
397
- # puts +:y =~ +:sym1 + +:sym2 * +:sym3 + (:sym1 ** 2).i
398
- # puts +:y =~ :all
399
-
400
-
401
- # p sym
402
- # puts sym
403
- # sym.typeof
404
- =begin
405
- f1 = +:sym1 + +:sym2
406
- puts f1
407
- puts f1.deparse
408
-
409
- puts R.c(1, 2, 3).deparse
410
- puts R.list(a: 1, b: 2, c: 3).deparse
411
- =end
412
-
413
- =begin
414
- # Set seed
415
- R.set__seed(123)
416
-
417
- # Data
418
- x = R.rnorm(5)
419
- x2 = R.rnorm(5)
420
- y = R.rnorm(5)
421
-
422
- # Model frame
423
- puts R.model__frame(+:y =~ +:x * +:x2, data: R.data__frame(x: x, y: y, x2: x2))
424
-
425
- puts R.model__frame(+:y =~ +:x + +:x2 + (+:x ^ +:x2),
426
- data: R.data__frame(x: x, y: y, x2: x2))
427
-
428
- puts R.model__frame(+:y =~ +:x + (:x ** 2).i,
429
- data: R.data__frame(x: R.rnorm(5), y: R.rnorm(5)))
430
- =end
431
37
  =begin
432
- class User
433
-
434
- attr_reader :id
435
- attr_reader :name
436
- attr_reader :favoriteLanguage
437
-
438
- def initialize(id, name, favorite_language)
439
- @id = id
440
- @name = name
441
- @favorite_language = favorite_language
442
- end
443
-
444
- end
445
-
446
- class NameColumn
447
-
448
- attr_reader :users
449
-
450
- def initialize(users)
451
- @users = users
452
- end
453
-
454
- def get(index)
455
- users[index].name
456
- end
457
-
458
- def set(index, val)
459
- end
460
-
461
- def getSize
462
- users.size
463
- end
464
-
465
- def method_missing(symbol, *args)
466
- puts "NameColumn method_missing"
467
- puts symbol
468
- puts args
469
- end
470
-
471
- end
472
-
473
- class IdColumn
474
-
475
- attr_reader :users
476
-
477
- def initialize(users)
478
- @users = users
479
- end
480
-
481
- def get(index)
482
- users[index].id
483
- end
484
-
485
- def set(index, val)
486
- end
487
-
488
- def getSize
489
- users.size
490
- end
491
-
492
- def method_missing(symbol, *args)
493
- puts "IdColumn method_missing"
494
- puts symbol
495
- puts args
496
- end
497
-
498
- end
499
-
500
- class UsersTable
501
- attr_reader :id
502
- attr_reader :name
503
- attr_reader :language
504
-
505
- def initialize(users)
506
- @users = users
507
- @id = IdColumn.new(users)
508
- @name = NameColumn.new(users)
509
- end
510
-
511
- def getSize
512
- puts users.size
513
- users.size
514
- end
515
-
516
- def method_missing(symbol, *args)
517
- puts "UsersTable method_missing"
518
- puts symbol
519
- puts args
520
- end
521
-
522
- end
523
-
524
- rf = R::Support.eval(<<-R)
525
- function(table) {
526
- print(table);
527
- table <- as.data.frame(table);
528
- cat('The whole data frame printed in R:\n');
529
- print(table);
530
- cat('---------\n\n');
531
-
532
- cat('Filter out users with ID>2:\n');
533
- print(table[table$id > 2,]);
534
- cat('---------\n\n');
535
-
536
- cat('How many users like Java: ');
537
- cat(nrow(table[table$language == 'Java',]), '\n');
538
- }
539
- R
540
-
541
- users = []
542
- users << User.new(1, "Florian", "Python")
543
- users << User.new(2, "Lukas", "R")
544
- users << User.new(3, "Mila", "Java")
545
- users << User.new(4, "Paley", "Coq")
546
- users << User.new(5, "Stepan", "C#")
547
- users << User.new(6, "Tomas", "Java")
548
- users << User.new(7, "Zbynek", "Scala")
38
+ formula = :cyl.til :exp
39
+ puts formula
40
+ puts formula.to_s
549
41
 
550
- puts users[0].name
551
- ut = UsersTable.new(users)
552
- rut = R.list(ut)
553
- rf.call(rut)
554
42
  =end