Module: R::PNG

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

Class Method Summary collapse

Class Method Details

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



179
180
181
182
183
184
# File 'lib/rbbt/util/R/plot.rb', line 179

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

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



186
187
188
189
190
191
# File 'lib/rbbt/util/R/plot.rb', line 186

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

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/rbbt/util/R/plot.rb', line 197

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

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

  data.each do |k,v|
    v = Array === v ? v : [v]
    next if v == "NA" or v.nil? or v.include? "NA" or v.include? nil
    values = v
    break
  end
  values = [values] unless Array === values
  field_classes = values.collect do |v| 
    case v
    when FalseClass, TrueClass
      "'logical'"
    when Numeric
      "'numeric'"
    when String
      if v.strip =~ /^[-+]?[\d\.]+$/
        "'numeric'"
      else
        "'character'"
      end
    when Symbol
      "'factor'"
    else
      ":NA"
    end
  end
  options[:R_open] ||= "colClasses=c('character'," + field_classes * ", " + ')'

  data.R <<-EOF, :plot, options
rbbt.require('ggplot2')
plot = { #{script} }

ggsave('#{filename}', plot, width = #{R.ruby2R width}, height = #{R.ruby2R height})
data = NULL
  EOF
end

.ggplotPNG(*args) ⇒ Object



193
194
195
# File 'lib/rbbt/util/R/plot.rb', line 193

def self.ggplotPNG(*args)
  ggplot(*args)
end

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



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
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
319
320
321
322
323
# File 'lib/rbbt/util/R/plot.rb', line 240

def self.plot(filename, data = 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
  
  if data
    data.each do |k,v|
      v = Array === v ? v : [v]
      next if v == "NA" or v.nil? or v.include? "NA" or v.include? nil
      values = v
      break
    end 

    values = [values] unless values.nil? or Array === values

    field_classes = values.collect do |v| 
      case v
      when FalseClass, TrueClass
        "'logical'"
      when Numeric
        "'numeric'"
      when String
        if v.strip =~ /^[-+]?[\d\.]+$/
          "'numeric'"
        else
          "'character'"
        end
      when Symbol
        "'factor'"
      else
        ":NA"
      end
    end

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

    data.R <<-EOF, :plot, options
  rbbt.png_plot("#{ filename }", width=#{ width }, height = #{ height }, function(){ #{script} })
  data = NULL
    EOF
  else
    R.run <<-EOF, :plot, options
  rbbt.png_plot("#{ filename }", width=#{ width }, height = #{ height }, function(){ #{script} })
    EOF
  end
end