galaaz 0.4.5 → 0.4.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +696 -270
  3. data/Rakefile +9 -22
  4. data/bin/gknit +2 -217
  5. data/bin/gknit_old_r +236 -0
  6. data/bin/grun +5 -0
  7. data/blogs/dev/dev.Rmd +7 -0
  8. data/blogs/dev/dev.html +34 -26
  9. data/blogs/dev/dev.md +40 -25
  10. data/blogs/dev/dev_files/figure-html/bubble-1.png +0 -0
  11. data/blogs/dev/dev_files/figure-html/diverging_bar. +0 -0
  12. data/blogs/dev/dev_files/figure-html/diverging_bar.png +0 -0
  13. data/blogs/galaaz_ggplot/galaaz_ggplot.Rmd +4 -4
  14. data/blogs/galaaz_ggplot/galaaz_ggplot.html +251 -59
  15. data/blogs/galaaz_ggplot/galaaz_ggplot.log +640 -0
  16. data/blogs/galaaz_ggplot/galaaz_ggplot.md +199 -95
  17. data/blogs/galaaz_ggplot/galaaz_ggplot.tex +45 -228
  18. data/blogs/galaaz_ggplot/midwest.png +0 -0
  19. data/blogs/galaaz_ggplot/scatter_plot.png +0 -0
  20. data/blogs/gknit/gknit.Rmd +271 -148
  21. data/blogs/manual/manual.Rmd +212 -0
  22. data/blogs/manual/manual.html +1832 -0
  23. data/blogs/manual/manual.md +751 -0
  24. data/blogs/manual/manual_files/figure-html/diverging_bar.png +0 -0
  25. data/blogs/ruby_plot/ruby_plot.Rmd +5 -69
  26. data/blogs/ruby_plot/ruby_plot.html +195 -236
  27. data/blogs/ruby_plot/ruby_plot.md +1 -261
  28. data/blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.svg +38 -38
  29. data/examples/sthda_ggplot/two_variables_disc_cont/geom_dotplot.rb +5 -5
  30. data/examples/sthda_ggplot/two_variables_disc_cont/geom_jitter.rb +1 -0
  31. data/examples/sthda_ggplot/two_variables_disc_cont/geom_violin.rb +3 -7
  32. data/examples/sthda_ggplot/two_variables_error/geom_crossbar.rb +3 -1
  33. data/lib/R_interface/r.rb +12 -9
  34. data/lib/R_interface/r_methods.rb +2 -2
  35. data/lib/R_interface/rbinary_operators.rb +2 -20
  36. data/lib/R_interface/rdata_frame.rb +56 -9
  37. data/lib/R_interface/rdevices.R +0 -12
  38. data/lib/R_interface/rexpression.rb +0 -97
  39. data/lib/R_interface/rindexed_object.rb +12 -3
  40. data/lib/R_interface/rlanguage.rb +1 -1
  41. data/lib/R_interface/rlist.rb +29 -4
  42. data/lib/R_interface/rlogical_operators.rb +50 -0
  43. data/lib/R_interface/rmatrix.rb +7 -1
  44. data/lib/R_interface/robject.rb +29 -15
  45. data/lib/R_interface/rsupport.rb +74 -58
  46. data/lib/R_interface/rsymbol.rb +2 -1
  47. data/lib/R_interface/ruby_extensions.rb +11 -2
  48. data/lib/R_interface/rvector.rb +26 -11
  49. data/lib/gknit.rb +2 -0
  50. data/lib/gknit/include_engine.rb +57 -0
  51. data/lib/gknit/knitr_engine.rb +596 -50
  52. data/lib/gknit/rb_engine.rb +56 -0
  53. data/lib/gknit/ruby_engine.rb +13 -36
  54. data/lib/util/exec_ruby.rb +132 -21
  55. data/lib/util/inline_file.rb +9 -7
  56. data/specs/all.rb +5 -0
  57. data/specs/figures/bg.jpeg +0 -0
  58. data/specs/figures/bg.png +0 -0
  59. data/specs/figures/bg.svg +57 -0
  60. data/specs/figures/no_args.jpeg +0 -0
  61. data/specs/figures/no_args.png +0 -0
  62. data/specs/figures/no_args.svg +57 -0
  63. data/specs/figures/width_height.jpeg +0 -0
  64. data/specs/figures/width_height.png +0 -0
  65. data/specs/figures/width_height_units1.jpeg +0 -0
  66. data/specs/figures/width_height_units1.png +0 -0
  67. data/specs/figures/width_height_units2.jpeg +0 -0
  68. data/specs/figures/width_height_units2.png +0 -0
  69. data/specs/r_dataframe.spec.rb +29 -27
  70. data/specs/r_devices.spec.rb +347 -0
  71. data/specs/r_eval.spec.rb +10 -3
  72. data/specs/r_formula.spec.rb +2 -2
  73. data/specs/r_language.spec.rb +112 -0
  74. data/specs/r_list.spec.rb +174 -14
  75. data/specs/r_list_apply.spec.rb +17 -10
  76. data/specs/r_matrix.spec.rb +3 -3
  77. data/specs/r_vector_operators.spec.rb +13 -7
  78. data/specs/tmp.rb +42 -12
  79. data/version.rb +1 -1
  80. metadata +28 -24
  81. data/bin/gknit2 +0 -14
  82. data/bin/prepareR.rb +0 -3
  83. data/bin/tmp.py +0 -51
  84. data/blogs/gknit/gknit.html +0 -528
  85. data/blogs/gknit/gknit.md +0 -628
  86. data/blogs/gknit/gknit.pdf +0 -0
  87. data/blogs/gknit/gknit.tex +0 -745
  88. data/blogs/gknit/gknit_files/figure-html/bubble-1.png +0 -0
  89. data/blogs/gknit/gknit_files/figure-html/diverging_bar.png +0 -0
  90. data/blogs/ruby_plot/figures/dose_len.png +0 -0
  91. data/blogs/ruby_plot/figures/facet_by_delivery.png +0 -0
  92. data/blogs/ruby_plot/figures/facet_by_dose.png +0 -0
  93. data/blogs/ruby_plot/figures/facets_by_delivery_color.png +0 -0
  94. data/blogs/ruby_plot/figures/facets_by_delivery_color2.png +0 -0
  95. data/blogs/ruby_plot/figures/facets_with_decorations.png +0 -0
  96. data/blogs/ruby_plot/figures/facets_with_jitter.png +0 -0
  97. data/blogs/ruby_plot/figures/facets_with_points.png +0 -0
  98. data/blogs/ruby_plot/figures/final_box_plot.png +0 -0
  99. data/blogs/ruby_plot/figures/final_violin_plot.png +0 -0
  100. data/blogs/ruby_plot/figures/violin_with_jitter.png +0 -0
  101. data/lib/R/eng_ruby.R +0 -62
  102. data/lib/R_interface/rdevices.rb +0 -225
data/Rakefile CHANGED
@@ -160,11 +160,6 @@ blogs.each do |dir|
160
160
  (sh %{ bin/gknit #{blog_rmd} })
161
161
  end
162
162
 
163
- desc "run gknit2 to build #{dir} blog post"
164
- task "blog2:#{dir}" do
165
- (sh %{ bin/gknit2 #{blog_rmd} })
166
- end
167
-
168
163
  end
169
164
 
170
165
  #===========================================================================================
@@ -185,17 +180,19 @@ task :make_gem do
185
180
  (sh %{ gem build #{$gem_name}.gemspec })
186
181
  end
187
182
 
188
- =begin
189
- name = "#{$gem_name}-#{$version}.gem"
183
+ #===========================================================================================
184
+ # Publishes the gem at Rubygems
185
+ #===========================================================================================
186
+
187
+ desc 'Publish gem to rubygems'
188
+ task :publish_gem do
189
+ (sh %{ gem push #{$gem_name}-#{$version}.gem })
190
+ end
190
191
 
192
+ =begin
191
193
  desc 'default task'
192
194
  task :default => [:install_gem]
193
195
 
194
- desc 'Makes a Gem'
195
- task :make_gem do
196
- sh "gem build #{$gem_name}.gemspec"
197
- end
198
-
199
196
  desc 'Install the gem in the standard location'
200
197
  task :install_gem => [:make_gem] do
201
198
  sh "gem install #{$gem_name}-#{$version}-java.gem"
@@ -206,16 +203,6 @@ task :make_doc do
206
203
  sh "yard doc lib/*.rb lib/**/*.rb"
207
204
  end
208
205
 
209
- desc 'Push project to github'
210
- task :push do
211
- sh "git push origin master"
212
- end
213
-
214
- desc 'Push gem to rubygem'
215
- task :push_gem do
216
- sh "push #{name} -p $http_proxy"
217
- end
218
-
219
206
  Rake::TestTask.new do |t|
220
207
  t.libs << "test"
221
208
  t.test_files = FileList['test/complete.rb']
data/bin/gknit CHANGED
@@ -6,224 +6,9 @@ puts "Knitting #{dir}/#{ARGV[0]}"
6
6
 
7
7
  code = <<EOF
8
8
  eval.polyglot("ruby", "$LOAD_PATH.unshift %q(/home/rbotafogo/desenv/galaaz/lib)")
9
- eval.polyglot("ruby", "require %q(galaaz)")
10
- eval.polyglot("ruby", "R.install_and_loads %q(knitr)")
11
- eval.polyglot("ruby", "R.install_and_loads %q(rmarkdown)")
12
-
13
- # graphics devices in base R, plus those in Cairo, cairoDevice, tikzDevice
14
- auto_exts = c(
15
- bmp = "bmp", postscript = "eps", pdf = "pdf", png = "png", svg = "svg",
16
- jpeg = "jpeg", pictex = "tex", tiff = "tiff", win.metafile = "wmf",
17
- cairo_pdf = "pdf", cairo_ps = "eps",
18
-
19
- quartz_pdf = "pdf", quartz_png = "png", quartz_jpeg = "jpeg",
20
- quartz_tiff = "tiff", quartz_gif = "gif", quartz_psd = "psd",
21
- quartz_bmp = "bmp",
22
-
23
- CairoJPEG = "jpeg", CairoPNG = "png", CairoPS = "eps", CairoPDF = "pdf",
24
- CairoSVG = "svg", CairoTIFF = "tiff",
25
-
26
- Cairo_pdf = "pdf", Cairo_png = "png", Cairo_ps = "eps", Cairo_svg = "svg",
27
-
28
- svglite = "svg",
29
-
30
- tikz = "tex"
31
- )
32
-
33
- is_plot_output = function(x) {
34
- evaluate::is.recordedplot(x) || inherits(x, "knit_image_paths")
35
- }
36
-
37
- # find recorded plots in the output of evaluate()
38
- find_recordedplot = function(x) {
39
- vapply(x, is_plot_output, logical(1))
40
- }
41
-
42
- dev2ext = function(x) {
43
- res = auto_exts[x]
44
- if (any(idx <- is.na(res))) {
45
- for (i in x[idx]) check_dev(i)
46
- stop2(
47
- "cannot find appropriate filename extensions for device ", x[idx], "; ",
48
- "please use chunk option 'fig.ext' (https://yihui.name/knitr/options)"
49
- )
50
- }
51
- unname(res)
52
- }
53
-
54
- # filter the dev.args option
55
- get_dargs = function(dargs, dev) {
56
- if (length(dargs) == 0) return()
57
- if (is.list(dargs) && all(sapply(dargs, is.list))) {
58
- # dev.args is list(dev1 = list(arg1 = val1, ...), dev2 = list(arg2, ...))
59
- dargs = dargs[[dev]]
60
- }
61
- dargs
62
- }
63
-
64
- # open a device for a chunk; depending on the option global.device, may or may
65
- # not need to close the device on exit
66
- chunk_device = function(
67
- width, height, record = TRUE, dev, dev.args, dpi, options, tmp = tempfile()
68
- ) {
69
- dev_new = function() {
70
- # actually I should adjust the recording device according to dev, but here I
71
- # have only considered the png and tikz devices (because the measurement
72
- # results can be very different especially with the latter, see #1066), and
73
- # also the cairo_pdf device (#1235)
74
- if (identical(dev, "png")) {
75
- do.call(grDevices::png, c(list(
76
- filename = tmp, width = width, height = height, units = "in", res = dpi
77
- ), get_dargs(dev.args, "png")))
78
- } else if (identical(dev, "tikz")) {
79
- dargs = c(list(
80
- file = tmp, width = width, height = height
81
- ), get_dargs(dev.args, "tikz"))
82
- dargs$sanitize = options$sanitize; dargs$standAlone = options$external
83
- if (is.null(dargs$verbose)) dargs$verbose = FALSE
84
- do.call(tikz_dev, dargs)
85
- } else if (identical(dev, "cairo_pdf")) {
86
- do.call(grDevices::cairo_pdf, c(list(
87
- filename = tmp, width = width, height = height
88
- ), get_dargs(dev.args, "cairo_pdf")))
89
- } else if (identical(getOption("device"), "pdf_null")) {
90
- if (!is.null(dev.args)) {
91
- dev.args = get_dargs(dev.args, "pdf")
92
- dev.args = dev.args[intersect(names(dev.args), c("pointsize", "bg"))]
93
- }
94
- do.call(pdf_null, c(list(width = width, height = height), dev.args))
95
- } else dev.new(width = width, height = height)
96
- }
97
- if (!opts_knit$get("global.device")) {
98
- dev_new()
99
- dev.control(displaylist = if (record) "enable" else "inhibit") # enable recording
100
- # if returns TRUE, we need to close this device after code is evaluated
101
- return(TRUE)
102
- } else if (is.null(dev.list())) {
103
- # want to use a global device but not open yet
104
- dev_new()
105
- dev.control("enable")
106
- }
107
- FALSE
108
- }
109
-
110
- showtext = function(show) if (isTRUE(show)) showtext::showtext_begin()
111
-
112
- # define the ruby engine for processing Ruby chunks in
113
- # rmarkdown
114
- eng_ruby = function(options) {
115
- block_code = paste(options$code, collapse = "\\n");
116
- code = paste0("GalaazUtil.exec_ruby(",
117
- shQuote(block_code),
118
- ")
119
- ");
120
-
121
- keep = options$fig.keep
122
- keep.idx = NULL
123
- if (is.numeric(keep)) {
124
- keep.idx = keep
125
- keep = "index"
126
- }
127
-
128
- tmp.fig = tempfile(); on.exit(unlink(tmp.fig), add = TRUE)
129
-
130
- # open a device to record plots
131
- if (chunk_device(options$fig.width[1L], options$fig.height[1L], keep != "none",
132
- options$dev, options$dev.args, options$dpi, options, tmp.fig)) {
133
- # preserve par() settings from the last code chunk
134
- if (keep.pars <- opts_knit$get("global.par"))
135
- par2(opts_knit$get("global.pars"))
136
- showtext(options$fig.showtext) # showtext support
137
- dv = dev.cur()
138
- on.exit({
139
- if (keep.pars) opts_knit$set(global.pars = par(no.readonly = TRUE))
140
- dev.off(dv)
141
- }, add = TRUE)
142
- }
143
-
144
- # guess plot file type if it is NULL
145
- # if (keep != "none" && is.null(options$fig.ext))
146
- # options$fig.ext = dev2ext(options$dev)
147
-
148
- res = eval.polyglot("ruby", code);
149
-
150
- # rearrange locations of figures
151
- # figs = find_recordedplot(res)
152
- # if (length(figs) && any(figs)) {
153
- # if (keep == 'none') {
154
- # res = res[!figs] # remove all
155
- # } else {
156
- # if (options$fig.show == 'hold') res = c(res[!figs], res[figs]) # move to the end
157
- # figs = find_recordedplot(res)
158
- # if (length(figs) && sum(figs) > 1) {
159
- # if (keep %in% c('first', 'last')) {
160
- # res = res[-(if (keep == 'last') head else tail)(which(figs), -1L)]
161
- # } else {
162
- # # keep only selected
163
- # if (keep == 'index') res = res[which(figs)[keep.idx]]
164
- # # merge low-level plotting changes
165
- # if (keep == 'high') res = merge_low_plot(res, figs)
166
- # }
167
- # }
168
- # }
169
- #}
170
-
171
- engine_output(options, out = res)
172
-
173
- # out_list = list(source = block_code, text = res, message = "message from Galaaz",
174
- # warning = "Warning from Galaaz", error = "Error from Galaaz",
175
- # value = "Value from Galaaz")
176
- # engine_output(options, out = out_list)
177
- # engine_output(options, block_code, res)
178
- }
179
-
180
- knit_engines$set(ruby = eng_ruby)
181
-
182
- # define the rb engine for inline Ruby code
183
- eng_rb = function(options) {
184
- code = paste0("GalaazUtil.exec_ruby(",
185
- shQuote(options$label),
186
- ")
187
- ");
188
- out = eval.polyglot("ruby", code)
189
- return(out)
190
- }
191
-
192
- knit_engines$set(rb = eng_rb)
193
-
194
- # define the include engine for including Ruby files
195
- # in place
196
-
197
- eng_include = function(options) {
198
- if (is.null(options$relative) | isTRUE(options$relative)) {
199
- req = "require_relative "
200
- } else {
201
- req = "require "
202
- }
203
-
204
- # read the file and store it to output in the code block
205
- code = paste0("GalaazUtil.inline_file(",
206
- shQuote(options$label),
207
- ", ",
208
- shQuote(req),
209
- ")
210
- ");
211
- include = eval.polyglot("ruby", code);
212
-
213
- # require the file
214
- require = paste0(req, shQuote(options$label));
215
- code = paste0("GalaazUtil.exec_ruby(",
216
- shQuote(require),
217
- ")
218
- ");
219
-
220
- out = eval.polyglot("ruby", code);
221
- engine_output(options, include, out)
222
- }
223
-
224
- knit_engines$set(include = eng_include)
225
-
9
+ eval.polyglot("ruby", "require %q(gknit)")
226
10
 
11
+ gknit_file = "#{dir}/#{ARGV[0]}"
227
12
  rmarkdown::render("#{dir}/#{ARGV[0]}")
228
13
  EOF
229
14
 
@@ -0,0 +1,236 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ dir = Dir.pwd
5
+ puts "Knitting #{dir}/#{ARGV[0]}"
6
+
7
+ code = <<EOF
8
+ eval.polyglot("ruby", "$LOAD_PATH.unshift %q(/home/rbotafogo/desenv/galaaz/lib)")
9
+ eval.polyglot("ruby", "require %q(galaaz)")
10
+ eval.polyglot("ruby", "R.install_and_loads %q(knitr)")
11
+ eval.polyglot("ruby", "R.install_and_loads %q(rmarkdown)")
12
+
13
+ # graphics devices in base R, plus those in Cairo, cairoDevice, tikzDevice
14
+ auto_exts = c(
15
+ bmp = "bmp", postscript = "eps", pdf = "pdf", png = "png", svg = "svg",
16
+ jpeg = "jpeg", pictex = "tex", tiff = "tiff", win.metafile = "wmf",
17
+ cairo_pdf = "pdf", cairo_ps = "eps",
18
+
19
+ quartz_pdf = "pdf", quartz_png = "png", quartz_jpeg = "jpeg",
20
+ quartz_tiff = "tiff", quartz_gif = "gif", quartz_psd = "psd",
21
+ quartz_bmp = "bmp",
22
+
23
+ CairoJPEG = "jpeg", CairoPNG = "png", CairoPS = "eps", CairoPDF = "pdf",
24
+ CairoSVG = "svg", CairoTIFF = "tiff",
25
+
26
+ Cairo_pdf = "pdf", Cairo_png = "png", Cairo_ps = "eps", Cairo_svg = "svg",
27
+
28
+ svglite = "svg",
29
+
30
+ tikz = "tex"
31
+ )
32
+
33
+ is_plot_output = function(x) {
34
+ evaluate::is.recordedplot(x) || inherits(x, "knit_image_paths")
35
+ }
36
+
37
+ # find recorded plots in the output of evaluate()
38
+ find_recordedplot = function(x) {
39
+ vapply(x, is_plot_output, logical(1))
40
+ }
41
+
42
+ dev2ext = function(x) {
43
+ res = auto_exts[x]
44
+ if (any(idx <- is.na(res))) {
45
+ for (i in x[idx]) check_dev(i)
46
+ stop2(
47
+ "cannot find appropriate filename extensions for device ", x[idx], "; ",
48
+ "please use chunk option 'fig.ext' (https://yihui.name/knitr/options)"
49
+ )
50
+ }
51
+ unname(res)
52
+ }
53
+
54
+ # filter the dev.args option
55
+ get_dargs = function(dargs, dev) {
56
+ if (length(dargs) == 0) return()
57
+ if (is.list(dargs) && all(sapply(dargs, is.list))) {
58
+ # dev.args is list(dev1 = list(arg1 = val1, ...), dev2 = list(arg2, ...))
59
+ dargs = dargs[[dev]]
60
+ }
61
+ dargs
62
+ }
63
+
64
+ # open a device for a chunk; depending on the option global.device, may or may
65
+ # not need to close the device on exit
66
+ chunk_device = function(
67
+ width, height, record = TRUE, dev, dev.args, dpi, options, tmp = tempfile()
68
+ ) {
69
+ dev_new = function() {
70
+ # actually I should adjust the recording device according to dev, but here I
71
+ # have only considered the png and tikz devices (because the measurement
72
+ # results can be very different especially with the latter, see #1066), and
73
+ # also the cairo_pdf device (#1235)
74
+ if (identical(dev, "png")) {
75
+ do.call(grDevices::png, c(list(
76
+ filename = tmp, width = width, height = height, units = "in", res = dpi
77
+ ), get_dargs(dev.args, "png")))
78
+ } else if (identical(dev, "tikz")) {
79
+ dargs = c(list(
80
+ file = tmp, width = width, height = height
81
+ ), get_dargs(dev.args, "tikz"))
82
+ dargs$sanitize = options$sanitize; dargs$standAlone = options$external
83
+ if (is.null(dargs$verbose)) dargs$verbose = FALSE
84
+ do.call(tikz_dev, dargs)
85
+ } else if (identical(dev, "cairo_pdf")) {
86
+ do.call(grDevices::cairo_pdf, c(list(
87
+ filename = tmp, width = width, height = height
88
+ ), get_dargs(dev.args, "cairo_pdf")))
89
+ } else if (identical(getOption("device"), "pdf_null")) {
90
+ if (!is.null(dev.args)) {
91
+ dev.args = get_dargs(dev.args, "pdf")
92
+ dev.args = dev.args[intersect(names(dev.args), c("pointsize", "bg"))]
93
+ }
94
+ do.call(pdf_null, c(list(width = width, height = height), dev.args))
95
+ } else dev.new(width = width, height = height)
96
+ }
97
+ if (!opts_knit$get("global.device")) {
98
+ dev_new()
99
+ dev.control(displaylist = if (record) "enable" else "inhibit") # enable recording
100
+ # if returns TRUE, we need to close this device after code is evaluated
101
+ return(TRUE)
102
+ } else if (is.null(dev.list())) {
103
+ # want to use a global device but not open yet
104
+ dev_new()
105
+ dev.control("enable")
106
+ }
107
+ FALSE
108
+ }
109
+
110
+ showtext = function(show) if (isTRUE(show)) showtext::showtext_begin()
111
+
112
+ #########################################################################################
113
+
114
+ # define the ruby engine for processing Ruby chunks in
115
+ # rmarkdown
116
+ eng_ruby = function(options) {
117
+ block_code = paste(options$code, collapse = "\\n");
118
+ code = paste0("GalaazUtil.exec_ruby_tor(",
119
+ shQuote(block_code),
120
+ ")
121
+ ");
122
+
123
+ keep = options$fig.keep
124
+ keep.idx = NULL
125
+ if (is.numeric(keep)) {
126
+ keep.idx = keep
127
+ keep = "index"
128
+ }
129
+
130
+ tmp.fig = tempfile(); on.exit(unlink(tmp.fig), add = TRUE)
131
+
132
+ # open a device to record plots
133
+ if (chunk_device(options$fig.width[1L], options$fig.height[1L], keep != "none",
134
+ options$dev, options$dev.args, options$dpi, options, tmp.fig)) {
135
+ # preserve par() settings from the last code chunk
136
+ if (keep.pars <- opts_knit$get("global.par"))
137
+ par2(opts_knit$get("global.pars"))
138
+ showtext(options$fig.showtext) # showtext support
139
+ dv = dev.cur()
140
+ on.exit({
141
+ if (keep.pars) opts_knit$set(global.pars = par(no.readonly = TRUE))
142
+ dev.off(dv)
143
+ }, add = TRUE)
144
+ }
145
+
146
+ # guess plot file type if it is NULL
147
+ # if (keep != "none" && is.null(options$fig.ext))
148
+ # options$fig.ext = dev2ext(options$dev)
149
+
150
+ res = eval.polyglot("ruby", code);
151
+
152
+ # rearrange locations of figures
153
+ # figs = find_recordedplot(res)
154
+ # if (length(figs) && any(figs)) {
155
+ # if (keep == 'none') {
156
+ # res = res[!figs] # remove all
157
+ # } else {
158
+ # if (options$fig.show == 'hold') res = c(res[!figs], res[figs]) # move to the end
159
+ # figs = find_recordedplot(res)
160
+ # if (length(figs) && sum(figs) > 1) {
161
+ # if (keep %in% c('first', 'last')) {
162
+ # res = res[-(if (keep == 'last') head else tail)(which(figs), -1L)]
163
+ # } else {
164
+ # # keep only selected
165
+ # if (keep == 'index') res = res[which(figs)[keep.idx]]
166
+ # # merge low-level plotting changes
167
+ # if (keep == 'high') res = merge_low_plot(res, figs)
168
+ # }
169
+ # }
170
+ # }
171
+ #}
172
+
173
+ engine_output(options, out = res)
174
+
175
+ # out_list = list(source = block_code, text = res, message = "message from Galaaz",
176
+ # warning = "Warning from Galaaz", error = "Error from Galaaz",
177
+ # value = "Value from Galaaz")
178
+ # engine_output(options, out = out_list)
179
+ # engine_output(options, block_code, res)
180
+ }
181
+
182
+ knit_engines$set(ruby = eng_ruby)
183
+
184
+ #########################################################################################
185
+
186
+ # define the rb engine for inline Ruby code
187
+ eng_rb = function(options) {
188
+ code = paste0("GalaazUtil.exec_ruby(",
189
+ shQuote(options$label),
190
+ ")
191
+ ");
192
+ out = eval.polyglot("ruby", code)
193
+ return(out)
194
+ }
195
+
196
+ knit_engines$set(rb = eng_rb)
197
+
198
+ #########################################################################################
199
+
200
+ # define the include engine for including Ruby files
201
+ # in place
202
+
203
+ eng_include = function(options) {
204
+ if (is.null(options$relative) | isTRUE(options$relative)) {
205
+ req = "require_relative "
206
+ } else {
207
+ req = "require "
208
+ }
209
+
210
+ # read the file and store it to output in the code block
211
+ code = paste0("GalaazUtil.inline_file(",
212
+ shQuote(options$label),
213
+ ", ",
214
+ shQuote(req),
215
+ ")
216
+ ");
217
+ include = eval.polyglot("ruby", code);
218
+
219
+ # require the file
220
+ require = paste0(req, shQuote(options$label));
221
+ code = paste0("GalaazUtil.exec_ruby(",
222
+ shQuote(require),
223
+ ")
224
+ ");
225
+
226
+ out = eval.polyglot("ruby", code);
227
+ engine_output(options, include, out)
228
+ }
229
+
230
+ knit_engines$set(include = eng_include)
231
+
232
+
233
+ rmarkdown::render("#{dir}/#{ARGV[0]}")
234
+ EOF
235
+
236
+ exec "Rscript --jvm --polyglot -e '#{code}'"