67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
|
# File 'lib/rbbt/util/R/plot.rb', line 67
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 <<-EOF, :plot, options
plot = { #{script} }
ggsave('#{filename}', plot, width = #{R.ruby2R width}, height = #{R.ruby2R height})
data = NULL
EOF
end
|