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,6 +21,10 @@
21
21
  # OR MODIFICATIONS.
22
22
  ##########################################################################################
23
23
 
24
+ # Load required R libraries
25
+ dir = File.dirname(File.expand_path('.', __FILE__))
26
+ Polyglot.eval_file("#{dir}/r_libs.R")
27
+
24
28
  require_relative 'robject'
25
29
  require_relative 'rsupport'
26
30
 
@@ -81,15 +85,20 @@ module R
81
85
 
82
86
  end
83
87
 
84
- # define methods for accessing indexed object: Vector, Lists
85
- require_relative 'rindexed_object'
86
- # define methods for accessing multi dimensional indexed objects: Matrix, DataFrames
87
- require_relative 'rmd_indexed_object'
88
88
  # Binary operators: '+', '-', etc
89
89
  require_relative 'rbinary_operators'
90
90
  # Unary operators: '!', '@-', etc
91
91
  require_relative 'runary_operators'
92
- # Definition of R Vector
92
+ # Ruby class extensions. Extends Symbol to allow the creation of
93
+ # expressions using Symbol: (:x > 10)
94
+ require_relative 'ruby_extensions'
95
+
96
+ # define methods for accessing indexed object: Vector, Lists
97
+ require_relative 'rindexed_object'
98
+ # define methods for accessing multi dimensional indexed objects: Matrix, DataFrames
99
+ require_relative 'rmd_indexed_object'
100
+
101
+ # Definition of R Vector. Vectors include Unary and Binary operators
93
102
  require_relative 'rvector'
94
103
  # Definition of R Lists
95
104
  require_relative 'rlist'
@@ -110,11 +119,6 @@ require_relative 'rsymbol'
110
119
  # Access to package symbols
111
120
  require_relative 'rpkg'
112
121
 
113
- # Defines the E module for creating R expressions
114
- require_relative 'expression'
115
- # Ruby class extensions. Extends Symbol to allow the creation of
116
- # expressions using Symbol: (:x > 10)
117
- require_relative 'ruby_extensions'
118
122
  # Class to allow R calling back into Ruby
119
123
  require_relative 'ruby_callback'
120
124
 
@@ -0,0 +1,9 @@
1
+ list.of.packages <- c('rlang', 'purrr', 'formula.tools', 'lobstr')
2
+
3
+ new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
4
+ if(length(new.packages)) install.packages(new.packages)
5
+
6
+ library('formula.tools')
7
+ library('lobstr')
8
+ library('rlang')
9
+ library('purrr')
@@ -22,7 +22,7 @@
22
22
  ##########################################################################################
23
23
 
24
24
  module R
25
-
25
+
26
26
  module Support
27
27
 
28
28
  #--------------------------------------------------------------------------------------
@@ -41,6 +41,21 @@ module R
41
41
  R
42
42
  end
43
43
 
44
+ def self.capture2
45
+ Polyglot.eval("R", <<-R)
46
+ function(obj, ...) {
47
+ tryCatch({
48
+ sink(tt <- textConnection("results","w"), split=FALSE, type = c("output", "message"));
49
+ print(obj, ...);
50
+ sink();
51
+ results
52
+ }, finally = {
53
+ close(tt);
54
+ })
55
+ }
56
+ R
57
+ end
58
+
44
59
  def self.start_capture
45
60
  Polyglot.eval("R", <<-R)
46
61
  function(cap_variable) {
@@ -59,6 +74,18 @@ module R
59
74
  R
60
75
  end
61
76
 
77
+ #--------------------------------------------------------------------------------------
78
+ #
79
+ #--------------------------------------------------------------------------------------
80
+
81
+ def self.dbk_index
82
+ Polyglot.eval("R", <<-R)
83
+ function(obj, index) {
84
+ obj[[index]]
85
+ }
86
+ R
87
+ end
88
+
62
89
  #--------------------------------------------------------------------------------------
63
90
  # multi-dimensional indexing
64
91
  #--------------------------------------------------------------------------------------
@@ -89,15 +116,59 @@ module R
89
116
  end
90
117
 
91
118
  #--------------------------------------------------------------------------------------
92
- # @bug Needed to create method row__names because dispatch is not working properly
119
+ #
120
+ #--------------------------------------------------------------------------------------
121
+
122
+ def self.enquo
123
+ Polyglot.eval("R", <<-R)
124
+ function(x, ...) {
125
+ enquo(x)
126
+ }
127
+ R
128
+ end
129
+
130
+ #--------------------------------------------------------------------------------------
131
+ #
93
132
  #--------------------------------------------------------------------------------------
94
133
 
95
- def self.set_row_names
96
- Polyglot.eval("R", "function(object, x) row.names(object) <- x")
134
+ def self.range
135
+ Polyglot.eval("R", <<-R)
136
+ function(x, y, neg = FALSE) {
137
+ e1 = enexpr(x)
138
+ e2 = enexpr(y)
139
+ if (neg) {
140
+ expr(-(!!e1:!!e2))
141
+ } else {
142
+ expr(!!e1:!!e2)
143
+ }
144
+ }
145
+ R
97
146
  end
98
147
 
99
- def self.get_row_names
100
- Polyglot.eval("R", "function(x) row.names(x)")
148
+ #--------------------------------------------------------------------------------------
149
+ #
150
+ #--------------------------------------------------------------------------------------
151
+
152
+ def self.create_bin_expr(operator)
153
+ is_formula = (operator == "`~`")? 'TRUE' : 'FALSE'
154
+ Polyglot.eval("R", <<-R)
155
+ function(op1, op2) {
156
+ o1 = enexpr(op1)
157
+ o2 = enexpr(op2)
158
+ if (typeof(o1) == 'symbol' && o1 == expr(`__`)) {
159
+ o1 = expr(.)
160
+ }
161
+ if (typeof(o2) == 'symbol' && o2 == expr(`__`)) {
162
+ o2 = expr(.)
163
+ }
164
+ exp = expr(#{operator}(!!o1, !!o2))
165
+ if (#{is_formula}) {
166
+ as.formula(exp)
167
+ } else {
168
+ exp
169
+ }
170
+ }
171
+ R
101
172
  end
102
173
 
103
174
  end
@@ -21,24 +21,23 @@
21
21
  # OR MODIFICATIONS.
22
22
  ##########################################################################################
23
23
 
24
- module E
25
-
26
- #----------------------------------------------------------------------------------------
27
- # @param symbol [Symbol]
28
- # @param args [Array] arguments to the missing method
29
- #----------------------------------------------------------------------------------------
24
+ module R
30
25
 
31
- def self.method_missing(symbol, *args)
32
- name = R::Support.convert_symbol2r(symbol)
33
- R::Language.build(name, *args)
34
- end
35
-
36
26
  #--------------------------------------------------------------------------------------
37
- #
27
+ # Module S, is a support module with R functions that we do not want to polute the
28
+ # R namespace
38
29
  #--------------------------------------------------------------------------------------
39
30
 
40
- def self.[](executable)
41
- R::RubyCallback.build(executable)
31
+ module S
32
+
33
+ #--------------------------------------------------------------------------------------
34
+ #
35
+ #--------------------------------------------------------------------------------------
36
+
37
+ def self.columns(col_ini, col_end, remove: false)
38
+ R::Support.exec_function(R::Support.range, col_ini, col_end, remove)
39
+ end
40
+
42
41
  end
43
42
 
44
43
  end
@@ -28,76 +28,39 @@ module R
28
28
  #--------------------------------------------------------------------------------------
29
29
 
30
30
  module ExecBinOp
31
-
32
- #--------------------------------------------------------------------------------------
33
- #
31
+
34
32
  #--------------------------------------------------------------------------------------
33
+ #
34
+ #--------------------------------------------------------------------------------------
35
35
 
36
- def exec_oper(operator, other_object)
36
+ def exec_bin_oper(operator, other_object)
37
37
  R::Support.exec_function_name(operator, @r_interop, other_object)
38
38
  end
39
-
40
- end
41
-
42
- #--------------------------------------------------------------------------------------
43
- # Module for binary operators when creating an expression (call)
44
- #--------------------------------------------------------------------------------------
45
-
46
- module CallBinOp
47
-
39
+
48
40
  #--------------------------------------------------------------------------------------
49
- #
41
+ #
50
42
  #--------------------------------------------------------------------------------------
51
43
 
52
- def exec_oper(operator, other_object)
53
- R::Language.build(operator, self, other_object)
54
- end
55
-
44
+ def coerce(numeric)
45
+ [R.c(numeric), self]
46
+ end
47
+
56
48
  end
57
-
49
+
58
50
  #--------------------------------------------------------------------------------------
59
- # Module for binary operators when creating formulas
51
+ # Module for binary operators when creating an expression (call)
60
52
  #--------------------------------------------------------------------------------------
61
-
62
- module FormulaBinOp
53
+
54
+ module CallBinOp
63
55
 
64
- #--------------------------------------------------------------------------------------
65
- #
66
- #--------------------------------------------------------------------------------------
67
-
68
- def prep_object(object)
69
-
70
- case object
71
- when :all
72
- '.'
73
- when R::RSymbol, Symbol
74
- object.to_s
75
- when R::Language
76
- R.deparse(object).substring(2)
77
- when String
78
- object
79
- end
80
-
81
- end
82
-
83
56
  #--------------------------------------------------------------------------------------
84
57
  #
85
58
  #--------------------------------------------------------------------------------------
86
59
 
87
- def exec_oper(operator, other_object, response = false)
88
-
89
- o1 = prep_object(self)
90
- o2 = prep_object(other_object)
91
-
92
- response ? R.reformulate(o2, response: o1) :
93
- R.reformulate(R.paste0(o1, operator.delete("`"), o2))
94
-
60
+ def exec_bin_oper(operator, other_object)
61
+ R::Language.build(operator, self, other_object)
95
62
  end
96
63
 
97
- #--------------------------------------------------------------------------------------
98
- #
99
- #--------------------------------------------------------------------------------------
100
-
101
64
  end
102
65
 
103
66
  #--------------------------------------------------------------------------------------
@@ -111,7 +74,7 @@ module R
111
74
  #--------------------------------------------------------------------------------------
112
75
 
113
76
  def +(other_object)
114
- exec_oper("`+`", other_object)
77
+ exec_bin_oper("`+`", other_object)
115
78
  end
116
79
 
117
80
  #--------------------------------------------------------------------------------------
@@ -119,7 +82,7 @@ module R
119
82
  #--------------------------------------------------------------------------------------
120
83
 
121
84
  def -(other_object)
122
- exec_oper("`-`", other_object)
85
+ exec_bin_oper("`-`", other_object)
123
86
  end
124
87
 
125
88
  #--------------------------------------------------------------------------------------
@@ -127,7 +90,7 @@ module R
127
90
  #--------------------------------------------------------------------------------------
128
91
 
129
92
  def *(other_object)
130
- exec_oper("`*`", other_object)
93
+ exec_bin_oper("`*`", other_object)
131
94
  end
132
95
 
133
96
  #--------------------------------------------------------------------------------------
@@ -135,7 +98,7 @@ module R
135
98
  #--------------------------------------------------------------------------------------
136
99
 
137
100
  def /(other_object)
138
- exec_oper("`/`", other_object)
101
+ exec_bin_oper("`/`", other_object)
139
102
  end
140
103
 
141
104
  #--------------------------------------------------------------------------------------
@@ -143,7 +106,7 @@ module R
143
106
  #--------------------------------------------------------------------------------------
144
107
 
145
108
  def **(other_object)
146
- exec_oper("`^`", other_object)
109
+ exec_bin_oper("`^`", other_object)
147
110
  end
148
111
 
149
112
  #--------------------------------------------------------------------------------------
@@ -151,15 +114,25 @@ module R
151
114
  #--------------------------------------------------------------------------------------
152
115
 
153
116
  def %(other_object)
154
- exec_oper("`%%`", other_object)
117
+ exec_bin_oper("`%%`", other_object)
155
118
  end
119
+
120
+ alias_method :mod, :%
121
+
122
+ #--------------------------------------------------------------------------------------
123
+ #
124
+ #--------------------------------------------------------------------------------------
156
125
 
126
+ def int_div(other_object)
127
+ exec_bin_oper("`%/%`", other_object)
128
+ end
129
+
157
130
  #--------------------------------------------------------------------------------------
158
131
  #
159
132
  #--------------------------------------------------------------------------------------
160
133
 
161
- def ==(other_object)
162
- exec_oper("`==`", other_object)
134
+ def eq(other_object)
135
+ exec_bin_oper("`==`", other_object)
163
136
  end
164
137
 
165
138
  #--------------------------------------------------------------------------------------
@@ -167,63 +140,77 @@ module R
167
140
  #--------------------------------------------------------------------------------------
168
141
 
169
142
  def <(other_object)
170
- exec_oper("`<`", other_object)
143
+ exec_bin_oper("`<`", other_object)
171
144
  end
172
-
145
+
146
+ alias_method :lt, :<
147
+
173
148
  #--------------------------------------------------------------------------------------
174
149
  #
175
150
  #--------------------------------------------------------------------------------------
176
151
 
177
152
  def <=(other_object)
178
- exec_oper("`<=`", other_object)
153
+ exec_bin_oper("`<=`", other_object)
179
154
  end
180
155
 
156
+ alias_method :le, :<=
157
+
181
158
  #--------------------------------------------------------------------------------------
182
159
  #
183
160
  #--------------------------------------------------------------------------------------
184
161
 
185
162
  def >(other_object)
186
- exec_oper("`>`", other_object)
163
+ exec_bin_oper("`>`", other_object)
187
164
  end
188
165
 
166
+ alias_method :gt, :>
167
+
189
168
  #--------------------------------------------------------------------------------------
190
169
  #
191
170
  #--------------------------------------------------------------------------------------
192
171
 
193
172
  def >=(other_object)
194
- exec_oper("`>=`", other_object)
173
+ exec_bin_oper("`>=`", other_object)
195
174
  end
196
175
 
176
+ alias_method :ge, :>=
177
+
197
178
  #--------------------------------------------------------------------------------------
198
179
  #
199
180
  #--------------------------------------------------------------------------------------
200
181
 
201
182
  def !=(other_object)
202
- exec_oper("`!=`", other_object)
183
+ exec_bin_oper("`!=`", other_object)
203
184
  end
185
+
186
+ alias_method :ne, :!=
204
187
 
205
188
  #--------------------------------------------------------------------------------------
206
189
  #
207
190
  #--------------------------------------------------------------------------------------
208
191
 
209
192
  def &(other_object)
210
- R::Support.exec_function_name("`&`", @r_interop, other_object.r_interop)
193
+ exec_bin_oper("`&`", other_object)
211
194
  end
195
+
196
+ alias_method :and, :&
212
197
 
213
198
  #--------------------------------------------------------------------------------------
214
199
  #
215
200
  #--------------------------------------------------------------------------------------
216
201
 
217
202
  def |(other_object)
218
- R::Support.exec_function_name("`|`", @r_interop, other_object.r_interop)
203
+ exec_bin_oper("`|`", other_object)
219
204
  end
220
205
 
206
+ alias_method :or, :|
207
+
221
208
  #--------------------------------------------------------------------------------------
222
209
  #
223
210
  #--------------------------------------------------------------------------------------
224
211
 
225
- def coerce(numeric)
226
- [R.c(numeric), self]
212
+ def til(other_object)
213
+ exec_bin_oper("`~`", other_object)
227
214
  end
228
215
 
229
216
  end