Module: R::SVG
- Defined in:
- lib/rbbt/util/R/plot.rb
Class Method Summary collapse
- .ggplot(data, script = nil, width = nil, height = nil, options = {}) ⇒ Object
- .ggplotSVG(*args) ⇒ Object
- .plot(filename, data = nil, script = nil, width = nil, height = nil, options = {}, &block) ⇒ Object
Class Method Details
.ggplot(data, script = nil, width = nil, height = nil, options = {}) ⇒ Object
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 123 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 168 169 170 171 172 173 174 |
# File 'lib/rbbt/util/R/plot.rb', line 93 def self.ggplot(data, script = nil, width = nil, height = nil, = {}) width ||= 3 height ||= 3 values = [] = .dup sources = [:plot, :svg, [:source]].flatten.compact .delete :source field_classes = [:field_classes] fast = [:fast] if fast save_method = "rbbt.SVG.save.fast" else save_method = "rbbt.SVG.save" end 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 Array === values field_classes = values.collect do |v| v = v.first if Array === v case v when FalseClass, TrueClass "'logical'" when Numeric "'numeric'" when String if v.strip =~ /^[-+]?[\d\.]+$/ "'numeric'" else "'character'" end when Time "'Date'" when Symbol "'factor'" else ":NA" end end if field_classes.nil? if field_classes.any? [:R_open] ||= "colClasses=c('character'," + field_classes * ", " + ')' else [:R_open] ||= "colClasses=c('character')" end TmpFile.with_file nil, true, :extension => 'svg' do |tmpfile| data.R <<-EOF, sources, plot = { #{script} } #{save_method}('#{tmpfile}', plot, width = #{R.ruby2R width}, height = #{R.ruby2R height}) data = NULL EOF Open.read(tmpfile).gsub(/(glyph\d+-\d+)/, '\1-' + File.basename(tmpfile)) end else TmpFile.with_file nil, true, :extension => 'svg' do |tmpfile| R.run <<-EOF, sources, plot = { #{script} } #{save_method}('#{tmpfile}', plot, width = #{R.ruby2R width}, height = #{R.ruby2R height}) data = NULL EOF Open.read(tmpfile).gsub(/(glyph\d+-\d+)/, '\1-' + File.basename(tmpfile)) end end end |
.ggplotSVG(*args) ⇒ Object
89 90 91 |
# File 'lib/rbbt/util/R/plot.rb', line 89 def self.ggplotSVG(*args) ggplot(*args) end |
.plot(filename, data = nil, script = nil, width = nil, height = nil, options = {}, &block) ⇒ Object
4 5 6 7 8 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rbbt/util/R/plot.rb', line 4 def self.plot(filename, data = nil, script = nil, width = nil, height = nil, = {}, &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, [: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 [:R_open] ||= "colClasses=c('character'," + field_classes * ", " + ')' if field_classes.any? data.R <<-EOF, :plot, rbbt.svg_plot("#{ filename }", width=#{ width }, height = #{ height }, function(){ #{script} }) data = NULL EOF else R.run <<-EOF, :plot, rbbt.svg_plot("#{ filename }", width=#{ width }, height = #{ height }, function(){ #{script} }) EOF end end |