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
@@ -20,12 +20,6 @@
20
20
  ##########################################################################################
21
21
 
22
22
  require 'galaaz'
23
- require 'ggplot'
24
-
25
- # install.packages('quantreg')
26
- # install.packages('hexbin')
27
- # Library 'quantreg' is needed in misc
28
- R.install_and_loads('quantreg', 'hexbin')
29
23
 
30
24
  # Package Hmisc does not yet install on graalvm because of
31
25
  # problems with package data.table
@@ -24,7 +24,7 @@
24
24
  require 'galaaz'
25
25
  require 'ggplot'
26
26
 
27
- Polyglot.eval("R", "library('hexbin')")
27
+ R.install_and_loads('hexbin')
28
28
 
29
29
  diamonds = ~:diamonds
30
30
 
@@ -24,7 +24,7 @@
24
24
  require 'galaaz'
25
25
  require 'ggplot'
26
26
 
27
- Polyglot.eval("R", "library('quantreg')")
27
+ R.install_and_loads('quantreg')
28
28
 
29
29
  R.awt
30
30
 
@@ -30,8 +30,8 @@ df = R.data__frame(dose: R.c("D0.5", "D1", "D2"),
30
30
  puts df.head
31
31
 
32
32
  df2 = R.data__frame(supp: R.rep(R.c("VC", "OJ"), each: 3),
33
- dose: R.rep(R.c("D0.5", "D1", "D2"), 2),
34
- len: R.c(6.8, 15, 33, 4.2, 10, 29.5))
33
+ dose: R.rep(R.c("D0.5", "D1", "D2"), 2),
34
+ len: R.c(6.8, 15, 33, 4.2, 10, 29.5))
35
35
  puts df2.head
36
36
 
37
37
  f = df.ggplot(E.aes(x: :dose, y: :len))
@@ -32,7 +32,6 @@ print diamonds.ggplot(E.aes(:cut, :color)) +
32
32
  R.geom_jitter(E.aes(color: :cut), size: 0.5)
33
33
 
34
34
  sleep(2)
35
- R.grid__newpage
36
35
 
37
36
  # a = gets.chomp
38
37
 
@@ -0,0 +1,62 @@
1
+ # define the ruby engine for processing Ruby chunks in
2
+ # rmarkdown
3
+ eng_ruby = function(options) {
4
+ block_code = paste(options$code, collapse = "\\n");
5
+ code = paste0("GalaazUtil.exec_ruby(",
6
+ sQuote(block_code),
7
+ ")"
8
+ );
9
+
10
+ keep = options$fig.keep
11
+ keep.idx = NULL
12
+ if (is.numeric(keep)) {
13
+ keep.idx = keep
14
+ keep = "index"
15
+ }
16
+
17
+ tmp.fig = tempfile(); on.exit(unlink(tmp.fig), add = TRUE)
18
+
19
+ # open a device to record plots
20
+ if (knitr:::chunk_device(options$fig.width[1L], options$fig.height[1L], keep != "none",
21
+ options$dev, options$dev.args, options$dpi, options, tmp.fig)) {
22
+ # preserve par() settings from the last code chunk
23
+ if (keep.pars <- opts_knit$get("global.par"))
24
+ par2(opts_knit$get("global.pars"))
25
+ knitr:::showtext(options$fig.showtext) # showtext support
26
+ dv = dev.cur()
27
+ on.exit({
28
+ if (keep.pars) opts_knit$set(global.pars = par(no.readonly = TRUE))
29
+ dev.off(dv)
30
+ }, add = TRUE)
31
+ }
32
+
33
+ # guess plot file type if it is NULL
34
+ if (keep != "none" && is.null(options$fig.ext))
35
+ options$fig.ext = knitr:::dev2ext(options$dev)
36
+
37
+ res = eval.polyglot("ruby", code)
38
+
39
+ # rearrange locations of figures
40
+ figs = find_recordedplot(res)
41
+ if (length(figs) && any(figs)) {
42
+ if (keep == 'none') {
43
+ res = res[!figs] # remove all
44
+ } else {
45
+ if (options$fig.show == 'hold') res = c(res[!figs], res[figs]) # move to the end
46
+ figs = find_recordedplot(res)
47
+ if (length(figs) && sum(figs) > 1) {
48
+ if (keep %in% c('first', 'last')) {
49
+ res = res[-(if (keep == 'last') head else tail)(which(figs), -1L)]
50
+ } else {
51
+ # keep only selected
52
+ if (keep == 'index') res = res[which(figs)[keep.idx]]
53
+ # merge low-level plotting changes
54
+ if (keep == 'high') res = merge_low_plot(res, figs)
55
+ }
56
+ }
57
+ }
58
+ }
59
+ engine_output(options, block_code, res)
60
+ }
61
+
62
+ knit_engines$set(ruby = eng_ruby)
@@ -0,0 +1,63 @@
1
+ # define the ruby engine for processing Ruby chunks in
2
+ # rmarkdown
3
+ eng_ruby = function(options) {
4
+ block_code = paste(options$code, collapse = "\\n");
5
+ code = paste0("GalaazUtil.exec_ruby(",
6
+ shQuote(block_code),
7
+ ")
8
+ ");
9
+
10
+ # png(paste0("gknit_files/figure-html/", options$label, ".png"));
11
+
12
+ keep = options$fig.keep
13
+ keep.idx = NULL
14
+ if (is.numeric(keep)) {
15
+ keep.idx = keep
16
+ keep = "index"
17
+ }
18
+
19
+ tmp.fig = tempfile(); on.exit(unlink(tmp.fig), add = TRUE)
20
+
21
+ # open a device to record plots
22
+ if (chunk_device(options$fig.width[1L], options$fig.height[1L], keep != "none",
23
+ options$dev, options$dev.args, options$dpi, options, tmp.fig)) {
24
+ # preserve par() settings from the last code chunk
25
+ if (keep.pars <- opts_knit$get("global.par"))
26
+ par2(opts_knit$get("global.pars"))
27
+ showtext(options$fig.showtext) # showtext support
28
+ dv = dev.cur()
29
+ on.exit({
30
+ if (keep.pars) opts_knit$set(global.pars = par(no.readonly = TRUE))
31
+ dev.off(dv)
32
+ }, add = TRUE)
33
+ }
34
+
35
+ # guess plot file type if it is NULL
36
+ if (keep != "none" && is.null(options$fig.ext))
37
+ options$fig.ext = dev2ext(options$dev)
38
+
39
+ # out = eval.polyglot("ruby", code);
40
+ res = eval.polyglot("ruby", code);
41
+
42
+ # rearrange locations of figures
43
+ figs = find_recordedplot(res)
44
+ if (length(figs) && any(figs)) {
45
+ if (keep == 'none') {
46
+ res = res[!figs] # remove all
47
+ } else {
48
+ if (options$fig.show == 'hold') res = c(res[!figs], res[figs]) # move to the end
49
+ figs = find_recordedplot(res)
50
+ if (length(figs) && sum(figs) > 1) {
51
+ if (keep %in% c('first', 'last')) {
52
+ res = res[-(if (keep == 'last') head else tail)(which(figs), -1L)]
53
+ } else {
54
+ # keep only selected
55
+ if (keep == 'index') res = res[which(figs)[keep.idx]]
56
+ # merge low-level plotting changes
57
+ if (keep == 'high') res = merge_low_plot(res, figs)
58
+ }
59
+ }
60
+ }
61
+ }
62
+ engine_output(options, block_code, res)
63
+ }
@@ -0,0 +1,23 @@
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
+
File without changes
@@ -29,7 +29,8 @@ module R
29
29
  RCONSTANTS = ["LETTERS", "letters", "month.abb", "month.name", "pi"]
30
30
 
31
31
  #----------------------------------------------------------------------------------------
32
- #
32
+ # Executes a missing method. If a block is given, then the method needs to be executed
33
+ # in the scope of the block. @bug: Not ready yeat
33
34
  #----------------------------------------------------------------------------------------
34
35
 
35
36
  def self.method_missing(symbol, *args, &block)
@@ -117,3 +118,11 @@ require_relative 'ruby_extensions'
117
118
  # Class to allow R calling back into Ruby
118
119
  require_relative 'ruby_callback'
119
120
 
121
+ # R packages and functions needed
122
+ # Requires ggplot for plotting graphics
123
+ # require 'ggplot'
124
+ # require 'knitr'
125
+
126
+ # Methods to capture a plot sent to a device
127
+ require_relative 'rdevices'
128
+
File without changes
@@ -30,19 +30,35 @@ module R
30
30
  #--------------------------------------------------------------------------------------
31
31
 
32
32
  def self.capture
33
-
34
33
  Polyglot.eval("R", <<-R)
35
34
  function(obj, ...) {
36
- sink(tt <- textConnection("results","w"), split=FALSE);
35
+ sink(tt <- textConnection("results","w"), split=FALSE, type = c("output", "message"));
37
36
  print(obj, ...);
38
37
  sink();
39
38
  close(tt);
40
39
  results
41
40
  }
42
41
  R
43
-
44
- end
45
-
42
+ end
43
+
44
+ def self.start_capture
45
+ Polyglot.eval("R", <<-R)
46
+ function(cap_variable) {
47
+ sink(con <- textConnection(cap_variable,"w"), split=FALSE, type = c("output"));
48
+ con
49
+ }
50
+ R
51
+ end
52
+
53
+ def self.stop_capture
54
+ Polyglot.eval("R", <<-R)
55
+ function(con) {
56
+ sink();
57
+ close(con);
58
+ }
59
+ R
60
+ end
61
+
46
62
  #--------------------------------------------------------------------------------------
47
63
  # multi-dimensional indexing
48
64
  #--------------------------------------------------------------------------------------
@@ -60,6 +60,7 @@ module R
60
60
  #--------------------------------------------------------------------------------------
61
61
 
62
62
  module FormulaBinOp
63
+
63
64
  #--------------------------------------------------------------------------------------
64
65
  #
65
66
  #--------------------------------------------------------------------------------------
@@ -93,6 +94,10 @@ module R
93
94
 
94
95
  end
95
96
 
97
+ #--------------------------------------------------------------------------------------
98
+ #
99
+ #--------------------------------------------------------------------------------------
100
+
96
101
  end
97
102
 
98
103
  #--------------------------------------------------------------------------------------
@@ -220,7 +225,7 @@ module R
220
225
  def coerce(numeric)
221
226
  [R.c(numeric), self]
222
227
  end
223
-
228
+
224
229
  end
225
230
 
226
231
  end
@@ -0,0 +1,38 @@
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
+ module R
25
+
26
+ class Closure < Object
27
+
28
+ #----------------------------------------------------------------------------------------
29
+ #
30
+ #----------------------------------------------------------------------------------------
31
+
32
+ def call(*args)
33
+ R::Support.exec_function(@r_interop, *args)
34
+ end
35
+
36
+ end
37
+
38
+ end
File without changes
@@ -0,0 +1,31 @@
1
+ #" Capture snapshot of current device.
2
+ #"
3
+ #" There's currently no way to capture when a graphics device changes,
4
+ #" except to check its contents after the evaluation of every expression.
5
+ #" This means that only the last plot of a series will be captured.
6
+ #"
7
+ #" @return \code{NULL} if plot is blank or unchanged, otherwise the output of
8
+ #" \code{\link[grDevices]{recordPlot}}.
9
+ _plot_snapshot = function() {
10
+ evaluate:::plot_snapshot()
11
+ }
12
+
13
+ # Function accessible from Galaaz to call non-exported knitr:::chunk_device function
14
+ _ck_dv = function(width, height, record = TRUE, dev, dev.args, dpi, options,
15
+ tmp = tempfile()) {
16
+ knitr:::chunk_device(width, height, record, dev, dev.args, dpi, options, tmp)
17
+ }
18
+
19
+ # Function to guess the extension name of a file based on the device type
20
+ knitr_dev2ext = function(x) {
21
+ knitr:::dev2ext(x)
22
+ }
23
+
24
+ knitr_save_plot = function(plot, name, dev, width, height, ext, dpi, options) {
25
+ print(options)
26
+ knitr:::save_plot(plot, name, dev, width, height, ext, dpi, options)
27
+ }
28
+
29
+ knitr_opts_chunk = function() {
30
+ knitr:::opts_chunk
31
+ }
@@ -0,0 +1,225 @@
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 'singleton'
25
+
26
+ dir = File.dirname(File.expand_path('.', __FILE__))
27
+ src = "#{dir}/rdevices.R"
28
+
29
+ R.source(src)
30
+
31
+ module R
32
+
33
+ class Device
34
+
35
+ attr_reader :dev_type, :filename, :width, :height, :dpi, :record,
36
+ :args, :options, :tmp_file,
37
+ :dev_number
38
+ attr_accessor :file_dir
39
+
40
+
41
+ #--------------------------------------------------------------------------------------
42
+ #
43
+ #--------------------------------------------------------------------------------------
44
+
45
+ def self.list
46
+ R.dev__list
47
+ end
48
+
49
+ #--------------------------------------------------------------------------------------
50
+ #
51
+ #--------------------------------------------------------------------------------------
52
+
53
+ def initialize(dev_type, filename: nil, file_dir: '.', width: nil, height: nil,
54
+ dpi: nil, record: false, args: nil, options: nil, tmp_file: nil)
55
+
56
+ @dev_type = dev_type
57
+ @filename = filename
58
+ @width = width
59
+ @height = height
60
+ @dpi = dpi
61
+ # TODO: Needs to allow false, but truffle is crashing when false. When it is
62
+ # fixed, just change to @record = record
63
+ @record = true
64
+
65
+ @args = args
66
+ @options = options
67
+ @tmp_file = tmp_file
68
+
69
+ end
70
+
71
+ #--------------------------------------------------------------------------------------
72
+ #
73
+ #--------------------------------------------------------------------------------------
74
+
75
+ def open
76
+
77
+ case @dev_type
78
+
79
+ when 'svg'
80
+ when 'awt'
81
+ R.awt(width: @width, height: @height)
82
+ @record ? R.dev__control(displaylist: 'enable') :
83
+ R.dev__control(displaylist: 'inhibit')
84
+ @dev_number = R.dev__cur
85
+ when 'png'
86
+ R.png(filename: @filename, width: @width, height: @height, units: 'in', res: dpi)
87
+ when 'jpg'
88
+ when 'bmp'
89
+ else
90
+ raise "Device type #{@dev_type} not allowed"
91
+ end
92
+
93
+ end
94
+
95
+ #--------------------------------------------------------------------------------------
96
+ # Opens the device, evaluates the given graphics block expressions and closes the
97
+ # device at the end of execution
98
+ #--------------------------------------------------------------------------------------
99
+
100
+ def eval(&block)
101
+
102
+ if block_given?
103
+ begin
104
+ open
105
+ yield
106
+ ensure
107
+ close
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+ #--------------------------------------------------------------------------------------
114
+ # copies the temporary file to the definitive location
115
+ #--------------------------------------------------------------------------------------
116
+
117
+ def copy
118
+ file = "#{@file_dir}/#{@file_name}.#{ext}"
119
+ puts file
120
+ FileUtils.cp(@tmp_file << 0, file)
121
+ end
122
+
123
+ #--------------------------------------------------------------------------------------
124
+ #
125
+ #--------------------------------------------------------------------------------------
126
+
127
+ def close
128
+ R.dev__off(which: @dev_number)
129
+ # copy if @record
130
+ end
131
+
132
+ #--------------------------------------------------------------------------------------
133
+ #
134
+ #--------------------------------------------------------------------------------------
135
+
136
+ def newpage
137
+ R.grid__newpage
138
+ end
139
+
140
+ #--------------------------------------------------------------------------------------
141
+ #
142
+ #--------------------------------------------------------------------------------------
143
+
144
+ def plot_snapshot
145
+ R.knitr_plot_snapshot
146
+ end
147
+
148
+ #--------------------------------------------------------------------------------------
149
+ #
150
+ #--------------------------------------------------------------------------------------
151
+
152
+ def ext
153
+ R.knitr_dev2ext(@dev) << 0
154
+ end
155
+
156
+ #--------------------------------------------------------------------------------------
157
+ #
158
+ #--------------------------------------------------------------------------------------
159
+
160
+ def save_plot(plot, name, dev, width, height, ext, dpi, options = R.list())
161
+ R.knitr_save_plot(plot, name, dev, width, height, ext, dpi, options)
162
+ end
163
+
164
+ end
165
+
166
+
167
+ #--------------------------------------------------------------------------------------
168
+ #
169
+ #--------------------------------------------------------------------------------------
170
+
171
+ class Panel < Device
172
+ include Singleton
173
+
174
+ #--------------------------------------------------------------------------------------
175
+ #
176
+ #--------------------------------------------------------------------------------------
177
+
178
+ def initialize
179
+ super('awg', width: nil, height: nil, dpi: nil, record: false, args: nil,
180
+ options: nil)
181
+ end
182
+
183
+ def exec(&block)
184
+ yield
185
+ end
186
+
187
+ end
188
+
189
+ end
190
+
191
+ =begin
192
+ #--------------------------------------------------------------------------------------
193
+ #
194
+ #--------------------------------------------------------------------------------------
195
+
196
+ def open2
197
+
198
+ case @dev
199
+ when 'svg'
200
+ R.svg(@tmp_file)
201
+ when 'awt'
202
+ @record = false
203
+ R.awt
204
+ when 'png'
205
+ # for 'png' @record needs to be true, otherwise the device is not
206
+ # opened. Don't know why, yet!
207
+ @record = true
208
+ # opens a device for plot recording.
209
+ if (R._ck_dv(@width, @height, @dev, @args, @dpi, tmp: @tmp_file,
210
+ record: @record) << 0)
211
+ @dv = R.dev__cur
212
+ else
213
+ raise "Problem opening the device"
214
+ end
215
+ else
216
+ if (R._ck_dv(@width, @height, @dev, @args, @dpi, tmp: @tmp_file,
217
+ record: @record) << 0)
218
+ @dv = R.dev__cur
219
+ else
220
+ raise "Problem opening the device"
221
+ end
222
+ end
223
+
224
+ end
225
+ =end