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
@@ -25,7 +25,8 @@ module R
25
25
 
26
26
  class DataFrame < List
27
27
  include MDIndexedObject
28
-
28
+
29
+
29
30
  #--------------------------------------------------------------------------------------
30
31
  # Calls the R.qplot adding the data: parameter
31
32
  #--------------------------------------------------------------------------------------
@@ -29,3 +29,7 @@ knitr_save_plot = function(plot, name, dev, width, height, ext, dpi, options) {
29
29
  knitr_opts_chunk = function() {
30
30
  knitr:::opts_chunk
31
31
  }
32
+
33
+ evaluate_plot_snapshot = function() {
34
+ evaluate:::plot_snapshot()
35
+ }
@@ -168,7 +168,7 @@ module R
168
168
  #
169
169
  #--------------------------------------------------------------------------------------
170
170
 
171
- class Panel < Device
171
+ class Panel < Device
172
172
  include Singleton
173
173
 
174
174
  #--------------------------------------------------------------------------------------
@@ -28,7 +28,40 @@ module R
28
28
  #--------------------------------------------------------------------------------------
29
29
 
30
30
  class Environment < Object
31
+ include IndexedObject
32
+
33
+ #--------------------------------------------------------------------------------------
34
+ #
35
+ #--------------------------------------------------------------------------------------
31
36
 
32
- end
37
+ def set(key, value)
38
+ R.assign(key.to_s, value, pos: self)
39
+ end
40
+
41
+ #--------------------------------------------------------------------------------------
42
+ #
43
+ #--------------------------------------------------------------------------------------
44
+
45
+ def get(key)
46
+ R.get(key.to_s, pos: self)
47
+ end
48
+
49
+ #--------------------------------------------------------------------------------------
50
+ #
51
+ #--------------------------------------------------------------------------------------
33
52
 
53
+ def method_missing(symbol, *args, &block)
54
+
55
+ name = R::Support.convert_symbol2r(symbol)
56
+
57
+ case
58
+ when name =~ /(.*)=$/
59
+ set($1, args[0])
60
+ else
61
+ super(symbol, *args, &block)
62
+ end
63
+ end
64
+
65
+ end
66
+
34
67
  end
@@ -22,13 +22,119 @@
22
22
  ##########################################################################################
23
23
 
24
24
  module R
25
-
25
+ =begin
26
26
  #--------------------------------------------------------------------------------------
27
- #
27
+ #
28
28
  #--------------------------------------------------------------------------------------
29
+
30
+ class Expression
31
+ include BinaryOperators
32
+ include ExpBinOp
33
+ include UnaryOperators
34
+ include ExpUniOp
35
+
36
+ attr_reader :infix
37
+ attr_reader :prefix
38
+ attr_reader :formula
39
+ attr_accessor :environment
40
+
41
+ #--------------------------------------------------------------------------------------
42
+ #
43
+ #--------------------------------------------------------------------------------------
44
+
45
+ def to_s
46
+ @infix
47
+ end
48
+
49
+ #--------------------------------------------------------------------------------------
50
+ #
51
+ #--------------------------------------------------------------------------------------
52
+
53
+ def formula?
54
+ @formula
55
+ end
56
+
57
+ #--------------------------------------------------------------------------------------
58
+ #
59
+ #--------------------------------------------------------------------------------------
60
+
61
+ # private
29
62
 
63
+ #--------------------------------------------------------------------------------------
64
+ #
65
+ #--------------------------------------------------------------------------------------
66
+
67
+ def initialize(infix, formula = false)
68
+ @infix = infix
69
+ @formula = formula
70
+ @environment = R.list
71
+ end
72
+
73
+ end
74
+ =end
75
+ #--------------------------------------------------------------------------------------
76
+ #
77
+ #--------------------------------------------------------------------------------------
78
+
30
79
  class RExpression < Object
31
80
 
81
+ #--------------------------------------------------------------------------------------
82
+ # Builds
83
+ #--------------------------------------------------------------------------------------
84
+
85
+ def self.build(expression)
86
+ R.parse(text: expression.to_s)
87
+ end
88
+
89
+ end
90
+
91
+ end
92
+
93
+ #=========================================================================================
94
+ #
95
+ #=========================================================================================
96
+ =begin
97
+ module E
98
+
99
+ #----------------------------------------------------------------------------------------
100
+ # @param symbol [Symbol]
101
+ # @param args [Array] arguments to the missing method
102
+ #----------------------------------------------------------------------------------------
103
+
104
+ def self.method_missing(symbol, *args)
105
+ name = R::Support.convert_symbol2r(symbol)
106
+
107
+ params = []
108
+ args.each do |arg|
109
+ params << R::Expression.parse_expression(arg)
110
+ end
111
+ exp = "#{name}(#{params.join(", ")})"
112
+
113
+ # rhs = R.rhs(R.as__formula("~ #{exp}"))
114
+ # R.enq(rhs)
115
+
116
+ R::Expression.new(exp)
32
117
  end
33
118
 
119
+ #=begin
120
+ #----------------------------------------------------------------------------------------
121
+ # @param symbol [Symbol]
122
+ # @param args [Array] arguments to the missing method
123
+ #----------------------------------------------------------------------------------------
124
+
125
+ def self.method_missing(symbol, *args)
126
+ name = R::Support.convert_symbol2r(symbol)
127
+ R::Language.build(name, *args)
128
+ end
129
+ #=end
130
+
131
+ #--------------------------------------------------------------------------------------
132
+ #
133
+ #--------------------------------------------------------------------------------------
134
+
135
+ def self.[](executable)
136
+ R::RubyCallback.build(executable)
137
+ end
138
+
34
139
  end
140
+ =end
@@ -36,7 +36,9 @@ module R
36
36
 
37
37
  def [](index)
38
38
  if (index.is_a? Array)
39
- R::Support.exec_function_name("`[[`", @r_interop, R.internal_eval(:c, *index))
39
+ # R::Support.exec_function_name("`[[`", @r_interop, R.internal_eval(:c, *index))
40
+ R::Support.exec_function(R::Support.dbk_index, @r_interop,
41
+ R.internal_eval(:c, *index))
40
42
  else
41
43
  R::Support.exec_function_name("`[`", @r_interop, index)
42
44
  end
@@ -25,7 +25,9 @@ module R
25
25
 
26
26
  class Language < Object
27
27
  include BinaryOperators
28
- include FormulaBinOp
28
+ include ExpBinOp
29
+ include IndexedObject
30
+ # include CallBinOp
29
31
 
30
32
  attr_accessor :expression
31
33
 
@@ -38,10 +40,15 @@ module R
38
40
  end
39
41
 
40
42
  #--------------------------------------------------------------------------------------
41
- #
43
+ # Builds a call functions with the given arguments.
44
+ # @param function_name [String] name of the function to apply: ex: '+'
45
+ # @param args [Array] two elements array for the binary function: ex [:a, :b]
46
+ # @return call expression. With the above paramenters the result would be:
47
+ # .Primitive("+")(a, b)
42
48
  #--------------------------------------------------------------------------------------
43
49
 
44
50
  def self.build(function_name, *args)
51
+
45
52
  res = R::Language.new(
46
53
  R::Support.eval("as.call").
47
54
  call(R::Support.parse2list(
@@ -49,8 +56,17 @@ module R
49
56
  ))
50
57
  res.expression = "#{Language.to_expression(args[0])} #{function_name.delete("`")} #{Language.to_expression(args[1])}"
51
58
  res
59
+
52
60
  end
53
61
 
62
+ #--------------------------------------------------------------------------------------
63
+ #
64
+ #--------------------------------------------------------------------------------------
65
+
66
+ def assign(expression)
67
+ exec_bin_oper("`<-`", expression).eval
68
+ end
69
+
54
70
  #--------------------------------------------------------------------------------------
55
71
  #
56
72
  #--------------------------------------------------------------------------------------
@@ -32,6 +32,20 @@ module R
32
32
  include BinaryOperators
33
33
  include ExecBinOp
34
34
  include UnaryOperators
35
+ include ExecUniOp
36
+
37
+ #--------------------------------------------------------------------------------------
38
+ # When indexing with '[' or '[[' an R object is returned. Sometimes we need to have
39
+ # access to an umboxed Ruby element, for instance, in an numeric array, we might want
40
+ # to receive the actual number that can be used in a Ruby method. In this case, we
41
+ # use the '<<' operator.
42
+ # @return the Ruby element at the given index in the vector
43
+ #--------------------------------------------------------------------------------------
44
+
45
+ def <<(index)
46
+ @r_interop[index]
47
+ end
48
+
35
49
  end
36
50
 
37
51
  end
@@ -35,7 +35,11 @@ module R
35
35
  #--------------------------------------------------------------------------------------
36
36
 
37
37
  def [](*index)
38
- R::Support.exec_function(R::Support.md_index, @r_interop, *index)
38
+ if (index.size > 1)
39
+ R::Support.exec_function(R::Support.md_index, @r_interop, *index)
40
+ else
41
+ super(*index)
42
+ end
39
43
  end
40
44
 
41
45
  end
@@ -24,7 +24,7 @@
24
24
  require_relative 'r_methods'
25
25
 
26
26
  module R
27
-
27
+
28
28
  #--------------------------------------------------------------------------------------
29
29
  #
30
30
  #--------------------------------------------------------------------------------------
@@ -43,13 +43,36 @@ module R
43
43
  end
44
44
 
45
45
  #--------------------------------------------------------------------------------------
46
- #
46
+ # Checks for equality between two R::Objects. This method is used by rspec's
47
+ # expectation. It returns a Ruby true or false and not an R::Vector with [TRUE] or
48
+ # [FALSE]
49
+ #--------------------------------------------------------------------------------------
50
+
51
+ def ==(other_object)
52
+ res = R::Support.exec_function_name("`==`", @r_interop, other_object)
53
+ return nil if (res.length << 0) == 0
54
+ res << 0
55
+ end
56
+
57
+ #--------------------------------------------------------------------------------------
58
+ # Use eql to check for equality between two objects and receive in return an R::Vector
47
59
  #--------------------------------------------------------------------------------------
48
60
 
49
- def as__data__frame
50
- R.as__data__frame.call(@r_interop)
61
+ def eql(other_object)
62
+ # exec_bin_oper("`==`", other_object)
63
+ R::Support.exec_function_name("`==`", @r_interop, other_object)
51
64
  end
52
65
 
66
+ #--------------------------------------------------------------------------------------
67
+ # @TODO: rspec sometimes calls to_ary when an expected value is false. I still don't
68
+ # understand this call. Returning an array with a number inside seems to make
69
+ # rspec happy, however this could have other consequences I don't know of.
70
+ #--------------------------------------------------------------------------------------
71
+
72
+ def to_ary
73
+ [1]
74
+ end
75
+
53
76
  #--------------------------------------------------------------------------------------
54
77
  # @param r_interop [Interop] pointer to an R object
55
78
  # @return the R object wrapped in a Ruby class
@@ -60,30 +83,42 @@ module R
60
83
  # if the value is actually not an r_interop, then just return it: native Ruby
61
84
  # object
62
85
  if (!Truffle::Interop.foreign?(r_interop))
86
+ # puts "I'm native"
63
87
  return r_interop
64
88
  # a matrix is also a vector... test should come before
65
89
  elsif (R::Support.eval("is.matrix").call(r_interop) == true)
66
- Matrix.new(r_interop)
90
+ # puts "1"
91
+ R::Matrix.new(r_interop)
67
92
  elsif (R::Support.eval("is.atomic").call(r_interop) == true)
93
+ # puts "2"
68
94
  Vector.new(r_interop)
69
95
  elsif (R::Support.eval("is.function").call(r_interop) == true)
96
+ # puts "3"
70
97
  Closure.new(r_interop)
71
98
  elsif (R::Support.eval("is.data.frame").call(r_interop) == true)
99
+ # puts "4"
72
100
  DataFrame.new(r_interop)
73
101
  elsif (R::Support.eval("is.list").call(r_interop) == true)
102
+ # puts "5"
74
103
  List.new(r_interop)
75
104
  elsif (R::Support.eval("typeof").call(r_interop) == "language")
105
+ # puts "6"
76
106
  Language.new(r_interop)
77
107
  elsif (R::Support.eval("typeof").call(r_interop) == "expression")
108
+ # puts "7"
78
109
  RExpression.new(r_interop)
79
110
  elsif (R::Support.eval("typeof").call(r_interop) == "name")
111
+ # puts "8"
80
112
  p "i'm of type name"
81
113
  Name.new(r_interop)
82
114
  elsif (R::Support.eval("typeof").call(r_interop) == "symbol")
115
+ # puts "9"
83
116
  RSymbol.new(r_interop)
84
117
  elsif (R::Support.eval("typeof").call(r_interop) == "environment")
118
+ # puts "10"
85
119
  Environment.new(r_interop)
86
120
  else # Generic type
121
+ # puts "11"
87
122
  p "Generic type: #{R::Support.eval("typeof").call(r_interop).to_s}"
88
123
  r_interop
89
124
  end
@@ -191,7 +226,7 @@ module R
191
226
  def rclass=(class_name)
192
227
  setR_name("`class<-`", class_name)
193
228
  end
194
-
229
+
195
230
  def rclass
196
231
  R::Support.exec_function_name("class", @r_interop)
197
232
  end
@@ -236,16 +271,15 @@ module R
236
271
  # @bug Needed to create method R.row__names because dispatch is not working properly
237
272
  #--------------------------------------------------------------------------------------
238
273
 
239
- def row__names
240
- R::Support.exec_function(R::Support.get_row_names, @r_interop)
241
- end
242
-
243
274
  # since we need to call a method and the method changes the object, then we need to
244
275
  # change our internal pointer also @r_interop. Ideally, just setting the row.names
245
276
  # should work.
246
277
  def row__names=(names_vector)
247
- @r_interop = R::Support.set_row_names.call(@r_interop, names_vector.r_interop)
248
- self
278
+ setR_name("`row.names<-`", names_vector)
279
+ end
280
+
281
+ def row__names
282
+ R::Support.exec_function_name("row.names", @r_interop)
249
283
  end
250
284
 
251
285
  #--------------------------------------------------------------------------------------
@@ -284,19 +318,23 @@ module R
284
318
 
285
319
  def to_s
286
320
 
287
- cap = nil
288
- # dev = R::Device.new('png', width: 5, height: 7, dpi: 300, record: true) {
289
- cap = R::Support.capture.call(r_interop)
290
- # cap = R::Support.capture_output.call(r_interop)
291
- # }
292
- str = String.new
293
- (0...(cap.size - 1)).each do |i|
294
- str << cap[i] << "\n"
321
+ begin
322
+
323
+ cap = nil
324
+ cap = R::Support.capture2.call(r_interop)
325
+ str = String.new
326
+ (0...(cap.size - 1)).each do |i|
327
+ str << cap[i] << "\n"
328
+ end
329
+ str << cap[cap.size - 1] if cap.size >= 1
330
+ str
331
+
332
+ rescue StandardError => e
333
+ puts e
295
334
  end
296
- str << cap[cap.size - 1] if cap.size >= 1
297
- str
335
+
298
336
  end
299
-
337
+
300
338
  end
301
339
 
302
340
  end
@@ -23,7 +23,7 @@
23
23
 
24
24
 
25
25
  module R
26
-
26
+
27
27
  #--------------------------------------------------------------------------------------
28
28
  # The empty_symbol is necessary to represent indexing with a missing argument such
29
29
  # as [x, ]. What follows the ',' is an empty_symbol. Whenever we use in Ruby the
@@ -64,24 +64,33 @@ module R
64
64
  @@exec_from_ruby = Polyglot.eval("R", <<-R)
65
65
  function(build_method, ...) {
66
66
  # print(build_method);
67
- # print("exec_from_ruby")
67
+ # print("exec_from_ruby");
68
68
  # args = list(...);
69
69
  # print(args);
70
- res = do.call(...);
70
+
71
+ # function 'invoke' from package rlang should do a cleaner than
72
+ # do.call (this is what the documentation says).
73
+ # res = do.call(...);
74
+ res = invoke(...);
75
+
71
76
  # print(res);
72
77
  res2 = build_method(res);
73
78
  # print(res2);
74
79
  res2
75
80
  }
76
81
  R
77
-
82
+
78
83
  #----------------------------------------------------------------------------------------
79
84
  # Evaluates an R code
80
85
  # @param string [String] A string of R code that can be correctly parsed by R
81
86
  #----------------------------------------------------------------------------------------
82
87
 
83
88
  def self.eval(string)
84
- Polyglot.eval("R", string)
89
+ begin
90
+ Polyglot.eval("R", string)
91
+ rescue RuntimeError
92
+ raise NoMethodError.new("Method #{string} not found in R environment")
93
+ end
85
94
  end
86
95
 
87
96
  #----------------------------------------------------------------------------------------
@@ -92,7 +101,31 @@ module R
92
101
  def self.interop(object)
93
102
  Truffle::Interop.foreign?(object)
94
103
  end
104
+
105
+ =begin
106
+ #----------------------------------------------------------------------------------------
107
+ # Builds a quo or a formula based on the given expression. If the expression has a
108
+ # '=~' operator, then a formula should be constructed, if not, then an quo should be
109
+ # constructed
110
+ # @param expression [R::Expression]
111
+ # @return quo or formula
112
+ #----------------------------------------------------------------------------------------
113
+
114
+ def self.quo_or_formula(expression)
95
115
 
116
+ if expression.formula?
117
+ R.as__formula(expression.infix)
118
+ else
119
+ # add a '~' before the expression so we can parse it as an
120
+ # R expression from a string. Didn't find a good way of creating an
121
+ # expression from string.
122
+ # R.as_quosure(R.as__formula("~ #{expression.infix}"))
123
+ R.rhs(R.as__formula("~ #{expression.infix}"))
124
+ end
125
+
126
+ end
127
+ =end
128
+
96
129
  #----------------------------------------------------------------------------------------
97
130
  # @param arg [Object] A Ruby object to be converted to R to be used by an R function, or
98
131
  # whatever needs it
@@ -100,12 +133,11 @@ module R
100
133
  #----------------------------------------------------------------------------------------
101
134
 
102
135
  def self.parse_arg(arg)
103
-
136
+
104
137
  case(arg)
105
- when Truffle::Interop
138
+ when -> (arg) { interop(arg) }
106
139
  arg
107
140
  when R::Object
108
- # return (Truffle::Interop.null?(arg.r_interop)) ? nil : arg.r_interop
109
141
  arg.r_interop
110
142
  when NegRange
111
143
  final_value = (arg.exclude_end?)? (arg.last - 1) : arg.last
@@ -116,10 +148,12 @@ module R
116
148
  when :all
117
149
  R.empty_symbol
118
150
  when Symbol
119
- arg = R::Support.eval("as.name").call(arg.to_s)
151
+ arg = R::Support.eval("as.name").call(arg.to_s.gsub(/__/,"."))
120
152
  when Proc, Method
121
153
  R::RubyCallback.build(arg)
122
- else
154
+ # when R::Expression
155
+ # R::Support.quo_or_formula(arg).r_interop
156
+ else # This is already a Ruby argument
123
157
  arg
124
158
  end
125
159
 
@@ -138,7 +172,7 @@ module R
138
172
  if (arg.is_a? Hash)
139
173
  arg.each_pair do |key, value|
140
174
  k = key.to_s.gsub(/__/,".")
141
- # HAS CHANGED IN RC6... FIXME: THIS TO THE NEW API
175
+ # HAS CHANGED IN RC6... @TODO: THIS TO THE NEW API
142
176
  # When evaluating to NA, Interop treats it as FALSE. This breaks
143
177
  # all expectations about NA. We need to protect NA from Interop
144
178
  # unboxing. Class NotAvailable
@@ -183,15 +217,6 @@ module R
183
217
  name.gsub!("rclass", "class")
184
218
  name
185
219
  end
186
-
187
- #----------------------------------------------------------------------------------------
188
- # Executes the given R function with the given arguments.
189
- #----------------------------------------------------------------------------------------
190
-
191
- def self.exec_function_i(function, *args)
192
- pl = R::Support.parse2list(*args)
193
- R::Support.eval("do.call").call(function, pl)
194
- end
195
220
 
196
221
  #----------------------------------------------------------------------------------------
197
222
  # @param function [R function (Interop)] R function to execute
@@ -202,42 +227,49 @@ module R
202
227
 
203
228
  def self.exec_function(function, *args)
204
229
 
205
- # If the execution counter is 0, function was not recursively called
206
- # Starts capturing output
207
- if (@@exec_counter == 0)
208
- R::Support.eval("unlockBinding('r_capture', globalenv())")
209
- @@con = R::Support.start_capture.call("r_capture")
210
- end
211
-
212
- @@exec_counter = @@exec_counter + 1
213
-
214
- # function has no arguments, call it directly
215
- if (args.length == 0)
216
- res = R::Object.build(function.call)
217
- else
218
- pl = R::Support.parse2list(*args)
219
- res = @@exec_from_ruby.call(R::Object.method(:build), function, pl)
220
- # R::Object.build(R::Support.eval("do.call").call(function, pl))
221
- end
222
-
223
- @@exec_counter = @@exec_counter - 1
224
-
225
- # When execution counter back to 0, print the captured output if the lenght
226
- # of the output is greater than 0
227
- if (@@exec_counter == 0)
228
- R::Support.stop_capture.call(@@con)
230
+ begin
231
+
232
+ # If the execution counter is 0, function was not recursively called
233
+ # Starts capturing output
234
+ if (@@exec_counter == 0)
235
+ R::Support.eval("unlockBinding('r_capture', globalenv())")
236
+ @@con = R::Support.start_capture.call("r_capture")
237
+ end
229
238
 
230
- if (R::Support.eval("length(r_capture) > 0")[0])
231
- cap = R::Object.build(R::Support.eval("r_capture"))
232
- # puts ~:r_capture if (R::Support.eval("length(r_capture) > 0")[0])
233
- (0...cap.size).each do |i|
234
- puts cap << i
239
+ @@exec_counter = @@exec_counter + 1
240
+
241
+ # function has no arguments, call it directly
242
+ if (args.length == 0)
243
+ res = R::Object.build(function.call)
244
+ else
245
+ pl = R::Support.parse2list(*args)
246
+ res = @@exec_from_ruby.call(R::Object.method(:build), function, pl)
247
+ # R::Object.build(R::Support.eval("do.call").call(function, pl))
248
+ end
249
+
250
+ @@exec_counter = @@exec_counter - 1
251
+
252
+ # When execution counter back to 0, print the captured output if the length
253
+ # of the output is greater than 0
254
+ if (@@exec_counter == 0)
255
+ R::Support.stop_capture.call(@@con)
256
+
257
+ if (R::Support.eval("length(r_capture) > 0")[0])
258
+ cap = R::Object.build(R::Support.eval("r_capture"))
259
+ (0...cap.size).each do |i|
260
+ puts cap << i
261
+ end
235
262
  end
263
+
236
264
  end
237
265
 
266
+ rescue StandardError => e
267
+ R::Support.stop_capture.call(@@con)
268
+ raise e
238
269
  end
239
-
270
+
240
271
  res
272
+
241
273
  end
242
274
 
243
275
  #----------------------------------------------------------------------------------------
@@ -251,6 +283,15 @@ module R
251
283
  R::Support.exec_function(f, *args)
252
284
  end
253
285
 
286
+ #----------------------------------------------------------------------------------------
287
+ # Executes the given R function with the given arguments.
288
+ #----------------------------------------------------------------------------------------
289
+
290
+ def self.exec_function_i(function, *args)
291
+ pl = R::Support.parse2list(*args)
292
+ R::Support.eval("do.call").call(function, pl)
293
+ end
294
+
254
295
  #----------------------------------------------------------------------------------------
255
296
  # sets the R symbol <name> to the given value
256
297
  # @param name [String] String representation of the R symbol that needs to be assigned
@@ -272,7 +313,7 @@ module R
272
313
 
273
314
  def self.r_evaluate(*args)
274
315
  r_args = args.map { |arg| R::Support.parse_arg(arg) }
275
- R::Object.build(eval("eval").call(*r_args))
316
+ R::Object.build(R::Support.eval("eval").call(*r_args))
276
317
  end
277
318
 
278
319
  #----------------------------------------------------------------------------------------
@@ -303,6 +344,22 @@ module R
303
344
 
304
345
  end
305
346
 
347
+ #----------------------------------------------------------------------------------------
348
+ #
349
+ #----------------------------------------------------------------------------------------
350
+
351
+
352
+ def self.print_str(obj)
353
+
354
+ lst = obj.as__list
355
+ puts lst
356
+ =begin
357
+ (1..lst.length << 0).each do |i|
358
+ puts lst[[i]]
359
+ end
360
+ =end
361
+ end
362
+
306
363
  #----------------------------------------------------------------------------------------
307
364
  # Prints a foreign R interop pointer. Used for debug.
308
365
  #----------------------------------------------------------------------------------------
@@ -316,13 +373,14 @@ module R
316
373
  class << self
317
374
  alias :pf :print_foreign
318
375
  end
319
-
376
+
320
377
  #----------------------------------------------------------------------------------------
321
378
  #
322
379
  #----------------------------------------------------------------------------------------
323
-
380
+
324
381
  end
325
382
 
326
383
  end
327
384
 
385
+ require_relative 'r_module_s'
328
386
  require_relative 'rsupport_scope'