Module: R::PNG

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

Class Method Summary collapse

Class Method Details

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rbbt/util/R/plot.rb', line 82

def self.ggplotPNG(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 Fixnum, Float
      "'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 "plot = { \#{script} }\n\nggsave('\#{filename}', plot, width = \#{R.ruby2R width}, height = \#{R.ruby2R height})\ndata = NULL\n  EOF\nend\n", :plot, options

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rbbt/util/R/plot.rb', line 124

def self.plot(filename, data, script = nil, width = nil, height = nil, options = {})
  width ||= 200
  height ||= 200
  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 Fixnum, Float
      "'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 "png(\"\#{ filename }\", \#{ width }, \#{ height })\n{ \#{script} }\ndev.off()\ndata = NULL\n  EOF\nend\n", :plot, options