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
@@ -29,23 +29,25 @@ module R
29
29
 
30
30
  class RSymbol < R::Object
31
31
  include BinaryOperators
32
- include FormulaBinOp
33
-
32
+ include ExecBinOp
33
+ # include FormulaBinOp
34
+ =begin
34
35
  #--------------------------------------------------------------------------------------
35
36
  #
36
37
  #--------------------------------------------------------------------------------------
37
38
 
38
39
  def =~(other_object)
39
40
  # puts "doing #{self} =~ #{other_object}"
40
- exec_oper("`~`", other_object, true)
41
+ exec_bin_oper("`~`", other_object, true)
41
42
  end
43
+ =end
42
44
 
43
45
  #--------------------------------------------------------------------------------------
44
46
  #
45
47
  #--------------------------------------------------------------------------------------
46
48
 
47
49
  def ^(other_object)
48
- exec_oper("`:`", other_object)
50
+ exec_bin_oper("`:`", other_object)
49
51
  end
50
52
 
51
53
  #--------------------------------------------------------------------------------------
@@ -54,5 +56,4 @@ module R
54
56
 
55
57
  end
56
58
 
57
-
58
59
  end
@@ -25,6 +25,54 @@
25
25
  #
26
26
  #==========================================================================================
27
27
 
28
+ module R
29
+
30
+ module ExpBinOp
31
+
32
+ #--------------------------------------------------------------------------------------
33
+ #
34
+ #--------------------------------------------------------------------------------------
35
+
36
+ def exec_bin_oper(operator, other_object)
37
+ R::Support.exec_function(R::Support.create_bin_expr(operator), self,
38
+ other_object)
39
+ end
40
+
41
+ #--------------------------------------------------------------------------------------
42
+ #
43
+ #--------------------------------------------------------------------------------------
44
+
45
+ def coerce(numeric)
46
+ [R::RubyExpression.build_bin_exp(numeric), self]
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+
53
+ #==========================================================================================
54
+ #
55
+ #==========================================================================================
56
+
57
+ module E
58
+
59
+ #----------------------------------------------------------------------------------------
60
+ # @param symbol [Symbol]
61
+ # @param args [Array] arguments to the missing method
62
+ #----------------------------------------------------------------------------------------
63
+
64
+ def self.method_missing(symbol, *args)
65
+ name = R::Support.convert_symbol2r(symbol)
66
+ rargs = R.exprs(name.to_sym, *args)
67
+ R.as__call(rargs)
68
+ end
69
+
70
+ end
71
+
72
+ #==========================================================================================
73
+ #
74
+ #==========================================================================================
75
+
28
76
  class Range
29
77
 
30
78
  #----------------------------------------------------------------------------------------
@@ -32,7 +80,11 @@ class Range
32
80
  #----------------------------------------------------------------------------------------
33
81
 
34
82
  def -@
35
- NegRange.new(self.begin, self.end)
83
+ final_value = (exclude_end?)?
84
+ (first > last)?
85
+ (last + 1) : (last - 1)
86
+ : last
87
+ NegRange.new(self.begin, final_value)
36
88
  end
37
89
 
38
90
  end
@@ -53,15 +105,14 @@ end
53
105
 
54
106
  class Symbol
55
107
  include R::BinaryOperators
56
- include R::CallBinOp
108
+ include R::ExpBinOp
57
109
 
58
110
  #--------------------------------------------------------------------------------------
59
111
  # Unary '+' converts a Ruby Symbol into an R Symbol
60
112
  #--------------------------------------------------------------------------------------
61
113
 
62
114
  def +@
63
- var = (self == :all)? '.' : to_s
64
- R::Object.build(R::Support.eval("as.name").call(var))
115
+ R.expr(self)
65
116
  end
66
117
 
67
118
  #--------------------------------------------------------------------------------------
@@ -71,5 +122,80 @@ class Symbol
71
122
  def ~@
72
123
  R::Object.build(R::Support.eval(to_s))
73
124
  end
125
+
126
+ #--------------------------------------------------------------------------------------
127
+ #
128
+ #--------------------------------------------------------------------------------------
129
+
130
+ def succ
131
+ self.to_s.succ.to_sym
132
+ end
133
+
134
+ #----------------------------------------------------------------------------------------
135
+ # We use the following notation to access binary R functions such as %in%:
136
+ # R.vec_ "in", list.
137
+ # @param args [Array] The first element of the array is an R infix function, the other
138
+ # arguments are the list of arguments for the function.
139
+ #----------------------------------------------------------------------------------------
140
+
141
+ def _(*args)
142
+ name = "%#{args.shift.to_s}%"
143
+ args.unshift(self)
144
+ rargs = R.exprs(name, *args)
145
+ R.as__call(rargs)
146
+ end
147
+
148
+ #--------------------------------------------------------------------------------------
149
+ #
150
+ #--------------------------------------------------------------------------------------
151
+
152
+ def assign(expression)
153
+ exec_bin_oper("`<-`", expression)
154
+ end
155
+
156
+ #--------------------------------------------------------------------------------------
157
+ #
158
+ #--------------------------------------------------------------------------------------
159
+
160
+ def [](index)
161
+ exec_bin_oper("`[[`", index)
162
+ end
163
+
164
+ #--------------------------------------------------------------------------------------
165
+ #
166
+ #--------------------------------------------------------------------------------------
167
+
168
+ def method_missing(symbol, *args, &block)
169
+
170
+ if (symbol =~ /(.*)=$/)
171
+ # method_missing_assign($1, args[0])
172
+ elsif (args.length == 0 && ((R.c(symbol.to_s)._ :in, R.names(self)) << 0))
173
+ return self[symbol.to_s]
174
+ end
175
+
176
+ R.send(symbol.to_s, self, *args)
177
+
178
+ end
179
+
180
+ #--------------------------------------------------------------------------------------
181
+ #
182
+ #--------------------------------------------------------------------------------------
183
+
184
+ def inter(var2)
185
+ R::Support.exec_function(R::Support.range, self, var2)
186
+ end
187
+
188
+ #--------------------------------------------------------------------------------------
189
+ # If method_missing is implemented, then we also need to implement method 'to_ary'.
190
+ # This is because starting from ruby 1.9 the code for Array#flatten has changed,
191
+ # calling 'to_ary' blindly (even if the method is not implemented). This causes
192
+ # the method to be caught by 'method_missing' and here sending a 'to_ary' no found
193
+ # error. If we create to_ary, then no error is issued.
194
+ # @TODO: make sure that implementing 'to_ary' as bellow will not create problems
195
+ #--------------------------------------------------------------------------------------
196
+
197
+ def to_ary
198
+ [self.to_s]
199
+ end
74
200
 
75
201
  end
@@ -23,6 +23,35 @@
23
23
 
24
24
  module R
25
25
 
26
+ module ExecUniOp
27
+
28
+ #--------------------------------------------------------------------------------------
29
+ #
30
+ #--------------------------------------------------------------------------------------
31
+
32
+ def exec_uni_oper(operator)
33
+ R::Support.exec_function_name(operator, @r_interop)
34
+ end
35
+
36
+ end
37
+
38
+
39
+ module ExpUniOp
40
+
41
+ #--------------------------------------------------------------------------------------
42
+ #
43
+ #--------------------------------------------------------------------------------------
44
+
45
+ def exec_uni_oper(operator)
46
+ # op1 = prep(self)
47
+ # op2 = prep(other_object)
48
+ # R::Expression.build(operator, self)
49
+ optr = operator.delete("`")
50
+ R::Expression.build("#{operator}#{self}")
51
+ end
52
+
53
+ end
54
+
26
55
  #--------------------------------------------------------------------------------------
27
56
  #
28
57
  #--------------------------------------------------------------------------------------
@@ -34,7 +63,8 @@ module R
34
63
  #--------------------------------------------------------------------------------------
35
64
 
36
65
  def !
37
- R::Support.exec_function_name("`!`", @r_interop)
66
+ # R::Support.exec_function_name("`!`", @r_interop)
67
+ exec_uni_oper("`!`")
38
68
  end
39
69
 
40
70
  #--------------------------------------------------------------------------------------
@@ -42,7 +72,8 @@ module R
42
72
  #--------------------------------------------------------------------------------------
43
73
 
44
74
  def -@
45
- R::Support.exec_function_name("`-`", @r_interop)
75
+ # R::Support.exec_function_name("`-`", @r_interop)
76
+ exec_uni_oper("`-`")
46
77
  end
47
78
 
48
79
  #--------------------------------------------------------------------------------------
@@ -50,7 +81,8 @@ module R
50
81
  #--------------------------------------------------------------------------------------
51
82
 
52
83
  def +@
53
- R::Support.exec_function_name("`+`", @r_interop)
84
+ # R::Support.exec_function_name("`+`", @r_interop)
85
+ exec_uni_oper("`+`")
54
86
  end
55
87
 
56
88
  end
@@ -28,6 +28,7 @@ module R
28
28
  include BinaryOperators
29
29
  include ExecBinOp
30
30
  include UnaryOperators
31
+ include ExecUniOp
31
32
  include Enumerable
32
33
 
33
34
  #--------------------------------------------------------------------------------------
@@ -21,8 +21,6 @@
21
21
  # OR MODIFICATIONS.
22
22
  ##########################################################################################
23
23
 
24
- require 'pry'
25
-
26
24
  $LOAD_PATH << File.dirname(File.expand_path('..', __FILE__)) + "/r_requires"
27
25
 
28
26
  require_relative 'R_interface/r'
@@ -88,11 +88,29 @@ class KnitrEngine
88
88
 
89
89
  def self.file_ext
90
90
  # guess plot file type if it is NULL
91
- if (@keep != 'none' && @options.fig__ext.is__null)
92
- @options.fig__ext = R.dev2ext(@options.dev)
91
+ if (((@keep != 'none') << 0) && (@options.fig__ext.is__null << 0))
92
+ # TODO: allow @options.fig__ext when it is null... confusing otherwise!
93
+ @options["fig.ext"] = R.knitr_dev2ext(@options.dev)
94
+ puts @options.fig__ext
93
95
  end
94
96
  end
97
+
98
+ #--------------------------------------------------------------------------------------
99
+ #
100
+ #--------------------------------------------------------------------------------------
101
+
102
+ def self.capture_plot
103
+
104
+ plot = R.evaluate_plot_snapshot
105
+
106
+ if (!(R.is__null(plot) << 0))
107
+ R.png(@filename)
108
+ R.print(plot)
109
+ R.dev__off
110
+ end
95
111
 
112
+ end
113
+
96
114
  #--------------------------------------------------------------------------------------
97
115
  # Process the chunk options
98
116
  #--------------------------------------------------------------------------------------
@@ -102,9 +120,15 @@ class KnitrEngine
102
120
 
103
121
  # verifies if figures should be kept
104
122
  fig_keep
105
-
123
+
106
124
  # if figures are to be kept, take or guess the file extension
107
- # file_ext
125
+ file_ext
126
+
127
+ # puts @keep
128
+
129
+ # make final filename
130
+ @filename = R.paste0(@options.fig__path, @options.label, ".", @options.fig__ext)
131
+
108
132
  end
109
133
 
110
134
  #--------------------------------------------------------------------------------------
@@ -118,3 +142,33 @@ class KnitrEngine
118
142
  end
119
143
 
120
144
  end
145
+
146
+
147
+
148
+ module R
149
+
150
+ class Object
151
+
152
+ #--------------------------------------------------------------------------------------
153
+ # Redefine to_s in order to capture plots when in knitr
154
+ #--------------------------------------------------------------------------------------
155
+
156
+ def to_s
157
+
158
+ cap = nil
159
+ # dev = R::Device.new('png', width: 5, height: 7, dpi: 300, record: true) {
160
+ cap = R::Support.capture.call(r_interop)
161
+ # cap = R::Support.capture_output.call(r_interop)
162
+ # }
163
+ str = String.new
164
+ (0...(cap.size - 1)).each do |i|
165
+ str << cap[i] << "\n"
166
+ end
167
+ str << cap[cap.size - 1] if cap.size >= 1
168
+ KnitrEngine.capture_plot
169
+ str
170
+ end
171
+
172
+ end
173
+
174
+ end
@@ -26,9 +26,10 @@ eng_ruby = Proc.new do |options|
26
26
 
27
27
  begin
28
28
  # read the chunk code
29
- code = R.paste(options.code, collapse: "\n") << 0
29
+ # code = R.paste(options.code, collapse: "\n") << 0
30
30
 
31
- # process the chunk options
31
+ # process the chunk options. Better to let interested methods process their
32
+ # options
32
33
  KnitrEngine.process_options(options)
33
34
 
34
35
  tmp_fig = R.tempfile()
@@ -52,15 +53,13 @@ eng_ruby = Proc.new do |options|
52
53
  end
53
54
 
54
55
  # executes the code
55
- res = GalaazUtil.exec_ruby(code)
56
+ res = GalaazUtil.exec_ruby(options)
56
57
 
57
58
  # formats and outputs the code and results
58
- R.engine_output(options, options.code, res)
59
+ R.engine_output(options, out: res)
59
60
 
60
61
  ensure
61
62
  R.dev__off(dv)
62
- # tmp_fig.unlink
63
- # if (keep.pars) opts_knit$set(global.pars = par(no.readonly = TRUE))
64
63
  end
65
64
 
66
65
  end
@@ -23,22 +23,68 @@
23
23
 
24
24
  require 'stringio'
25
25
 
26
+ #----------------------------------------------------------------------------------------
27
+ # Class RubyChunk is used only as a context for all ruby chunks in the rmarkdown file.
28
+ # This allows for chunks to access instance_variables (@)
29
+ #----------------------------------------------------------------------------------------
30
+
26
31
  class RubyChunk
27
32
 
28
33
  end
29
34
 
35
+ #----------------------------------------------------------------------------------------
36
+ #
37
+ #----------------------------------------------------------------------------------------
38
+
30
39
  module GalaazUtil
40
+
41
+ #----------------------------------------------------------------------------------------
42
+ # Executes the ruby code with the given options.
43
+ # @param options [R::List] An R list of options
44
+ # @return [R::List] an R list with everything that needs to be outputed.
45
+ # The options are:
46
+ # * options.code: the ruby code
47
+ # * options[["eval"]]: evaluate if true
48
+ # * options.echo: if true, show the source code of the chunk in the output
49
+ # * options.message: if true, show error message if any exception in the ruby code
50
+ # * options.warning: if true, show stack trace from the ruby code
51
+ # * options.include: if true, include the code output
52
+ # Note that we need to access the 'eval' element of the list by indexing as
53
+ # options[["eval"]], this is because eval is a Ruby function and doing options.eval
54
+ # will call the eval method on options, which is not what we want
55
+ #----------------------------------------------------------------------------------------
31
56
 
32
- def self.exec_ruby(code)
57
+ def self.exec_ruby(options)
58
+
59
+ # read the chunk code
60
+ code = R.paste(options.code, collapse: "\n") << 0
61
+
62
+ out_list = R.list
63
+ out_list["source"] = code if (options.echo << 0)
33
64
 
34
- # Set up standard output as a StringIO object.
35
- $stdout = StringIO.new
36
- RubyChunk.instance_eval(code)
37
- out = $stdout.string
38
- # return $stdout to standard output
39
- $stdout = STDOUT
40
- # return everything that was outputed
41
- out
65
+ begin
66
+ # Set up standard output as a StringIO object.
67
+ $stdout = StringIO.new
68
+ RubyChunk.instance_eval(code) if (options[["eval"]] << 0)
69
+ out = $stdout.string
70
+ # KnitrEngine.capture_plot
71
+
72
+ # return $stdout to standard output
73
+ $stdout = STDOUT
74
+ # return everything that was outputed
75
+ out_list["text"] = out
76
+ rescue StandardError => e
77
+ out_list["message"] = e.message if (options.message << 0)
78
+ out_list["warning"] = e.backtrace.inspect if (options.warning << 0)
79
+ end
80
+
81
+ # TODO: check the name of procedures since communication is
82
+ # bidirectional. The name parse_arg was done thinking only on
83
+ # the Ruby -> R direction.
84
+ # exec_ruby returns its output to an R script, so we need to pass
85
+ # the output (out_list) through parse_arg to make it available
86
+ # to R.
87
+ (options.include << 0)? R::Support.parse_arg(out_list) : nil
42
88
 
43
89
  end
44
90