Module: R::SVG

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

Class Method Summary collapse

Class Method Details

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



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
# File 'lib/rbbt/util/R/plot.rb', line 105

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

  options = options.dup

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

  entity_geom = options.delete :entity_geom

  field_classes = options[:field_classes]

  fast = options[:fast]

  if fast
    save_method = "rbbt.SVG.save.fast"
  else
    save_method = "rbbt.SVG.save"
  end

  if data

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

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

    TmpFile.with_file nil, true, :extension => 'svg' do |tmpfile|

      if entity_geom
        data.R <<-EOF, sources, options
  plot = { #{script} }
  #{save_method}('#{tmpfile}', plot, width = #{R.ruby2R width}, height = #{R.ruby2R height}, entity.geom=#{R.ruby2R(entity_geom)}, data=data)
  data = NULL
        EOF
      else
        data.R <<-EOF, sources, options
  plot = { #{script} }

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

      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, options
  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



101
102
103
# File 'lib/rbbt/util/R/plot.rb', line 101

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

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



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
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rbbt/util/R/plot.rb', line 40

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

  field_classes = options[:field_classes]

  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
  rbbt.svg_plot("#{ filename }", width=#{ width }, height = #{ height }, function(){ #{script} })
  data = NULL
    EOF
  else
    R.run <<-EOF, :plot, options
  rbbt.svg_plot("#{ filename }", width=#{ width }, height = #{ height }, function(){ #{script} })
    EOF
  end
end