Module: R::GIF

Defined in:
lib/rbbt/util/R/plot.rb

Class Method Summary collapse

Class Method Details

.eog(data, frames = nil, script = nil, width = nil, height = nil, options = {}) ⇒ Object



273
274
275
276
277
278
# File 'lib/rbbt/util/R/plot.rb', line 273

def self.eog(data, frames = nil, script = nil, width = nil, height = nil, options = {})
  TmpFile.with_file :extension => 'gif' do |filename|
    ggplot(filename, data, frames, script, width, height, options)
    `eog #{ filename }`
  end
end

.eog_plot(data, frames = nil, script = nil, width = nil, height = nil, options = {}) ⇒ Object



280
281
282
283
284
285
# File 'lib/rbbt/util/R/plot.rb', line 280

def self.eog_plot(data, frames = nil, script = nil, width = nil, height = nil, options = {})
  TmpFile.with_file :extension => 'gif' do |filename|
    plot(filename, data, frames, script, width, height, options)
    `eog #{ filename }`
  end
end

.ggplot(filename, data, frames = nil, script = nil, width = nil, height = nil, options = {}) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/rbbt/util/R/plot.rb', line 288

def self.ggplot(filename, data, frames = nil, script = nil, width = nil, height = nil, options = {})
  width ||= 3
  height ||= 3
  values = []

  sources = [:plot, options[:source]].flatten.compact

  field_classes = R.field_classes(data) if field_classes.nil?

  options[:R_open] ||= "colClasses=c('character'," + field_classes * ", " + ')'

  delay = options[:delay]
  delay = 10 if delay.nil?

  frames = data.keys if frames.nil?
  frames = (1..frames).to_a if Integer === frames

  data.R <<-EOF, :plot, options
frames = #{R.ruby2R frames}
for (frame in seq(1, length(frames))){
  frame.value = frames[frame]
  frame.str = sprintf("%06d", frame)
  plot = { #{script} }

  ggsave(paste('#{filename}', frame.str, 'tmp.png', sep="."), plot, width = #{R.ruby2R width}, height = #{R.ruby2R height})
}
data = NULL
  EOF

  CMD.cmd("convert #{filename}.*.tmp.png -set delay #{delay} -loop 0 #{filename} && rm #{filename}.*.tmp.png")
end

.plot(filename, data = nil, frames = nil, script = nil, width = nil, height = nil, options = {}, &block) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/rbbt/util/R/plot.rb', line 320

def self.plot(filename, data = nil, frames = nil, script = nil, width = nil, height = nil, options = {}, &block)
  width ||= 600
  height ||= 600
  values = []

  script ||= ""
  if block_given?
    s = StringIO.new
    class << s
      def method_missing(name, *args)
        name = name.to_s
        if name[-1] == '='
          arg = args.first
          value = if String === arg
                    arg
                  else
                    R.ruby2R arg
                  end
          add("" << name[0..-2] << "=" << value)
        else
          args_strs = []
          args.each do |arg|
            value = if String === arg
                      arg
                    else
                      R.ruby2R arg
                    end
            args_strs << value
          end
          add("" << name << "(" << args_strs * ", " << ")")
        end
      end

      def add(line)
        self.write line << "\n"
      end
    end
    block.call(s)
    s.rewind
    script << "\n" << s.read
  end
  sources = [:plot, options[:source]].flatten.compact
  
  delay = options[:delay]
  delay = 10 if delay.nil?

  frames = data.keys if frames.nil?
  frames = (1..frames).to_a if Integer === frames

  if data
    field_classes = R.field_classes(data) if field_classes.nil?

    options[:R_open] ||= "colClasses=c('character'," + field_classes * ", " + ')' if field_classes.any?

    data.R <<-EOF, :plot, options
frames = #{R.ruby2R frames}
for (frame in seq(1, length(frames))){
  frame.value = frames[frame]
  frame.str = sprintf("%06d", frame)
  plot = { #{script} }

  png(paste('#{filename}', frame.str, 'tmp.png', sep="."), #{ width }, #{ height })
  { #{script} }
  dev.off()
}
data = NULL
    EOF
  else
    R.run <<-EOF, :plot, options
frames = #{R.ruby2R frames}
for (frame in seq(1, length(frames))){
  frame.value = frames[frame]
  frame.str = sprintf("%06d", frame)
  png(paste('#{filename}', frame.str, 'tmp.png', sep="."), #{ width }, #{ height })
  { #{script} }
  dev.off()
}
    EOF
  end
  CMD.cmd("convert #{filename}.*.tmp.png -set delay #{delay} -loop 0 #{filename} && rm #{filename}.*.tmp.png")
end