galaaz 0.4.9 → 0.4.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +798 -285
  3. data/blogs/galaaz_ggplot/galaaz_ggplot.Rmd +3 -12
  4. data/blogs/galaaz_ggplot/galaaz_ggplot.aux +5 -7
  5. data/blogs/galaaz_ggplot/galaaz_ggplot.html +69 -29
  6. data/blogs/galaaz_ggplot/galaaz_ggplot.pdf +0 -0
  7. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/midwest_rb.png +0 -0
  8. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/scatter_plot_rb.png +0 -0
  9. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-latex/midwest_rb.pdf +0 -0
  10. data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-latex/scatter_plot_rb.pdf +0 -0
  11. data/blogs/galaaz_ggplot/midwest.Rmd +1 -9
  12. data/blogs/gknit/gknit.Rmd +37 -40
  13. data/blogs/gknit/gknit.html +32 -30
  14. data/blogs/gknit/gknit.md +36 -37
  15. data/blogs/gknit/gknit.pdf +0 -0
  16. data/blogs/gknit/gknit.tex +35 -37
  17. data/blogs/manual/manual.Rmd +548 -125
  18. data/blogs/manual/manual.html +509 -286
  19. data/blogs/manual/manual.md +798 -285
  20. data/blogs/manual/manual.pdf +0 -0
  21. data/blogs/manual/manual.tex +2816 -0
  22. data/blogs/manual/manual_files/figure-latex/diverging_bar.pdf +0 -0
  23. data/blogs/nse_dplyr/nse_dplyr.Rmd +240 -74
  24. data/blogs/nse_dplyr/nse_dplyr.html +191 -87
  25. data/blogs/nse_dplyr/nse_dplyr.md +361 -107
  26. data/blogs/nse_dplyr/nse_dplyr.pdf +0 -0
  27. data/blogs/nse_dplyr/nse_dplyr.tex +1373 -0
  28. data/blogs/ruby_plot/ruby_plot.Rmd +61 -81
  29. data/blogs/ruby_plot/ruby_plot.html +54 -57
  30. data/blogs/ruby_plot/ruby_plot.md +48 -67
  31. data/blogs/ruby_plot/ruby_plot.pdf +0 -0
  32. data/blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.png +0 -0
  33. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_delivery.png +0 -0
  34. data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_dose.png +0 -0
  35. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color.png +0 -0
  36. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color2.png +0 -0
  37. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_jitter.png +0 -0
  38. data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_points.png +0 -0
  39. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_box_plot.png +0 -0
  40. data/blogs/ruby_plot/ruby_plot_files/figure-html/final_violin_plot.png +0 -0
  41. data/blogs/ruby_plot/ruby_plot_files/figure-html/violin_with_jitter.png +0 -0
  42. data/blogs/ruby_plot/ruby_plot_files/figure-latex/dose_len.png +0 -0
  43. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_delivery.png +0 -0
  44. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_dose.png +0 -0
  45. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color.png +0 -0
  46. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color2.png +0 -0
  47. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_decorations.png +0 -0
  48. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_jitter.png +0 -0
  49. data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_points.png +0 -0
  50. data/blogs/ruby_plot/ruby_plot_files/figure-latex/final_box_plot.png +0 -0
  51. data/blogs/ruby_plot/ruby_plot_files/figure-latex/final_violin_plot.png +0 -0
  52. data/blogs/ruby_plot/ruby_plot_files/figure-latex/violin_with_jitter.png +0 -0
  53. data/lib/R_interface/rdata_frame.rb +0 -12
  54. data/lib/R_interface/robject.rb +14 -14
  55. data/lib/R_interface/ruby_extensions.rb +3 -31
  56. data/lib/R_interface/rvector.rb +0 -12
  57. data/lib/gknit/knitr_engine.rb +5 -3
  58. data/lib/util/exec_ruby.rb +22 -61
  59. data/specs/tmp.rb +26 -12
  60. data/version.rb +1 -1
  61. metadata +22 -17
  62. data/bin/gknit_old_r +0 -236
  63. data/blogs/dev/dev.Rmd +0 -23
  64. data/blogs/dev/dev.md +0 -58
  65. data/blogs/dev/dev2.Rmd +0 -65
  66. data/blogs/dev/model.rb +0 -41
  67. data/blogs/dplyr/dplyr.Rmd +0 -29
  68. data/blogs/dplyr/dplyr.html +0 -433
  69. data/blogs/dplyr/dplyr.md +0 -58
  70. data/blogs/dplyr/dplyr.rb +0 -63
  71. data/blogs/galaaz_ggplot/galaaz_ggplot.log +0 -640
  72. data/blogs/galaaz_ggplot/galaaz_ggplot.md +0 -431
  73. data/blogs/galaaz_ggplot/galaaz_ggplot.tex +0 -481
  74. data/blogs/galaaz_ggplot/midwest.png +0 -0
  75. data/blogs/galaaz_ggplot/scatter_plot.png +0 -0
  76. data/blogs/ruby_plot/ruby_plot.tex +0 -1077
@@ -115,18 +115,6 @@ module R
115
115
  puts "comparison called"
116
116
  end
117
117
 
118
- #=begin
119
- #--------------------------------------------------------------------------------------
120
- # @TODO Need to understand why to_ary is being called here and what the effect is of
121
- # returning the empty array. For now, doing this returns better error messages in
122
- # rspec
123
- #--------------------------------------------------------------------------------------
124
-
125
- def to_ary
126
- []
127
- end
128
- #=end
129
-
130
118
  end
131
119
 
132
120
  end
@@ -652,11 +652,13 @@ class KnitrEngine
652
652
  # function engine_output will format whatever is in out inside a white box
653
653
  out = R.engine_output(@options, out: res) if @echo
654
654
 
655
- # ouputs the data in RubyChunk '@@outputs' variable. Everything that should
655
+ # ouputs the data in RubyChunk '@outputs' variable. Everything that should
656
656
  # be processed by 'pandoc' and not appear in the output block from
657
657
  # engine_output, should be outputed with the 'outputs' function and will be
658
- # stored in the @@outputs variable
659
- out = R.c(out, RubyChunk.get_outputs)
658
+ # stored in the @outputs variable
659
+ # out = R.c(out, RubyChunk.get_outputs)
660
+ out = R.c(out, RChunk.out_list)
661
+ RChunk.reset_outputs
660
662
 
661
663
  # @TODO: allow capturing many plots in the block. For now, only the last
662
664
  # plot will be captured. Not a very serious problem for now.
@@ -22,77 +22,37 @@
22
22
  ##########################################################################################
23
23
 
24
24
  require 'stringio'
25
- =begin
25
+
26
26
  #----------------------------------------------------------------------------------------
27
- # Path StringIO puts... Already opened an issue in RC12
27
+ # Class RC is used only as a context for all ruby chunks in the rmarkdown file.
28
+ # This allows for chunks to access local variables defined in other chunks.
28
29
  #----------------------------------------------------------------------------------------
29
30
 
30
- class StringIO
31
-
32
- def puts(*args)
31
+ class RC
33
32
 
34
- print "========================\n"
35
- print "Inside puts\n"
36
- print "========================\n"
37
-
38
- if args.empty?
39
- write(DEFAULT_RECORD_SEPARATOR)
40
- else
41
- args.each do |arg|
42
-
43
- # method to_s is not being called when the output is diverted
44
- # need to fix it
45
- if (arg.is_a? R::Object)
46
- arg = arg.to_s
47
- end
48
-
49
- if arg.nil?
50
- line = ''
51
- elsif Thread.guarding? arg
52
- line = '[...]'
53
- else
54
- begin
55
- arg = Truffle::Type.coerce_to(arg, Array, :to_ary)
56
- Thread.recursion_guard arg do
57
- arg.each { |a| puts a }
58
- end
59
- next
60
- rescue
61
- line = arg.to_s
62
- end
63
- end
64
-
65
- write(line)
66
- write(DEFAULT_RECORD_SEPARATOR) # unless line[-1] == ?\n
67
- end
68
- end
33
+ attr_reader :out_list
69
34
 
70
- nil
35
+ def initialize
36
+ @out_list = R.list
71
37
  end
72
38
 
73
- end
74
- =end
75
- #----------------------------------------------------------------------------------------
76
- # Class RubyChunk is used only as a context for all ruby chunks in the rmarkdown file.
77
- # This allows for chunks to access instance_variables (@)
78
- #----------------------------------------------------------------------------------------
79
-
80
- class RubyChunk
81
-
82
- def self.init
83
- @@outputs = R.list
39
+ def outputs(obj)
40
+ @out_list = R.c(@out_list, obj)
84
41
  end
85
-
86
- def self.get_outputs
87
- @@outputs
42
+
43
+ def reset_outputs
44
+ @out_list = nil
88
45
  end
89
-
90
- def self.outputs(obj)
91
- @@outputs = R.c(@@outputs, obj)
46
+
47
+ def get_binding
48
+ binding
92
49
  end
93
50
 
94
51
  end
95
52
 
53
+ RChunk = RC.new
54
+ RCbinding = RChunk.get_binding
55
+
96
56
  #----------------------------------------------------------------------------------------
97
57
  #
98
58
  #----------------------------------------------------------------------------------------
@@ -117,11 +77,11 @@ module GalaazUtil
117
77
 
118
78
  def self.exec_ruby(options)
119
79
 
120
- RubyChunk.init
80
+ # RubyChunk.init
121
81
 
122
82
  # read the chunk code
123
83
  code = R.paste(options.code, collapse: "\n") >> 0
124
-
84
+
125
85
  # the output should be a list with the proper structure to pass to
126
86
  # function engine_output. We first add the souce code from the block to
127
87
  # the list
@@ -138,7 +98,8 @@ module GalaazUtil
138
98
  # Execute the Ruby code in the scope of class RubyChunk. This is done
139
99
  # so that instance variables created in one chunk can be used again on
140
100
  # another chunk
141
- RubyChunk.instance_eval(code) if (options[["eval"]] >> 0)
101
+ # RubyChunk.instance_eval(code) if (options[["eval"]] >> 0)
102
+ eval(code, RCbinding, __FILE__, __LINE__ + 1) if (options[["eval"]] >> 0)
142
103
 
143
104
  # add the returned value to the list
144
105
  # this should have captured everything in the evaluation code
@@ -22,21 +22,35 @@
22
22
  ##########################################################################################
23
23
 
24
24
  require 'galaaz'
25
- # require 'ggplot'
26
25
 
27
- R.install_and_loads('nycflights13')
28
- R.library('dplyr')
26
+ puts RChunk
27
+ puts RCbinding
28
+
29
+ eval("a = 10", RCbinding)
30
+ eval("puts a", RCbinding)
31
+
32
+ code = <<-EOC
33
+ def test
34
+ puts "hello from test"
35
+ end
36
+ EOC
37
+
38
+ eval(code, RCbinding)
39
+ eval("test", RCbinding)
40
+
41
+
42
+
43
+ =begin
44
+ code6 = <<-EOC
45
+ test
46
+ EOC
47
+
48
+ puts code2
49
+
50
+ eval(code2, RubyChunk.binding)
51
+ =end
29
52
 
30
- @flights = ~:flights
31
- @flights = @flights.
32
- mutate("Diff Chegada": :arr_time - :sched_arr_time,
33
- 'Total Delay': :'Diff Chegada' *
34
- E.if_else(((:arr_time - :sched_arr_time).eq 0), 1, 2),
35
- 'Moeda.x': "R$",
36
- 'Valor Nominal': :'Diff Chegada' *
37
- E.if_else((:'Moeda.x'.eq "R$"), :'Total Delay', 100))
38
53
 
39
- puts @flights.head.as__data__frame
40
54
 
41
55
  ## Renaming columns
42
56
 
data/version.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  $gem_name = "galaaz"
2
- $version="0.4.9"
2
+ $version="0.4.10"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: galaaz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.9
4
+ version: 0.4.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Botafogo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-30 00:00:00.000000000 Z
11
+ date: 2019-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -80,31 +80,21 @@ files:
80
80
  - Rakefile
81
81
  - bin/galaaz
82
82
  - bin/gknit
83
- - bin/gknit_old_r
84
83
  - bin/grun
85
84
  - bin/gstudio
86
85
  - bin/gstudio.rb
87
- - blogs/dev/dev.Rmd
88
- - blogs/dev/dev.md
89
- - blogs/dev/dev2.Rmd
90
- - blogs/dev/model.rb
91
- - blogs/dplyr/dplyr.Rmd
92
- - blogs/dplyr/dplyr.html
93
- - blogs/dplyr/dplyr.md
94
- - blogs/dplyr/dplyr.rb
95
86
  - blogs/galaaz_ggplot/galaaz_ggplot.Rmd
96
87
  - blogs/galaaz_ggplot/galaaz_ggplot.aux
97
88
  - blogs/galaaz_ggplot/galaaz_ggplot.html
98
- - blogs/galaaz_ggplot/galaaz_ggplot.log
99
- - blogs/galaaz_ggplot/galaaz_ggplot.md
100
89
  - blogs/galaaz_ggplot/galaaz_ggplot.out
101
90
  - blogs/galaaz_ggplot/galaaz_ggplot.pdf
102
- - blogs/galaaz_ggplot/galaaz_ggplot.tex
91
+ - blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/midwest_rb.png
92
+ - blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/scatter_plot_rb.png
93
+ - blogs/galaaz_ggplot/galaaz_ggplot_files/figure-latex/midwest_rb.pdf
94
+ - blogs/galaaz_ggplot/galaaz_ggplot_files/figure-latex/scatter_plot_rb.pdf
103
95
  - blogs/galaaz_ggplot/midwest.Rmd
104
96
  - blogs/galaaz_ggplot/midwest.html
105
- - blogs/galaaz_ggplot/midwest.png
106
97
  - blogs/galaaz_ggplot/midwest_external_png
107
- - blogs/galaaz_ggplot/scatter_plot.png
108
98
  - blogs/gknit/gknit.Rmd
109
99
  - blogs/gknit/gknit.html
110
100
  - blogs/gknit/gknit.md
@@ -118,10 +108,15 @@ files:
118
108
  - blogs/manual/manual.Rmd
119
109
  - blogs/manual/manual.html
120
110
  - blogs/manual/manual.md
111
+ - blogs/manual/manual.pdf
112
+ - blogs/manual/manual.tex
121
113
  - blogs/manual/manual_files/figure-html/diverging_bar.png
114
+ - blogs/manual/manual_files/figure-latex/diverging_bar.pdf
122
115
  - blogs/nse_dplyr/nse_dplyr.Rmd
123
116
  - blogs/nse_dplyr/nse_dplyr.html
124
117
  - blogs/nse_dplyr/nse_dplyr.md
118
+ - blogs/nse_dplyr/nse_dplyr.pdf
119
+ - blogs/nse_dplyr/nse_dplyr.tex
125
120
  - blogs/oh_my/not_so.rb
126
121
  - blogs/oh_my/oh_my.Rmd
127
122
  - blogs/oh_my/oh_my.html
@@ -132,7 +127,6 @@ files:
132
127
  - blogs/ruby_plot/ruby_plot.html
133
128
  - blogs/ruby_plot/ruby_plot.md
134
129
  - blogs/ruby_plot/ruby_plot.pdf
135
- - blogs/ruby_plot/ruby_plot.tex
136
130
  - blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.png
137
131
  - blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.svg
138
132
  - blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_delivery.png
@@ -154,6 +148,17 @@ files:
154
148
  - blogs/ruby_plot/ruby_plot_files/figure-html/final_violin_plot.svg
155
149
  - blogs/ruby_plot/ruby_plot_files/figure-html/violin_with_jitter.png
156
150
  - blogs/ruby_plot/ruby_plot_files/figure-html/violin_with_jitter.svg
151
+ - blogs/ruby_plot/ruby_plot_files/figure-latex/dose_len.png
152
+ - blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_delivery.png
153
+ - blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_dose.png
154
+ - blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color.png
155
+ - blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color2.png
156
+ - blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_decorations.png
157
+ - blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_jitter.png
158
+ - blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_points.png
159
+ - blogs/ruby_plot/ruby_plot_files/figure-latex/final_box_plot.png
160
+ - blogs/ruby_plot/ruby_plot_files/figure-latex/final_violin_plot.png
161
+ - blogs/ruby_plot/ruby_plot_files/figure-latex/violin_with_jitter.png
157
162
  - examples/50Plots_MasterList/Images/midwest-scatterplot.PNG
158
163
  - examples/50Plots_MasterList/ScatterPlot.rb
159
164
  - examples/50Plots_MasterList/scatter_plot.rb
@@ -1,236 +0,0 @@
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}'"