Class: R::Device
- Inherits:
-
Object
- Object
- R::Device
- Defined in:
- lib/R_interface/rdevice.rb
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #ensure_plot_helpers! ⇒ Object
-
#initialize(type, width: 480, height: 480, dpi: 72, record: true, &block) ⇒ Device
constructor
A new instance of Device.
- #open ⇒ Object
- #plot_snapshot ⇒ Object
- #save_plot(plot, name, dev_type, width, height, ext, dpi) ⇒ Object
Constructor Details
#initialize(type, width: 480, height: 480, dpi: 72, record: true, &block) ⇒ Device
Returns a new instance of Device.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/R_interface/rdevice.rb', line 52 def initialize(type, width: 480, height: 480, dpi: 72, record: true, &block) @type = type.to_s @width = width @height = height @dpi = dpi @record = record @block = block @dev_path = nil @opened = false @last_save_target = nil end |
Class Method Details
.new(type, width: 480, height: 480, dpi: 72, record: true, &block) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/R_interface/rdevice.rb', line 114 def self.new(type, width: 480, height: 480, dpi: 72, record: true, &block) inst = allocate inst.send(:initialize, type, width: width, height: height, dpi: dpi, record: record, &block) if block_given? inst.open block.call inst.close end inst end |
Instance Method Details
#close ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/R_interface/rdevice.rb', line 80 def close return unless @opened R.dev__off @opened = false if @last_save_target && @dev_path && File.exist?(@dev_path) FileUtils.cp(@dev_path, @last_save_target) end end |
#ensure_plot_helpers! ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/R_interface/rdevice.rb', line 9 def ensure_plot_helpers! R.bridge.eval_r(<<~RCODE) if (!exists("evaluate_plot_snapshot", envir = .GlobalEnv, inherits = FALSE)) { .GlobalEnv$evaluate_plot_snapshot <- function() { grDevices::recordPlot() } } if (!exists("galaaz_save_plot", envir = .GlobalEnv, inherits = FALSE)) { .GlobalEnv$galaaz_save_plot <- function(path, dev_type, width, height, dpi) { if (dev_type == "png") { do.call(grDevices::dev.copy, list( device = grDevices::png, filename = path, width = width, height = height, units = "in", res = dpi )) } else if (dev_type == "svg") { do.call(grDevices::dev.copy, list( device = grDevices::svg, filename = path, width = width, height = height )) } else if (dev_type == "pdf") { do.call(grDevices::dev.copy, list( device = grDevices::pdf, file = path, width = width, height = height )) } else { stop(paste("unsupported dev_type:", dev_type)) } grDevices::dev.off() path } } invisible(NULL) RCODE end |
#open ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/R_interface/rdevice.rb', line 64 def open return if @opened dir = File.join(Dir.tmpdir, "galaaz_device_#{Process.pid}") FileUtils.mkdir_p(dir) unless Dir.exist?(dir) @dev_path = File.join(dir, "dev_#{object_id}.#{@type == 'svg' ? 'svg' : 'png'}") w_px = (@width * @dpi).to_i h_px = (@height * @dpi).to_i if @type == 'svg' R.bridge.eval_r("svg('#{@dev_path.gsub("'", "\\\\'")}', width = #{@width}, height = #{@height})") else R.bridge.eval_r("png('#{@dev_path.gsub("'", "\\\\'")}', width = #{w_px}, height = #{h_px}, res = #{@dpi})") end @opened = true yield if block_given? end |
#plot_snapshot ⇒ Object
89 90 91 92 |
# File 'lib/R_interface/rdevice.rb', line 89 def plot_snapshot ensure_plot_helpers! R.evaluate_plot_snapshot end |
#save_plot(plot, name, dev_type, width, height, ext, dpi) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/R_interface/rdevice.rb', line 94 def save_plot(plot, name, dev_type, width, height, ext, dpi) out_name = "#{name}.#{ext}" target = out_name.start_with?('/') ? out_name : File.join(Dir.pwd, out_name) if @opened # Persist current page and reopen the device to keep plotting. R.dev__off @opened = false if @dev_path && File.exist?(@dev_path) && File.size(@dev_path).to_i > 0 FileUtils.cp(@dev_path, target) end open @last_save_target = nil elsif !@opened && @dev_path && File.exist?(@dev_path) && File.size(@dev_path).to_i > 0 @last_save_target = target FileUtils.cp(@dev_path, target) end out_name end |