galaaz 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +29 -0
  3. data/bin/gknit +208 -10
  4. data/bin/gknit2 +14 -0
  5. data/bin/gknit2~ +6 -0
  6. data/bin/prepareR.rb +3 -0
  7. data/bin/prepareR.rb~ +1 -0
  8. data/bin/tmp.py +51 -0
  9. data/blogs/dev/dev.Rmd +70 -0
  10. data/blogs/dev/dev.Rmd~ +104 -0
  11. data/blogs/dev/dev.html +209 -0
  12. data/blogs/dev/dev.md +72 -0
  13. data/blogs/dev/dev_files/figure-html/bubble-1.png +0 -0
  14. data/blogs/dev/model.rb +41 -0
  15. data/blogs/galaaz_ggplot/galaaz_ggplot.Rmd +55 -27
  16. data/blogs/galaaz_ggplot/galaaz_ggplot.aux +44 -0
  17. data/blogs/galaaz_ggplot/galaaz_ggplot.dvi +0 -0
  18. data/blogs/galaaz_ggplot/galaaz_ggplot.html +17 -4
  19. data/blogs/galaaz_ggplot/galaaz_ggplot.out +10 -0
  20. data/blogs/galaaz_ggplot/galaaz_ggplot.pdf +0 -0
  21. data/blogs/galaaz_ggplot/galaaz_ggplot.tex +630 -0
  22. data/blogs/galaaz_ggplot/midwest.Rmd +1 -1
  23. data/blogs/galaaz_ggplot/midwest_external_png +13 -0
  24. data/blogs/galaaz_ggplot/midwest_external_png~ +1 -0
  25. data/blogs/gknit/gknit.Rmd +500 -0
  26. data/blogs/gknit/gknit.Rmd~ +184 -0
  27. data/blogs/gknit/gknit.Rnd~ +17 -0
  28. data/blogs/gknit/gknit.html +528 -0
  29. data/blogs/gknit/gknit.md +628 -0
  30. data/blogs/gknit/gknit.pdf +0 -0
  31. data/blogs/gknit/gknit.tex +745 -0
  32. data/blogs/gknit/gknit_files/figure-html/bubble-1.png +0 -0
  33. data/blogs/gknit/gknit_files/figure-html/diverging_bar.png +0 -0
  34. data/blogs/gknit/model.rb +41 -0
  35. data/blogs/gknit/model.rb~ +46 -0
  36. data/blogs/ruby_plot/figures/dose_len.png +0 -0
  37. data/blogs/ruby_plot/figures/facet_by_delivery.png +0 -0
  38. data/blogs/ruby_plot/figures/facet_by_dose.png +0 -0
  39. data/blogs/ruby_plot/figures/facets_by_delivery_color.png +0 -0
  40. data/blogs/ruby_plot/figures/facets_by_delivery_color2.png +0 -0
  41. data/blogs/ruby_plot/figures/facets_with_decorations.png +0 -0
  42. data/blogs/ruby_plot/figures/facets_with_jitter.png +0 -0
  43. data/blogs/ruby_plot/figures/facets_with_points.png +0 -0
  44. data/blogs/ruby_plot/figures/final_box_plot.png +0 -0
  45. data/blogs/ruby_plot/figures/final_violin_plot.png +0 -0
  46. data/blogs/ruby_plot/figures/violin_with_jitter.png +0 -0
  47. data/blogs/ruby_plot/ruby_plot.Rmd +680 -0
  48. data/blogs/ruby_plot/ruby_plot.Rmd~ +215 -0
  49. data/blogs/ruby_plot/ruby_plot.html +563 -0
  50. data/blogs/ruby_plot/ruby_plot.md +731 -0
  51. data/blogs/ruby_plot/ruby_plot.pdf +0 -0
  52. data/blogs/ruby_plot/ruby_plot.tex +458 -0
  53. data/examples/sthda_ggplot/all.rb +0 -6
  54. data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_hex.rb +1 -1
  55. data/examples/sthda_ggplot/two_variables_cont_cont/misc.rb +1 -1
  56. data/examples/sthda_ggplot/two_variables_disc_cont/geom_bar.rb +2 -2
  57. data/examples/sthda_ggplot/two_variables_disc_disc/geom_jitter.rb +0 -1
  58. data/lib/R/eng_ruby.R +62 -0
  59. data/lib/R/eng_ruby.R~ +63 -0
  60. data/lib/R_interface/capture_plot.rb~ +23 -0
  61. data/lib/{R → R_interface}/expression.rb +0 -0
  62. data/lib/{R → R_interface}/r.rb +10 -1
  63. data/lib/{R → R_interface}/r.rb~ +0 -0
  64. data/lib/{R → R_interface}/r_methods.rb +21 -5
  65. data/lib/{R → R_interface}/rbinary_operators.rb +6 -1
  66. data/lib/R_interface/rclosure.rb +38 -0
  67. data/lib/{R → R_interface}/rdata_frame.rb +0 -0
  68. data/lib/R_interface/rdevices.R +31 -0
  69. data/lib/R_interface/rdevices.rb +225 -0
  70. data/lib/{R/rclosure.rb → R_interface/rdevices.rb~} +3 -10
  71. data/lib/{R → R_interface}/renvironment.rb +0 -0
  72. data/lib/{R → R_interface}/rexpression.rb +0 -0
  73. data/lib/{R → R_interface}/rindexed_object.rb +0 -0
  74. data/lib/{R → R_interface}/rlanguage.rb +0 -0
  75. data/lib/{R → R_interface}/rlist.rb +0 -0
  76. data/lib/{R → R_interface}/rmatrix.rb +0 -0
  77. data/lib/{R → R_interface}/rmd_indexed_object.rb +0 -0
  78. data/lib/{R → R_interface}/robject.rb +5 -0
  79. data/lib/{R → R_interface}/rpkg.rb +0 -0
  80. data/lib/{R → R_interface}/rsupport.rb +49 -13
  81. data/lib/{R → R_interface}/rsupport_scope.rb +0 -0
  82. data/lib/{R → R_interface}/rsymbol.rb +1 -0
  83. data/lib/{R → R_interface}/ruby_callback.rb +0 -0
  84. data/lib/{R → R_interface}/ruby_extensions.rb +2 -1
  85. data/lib/{R → R_interface}/runary_operators.rb +0 -0
  86. data/lib/{R → R_interface}/rvector.rb +0 -0
  87. data/lib/galaaz.rb +4 -2
  88. data/lib/gknit.rb +27 -0
  89. data/lib/gknit.rb~ +26 -0
  90. data/lib/gknit/knitr_engine.rb +120 -0
  91. data/lib/gknit/knitr_engine.rb~ +102 -0
  92. data/lib/gknit/ruby_engine.rb +70 -0
  93. data/lib/gknit/ruby_engine.rb~ +72 -0
  94. data/lib/util/exec_ruby.rb +8 -7
  95. data/lib/util/inline_file.rb +70 -0
  96. data/lib/util/inline_file.rb~ +23 -0
  97. data/r_requires/ggplot.rb +1 -8
  98. data/r_requires/knitr.rb +27 -0
  99. data/r_requires/knitr.rb~ +4 -0
  100. data/specs/r_language.spec.rb +22 -0
  101. data/specs/r_plots.spec.rb +72 -0
  102. data/specs/r_plots.spec.rb~ +37 -0
  103. data/specs/tmp.rb +255 -1
  104. data/version.rb +1 -1
  105. metadata +89 -39
@@ -21,14 +21,7 @@
21
21
  # OR MODIFICATIONS.
22
22
  ##########################################################################################
23
23
 
24
- module R
24
+ dir = File.dirname(File.expand_path('.', __FILE__))
25
+ src = "#{dir}/capture_plot.R"
25
26
 
26
- class Closure < Object
27
-
28
- def call(*args)
29
- R::Support.exec_function(@r_interop, *args)
30
- end
31
-
32
- end
33
-
34
- end
27
+ R.source(src)
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -283,7 +283,12 @@ module R
283
283
  #--------------------------------------------------------------------------------------
284
284
 
285
285
  def to_s
286
+
287
+ cap = nil
288
+ # dev = R::Device.new('png', width: 5, height: 7, dpi: 300, record: true) {
286
289
  cap = R::Support.capture.call(r_interop)
290
+ # cap = R::Support.capture_output.call(r_interop)
291
+ # }
287
292
  str = String.new
288
293
  (0...(cap.size - 1)).each do |i|
289
294
  str << cap[i] << "\n"
File without changes
@@ -57,11 +57,14 @@ module R
57
57
 
58
58
  module Support
59
59
 
60
+ @@exec_counter = 0
61
+
60
62
  # Using this method gives us more control over what happens when calling do.call
61
63
  # and allows for debugging. Use it in exec_function when debugging is needed.
62
64
  @@exec_from_ruby = Polyglot.eval("R", <<-R)
63
65
  function(build_method, ...) {
64
66
  # print(build_method);
67
+ # print("exec_from_ruby")
65
68
  # args = list(...);
66
69
  # print(args);
67
70
  res = do.call(...);
@@ -102,6 +105,7 @@ module R
102
105
  when Truffle::Interop
103
106
  arg
104
107
  when R::Object
108
+ # return (Truffle::Interop.null?(arg.r_interop)) ? nil : arg.r_interop
105
109
  arg.r_interop
106
110
  when NegRange
107
111
  final_value = (arg.exclude_end?)? (arg.last - 1) : arg.last
@@ -127,14 +131,14 @@ module R
127
131
  #----------------------------------------------------------------------------------------
128
132
 
129
133
  def self.parse2list(*args)
130
-
134
+
131
135
  params = Polyglot.eval("R", "list()")
132
136
 
133
137
  args.each_with_index do |arg, i|
134
138
  if (arg.is_a? Hash)
135
139
  arg.each_pair do |key, value|
136
140
  k = key.to_s.gsub(/__/,".")
137
- # HAS CHANGED IN RC6... FIX THIS TO THE NEW API
141
+ # HAS CHANGED IN RC6... FIXME: THIS TO THE NEW API
138
142
  # When evaluating to NA, Interop treats it as FALSE. This breaks
139
143
  # all expectations about NA. We need to protect NA from Interop
140
144
  # unboxing. Class NotAvailable
@@ -157,7 +161,10 @@ module R
157
161
  end
158
162
  end
159
163
 
160
- params
164
+ # if the parameter is an empty list, then add the element NULL to the list
165
+ (Polyglot.eval("R", "length").call(params) == 0) ?
166
+ Polyglot.eval("R", "list").call(nil) : params
167
+ # params
161
168
 
162
169
  end
163
170
 
@@ -194,25 +201,54 @@ module R
194
201
  #----------------------------------------------------------------------------------------
195
202
 
196
203
  def self.exec_function(function, *args)
204
+
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
+
197
214
  # function has no arguments, call it directly
198
215
  if (args.length == 0)
199
- return R::Object.build(function.call) # 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))
200
221
  end
201
222
 
202
- pl = R::Support.parse2list(*args)
203
- @@exec_from_ruby.call(R::Object.method(:build), function, pl)
204
- # R::Object.build(R::Support.eval("do.call").call(function, pl))
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)
229
+
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
235
+ end
236
+ end
237
+
238
+ end
239
+
240
+ res
205
241
  end
206
242
 
207
243
  #----------------------------------------------------------------------------------------
208
244
  # @param function_name [String] Name of the R function to execute
209
- # @param internal [Boolean] true if returning to an internal object, i.e., does not
210
- # wrap the return object in a Ruby object
211
245
  # @args [Array] Array of arguments for the function
212
246
  #----------------------------------------------------------------------------------------
213
247
 
214
248
  def self.exec_function_name(function_name, *args)
215
- R::Support.exec_function(R::Support.eval(function_name), *args)
249
+ # TODO: should check the function name before calling eval
250
+ f = R::Support.eval(function_name)
251
+ R::Support.exec_function(f, *args)
216
252
  end
217
253
 
218
254
  #----------------------------------------------------------------------------------------
@@ -253,10 +289,10 @@ module R
253
289
  name = R::Support.convert_symbol2r(symbol)
254
290
 
255
291
  case name
256
- # missing method has an '=' sign in it...
292
+ # when missing method has an '=' sign in it...
257
293
  when ->(x) { x =~ /(.*)=$/ }
258
294
  R::Support.set_symbol($1, *args)
259
- # missing method is 'eval'... needs special treatment
295
+ # when missing method is 'eval'... needs special treatment
260
296
  when "eval"
261
297
  R::Support.r_evaluate(*args)
262
298
  else
@@ -264,7 +300,7 @@ module R
264
300
  internal ? R::Support.exec_function_i(function, *args) :
265
301
  R::Support.exec_function(function, *args)
266
302
  end
267
-
303
+
268
304
  end
269
305
 
270
306
  #----------------------------------------------------------------------------------------
File without changes
@@ -36,6 +36,7 @@ module R
36
36
  #--------------------------------------------------------------------------------------
37
37
 
38
38
  def =~(other_object)
39
+ # puts "doing #{self} =~ #{other_object}"
39
40
  exec_oper("`~`", other_object, true)
40
41
  end
41
42
 
File without changes
@@ -60,7 +60,8 @@ class Symbol
60
60
  #--------------------------------------------------------------------------------------
61
61
 
62
62
  def +@
63
- R::Object.build(R::Support.eval("as.name").call(to_s))
63
+ var = (self == :all)? '.' : to_s
64
+ R::Object.build(R::Support.eval("as.name").call(var))
64
65
  end
65
66
 
66
67
  #--------------------------------------------------------------------------------------
File without changes
@@ -21,8 +21,10 @@
21
21
  # OR MODIFICATIONS.
22
22
  ##########################################################################################
23
23
 
24
- require_relative 'R/r'
25
- require_relative 'util/exec_ruby'
24
+ require 'pry'
26
25
 
27
26
  $LOAD_PATH << File.dirname(File.expand_path('..', __FILE__)) + "/r_requires"
28
27
 
28
+ require_relative 'R_interface/r'
29
+ require_relative 'util/exec_ruby'
30
+ require_relative 'util/inline_file'
@@ -0,0 +1,27 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # @author Rodrigo Botafogo
5
+ #
6
+ # Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
7
+ # and distribute this software and its documentation, without fee and without a signed
8
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
9
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
10
+ # distributions.
11
+ #
12
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
13
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
14
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
15
+ # POSSIBILITY OF SUCH DAMAGE.
16
+ #
17
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
19
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
20
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
21
+ # OR MODIFICATIONS.
22
+ ##########################################################################################
23
+
24
+ require 'galaaz'
25
+
26
+ require_relative 'gknit/knitr_engine'
27
+ require_relative 'gknit/ruby_engine'
@@ -0,0 +1,26 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # @author Rodrigo Botafogo
5
+ #
6
+ # Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
7
+ # and distribute this software and its documentation, without fee and without a signed
8
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
9
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
10
+ # distributions.
11
+ #
12
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
13
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
14
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
15
+ # POSSIBILITY OF SUCH DAMAGE.
16
+ #
17
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
19
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
20
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
21
+ # OR MODIFICATIONS.
22
+ ##########################################################################################
23
+
24
+ require 'galaaz'
25
+
26
+ puts "gknit loaded"
@@ -0,0 +1,120 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # @author Rodrigo Botafogo
5
+ #
6
+ # Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
7
+ # and distribute this software and its documentation, without fee and without a signed
8
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
9
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
10
+ # distributions.
11
+ #
12
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
13
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
14
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
15
+ # POSSIBILITY OF SUCH DAMAGE.
16
+ #
17
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
19
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
20
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
21
+ # OR MODIFICATIONS.
22
+ ##########################################################################################
23
+
24
+ R.install_and_loads('knitr', 'rmarkdown')
25
+
26
+ # dir = File.dirname(File.expand_path('.', __FILE__))
27
+ # src = "#{dir}/R/eng_ruby.R"
28
+
29
+ class KnitrEngine
30
+
31
+ attr_reader :options
32
+ attr_reader :keep
33
+
34
+ #--------------------------------------------------------------------------------------
35
+ # opens a device for plot recording. Method chunk_device in knitr is not exported,
36
+ # so it cannot yet be accessed by Galaaz. Need to develop a local function that calls
37
+ # the non-exported function.
38
+ #--------------------------------------------------------------------------------------
39
+
40
+ R::Support.eval(<<-R)
41
+ _ck_dv = function(width, height, record = TRUE, dev, dev.args, dpi, options,
42
+ tmp = tempfile()) {
43
+ knitr:::chunk_device(width, height, record, dev, dev.args, dpi, options, tmp)
44
+ }
45
+ R
46
+
47
+ #--------------------------------------------------------------------------------------
48
+ # The showtext package, is able to support more font formats and more graphics
49
+ # devices, and avoids using external software such as Ghostscript. showtext makes it
50
+ # even easier to use various types of fonts (TrueType, OpenType, Type 1, web fonts,
51
+ # etc.) in R graphs.
52
+ #--------------------------------------------------------------------------------------
53
+
54
+ R::Support.eval(<<-R)
55
+ showtext = function() {
56
+ showtext::showtext_begin()
57
+ }
58
+ R
59
+
60
+ #--------------------------------------------------------------------------------------
61
+ # Process the fig.keep chunk option
62
+ # @param keep [String/Numeric] a string or a number. If it is a string then it should
63
+ # be one of the following:
64
+ # * high: only keep high-level plots (merge low-level changes into high-level plots);
65
+ # * none: discard all plots;
66
+ # * all: keep all plots (low-level plot changes may produce new plots)
67
+ # * first: only keep the first plot
68
+ # * last: only keep the last plot
69
+ # if set to a numeric vector: interpret value as index of (low-level) plots to keep
70
+ # In this case, set variable @keep to "index" and variable @keep_ind as the numeric
71
+ # index.
72
+ #--------------------------------------------------------------------------------------
73
+
74
+ def self.fig_keep
75
+ @keep = @options.fig__keep
76
+ @keep_idx = nil
77
+
78
+ if (@keep.is__numeric << 0)
79
+ @keep_idx = @keep
80
+ @keep = "index"
81
+ end
82
+
83
+ end
84
+
85
+ #--------------------------------------------------------------------------------------
86
+ #
87
+ #--------------------------------------------------------------------------------------
88
+
89
+ def self.file_ext
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)
93
+ end
94
+ end
95
+
96
+ #--------------------------------------------------------------------------------------
97
+ # Process the chunk options
98
+ #--------------------------------------------------------------------------------------
99
+
100
+ def self.process_options(options)
101
+ @options = options
102
+
103
+ # verifies if figures should be kept
104
+ fig_keep
105
+
106
+ # if figures are to be kept, take or guess the file extension
107
+ # file_ext
108
+ end
109
+
110
+ #--------------------------------------------------------------------------------------
111
+ # Adds the new knitr engine to the list of engines
112
+ # @param spec [Hash] hash with only one pair containing the machine key and the engine
113
+ # function
114
+ #--------------------------------------------------------------------------------------
115
+
116
+ def add(spec)
117
+ (~:knit_engines).set.call(spec)
118
+ end
119
+
120
+ end
@@ -0,0 +1,102 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # @author Rodrigo Botafogo
5
+ #
6
+ # Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
7
+ # and distribute this software and its documentation, without fee and without a signed
8
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
9
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
10
+ # distributions.
11
+ #
12
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
13
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
14
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
15
+ # POSSIBILITY OF SUCH DAMAGE.
16
+ #
17
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
19
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
20
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
21
+ # OR MODIFICATIONS.
22
+ ##########################################################################################
23
+
24
+ R.install_and_loads('knitr', 'rmarkdown')
25
+
26
+ # dir = File.dirname(File.expand_path('.', __FILE__))
27
+ # src = "#{dir}/R/eng_ruby.R"
28
+
29
+ class KnitrEngine
30
+
31
+ attr_reader :keep
32
+
33
+ #--------------------------------------------------------------------------------------
34
+ # opens a device for plot recording. Method chunk_device in knitr is not exported,
35
+ # so it cannot yet be accessed by Galaaz. Need to develop a local function that calls
36
+ # the non-exported function.
37
+ #--------------------------------------------------------------------------------------
38
+
39
+ R::Support.eval(<<-R)
40
+ _ck_dv = function(width, height, record = TRUE, dev, dev.args, dpi, options,
41
+ tmp = tempfile()) {
42
+ knitr:::chunk_device(width, height, record, dev, dev.args, dpi, options, tmp)
43
+ }
44
+ R
45
+
46
+ #--------------------------------------------------------------------------------------
47
+ # The showtext package, is able to support more font formats and more graphics
48
+ # devices, and avoids using external software such as Ghostscript. showtext makes it
49
+ # even easier to use various types of fonts (TrueType, OpenType, Type 1, web fonts,
50
+ # etc.) in R graphs.
51
+ #--------------------------------------------------------------------------------------
52
+
53
+ R::Support.eval(<<-R)
54
+ showtext = function() {
55
+ print("showtext beginning")
56
+ showtext::showtext_begin()
57
+ }
58
+ R
59
+
60
+ #--------------------------------------------------------------------------------------
61
+ # Process the fig.keep chunk option
62
+ # @bug this does not what it should
63
+ #--------------------------------------------------------------------------------------
64
+
65
+ def self.fig_keep(keep)
66
+ puts "checking fig_keep options"
67
+
68
+ @keep = keep
69
+ puts @keep
70
+
71
+ @keep.idx = nil
72
+
73
+ if (@keep.is__numeric << 0)
74
+ puts "keep is numeric"
75
+
76
+ @keep.idx = keep
77
+ @keep = "index"
78
+ end
79
+
80
+ puts @keep
81
+
82
+ end
83
+
84
+ #--------------------------------------------------------------------------------------
85
+ # Process the chunk options
86
+ #--------------------------------------------------------------------------------------
87
+
88
+ def self.process_options(options)
89
+ fig_keep(options.fig__keep)
90
+ end
91
+
92
+ #--------------------------------------------------------------------------------------
93
+ # Adds the new knitr engine to the list of engines
94
+ # @param spec [Hash] hash with only one pair containing the machine key and the engine
95
+ # function
96
+ #--------------------------------------------------------------------------------------
97
+
98
+ def add(spec)
99
+ (~:knit_engines).set.call(spec)
100
+ end
101
+
102
+ end