class ROSAR

Usage example:

r=ROSAR.instance
a=[1,2,3]
b=[4,5,6]
r.transfer :a=>a, :b=>b
r.plot :x=>:a, :y=>:b, :typ=>"'l'"
r.grid

Example based on dataframes:

df = {
  :x => [1,2,3,4],
  :y => [7,2,5.5,8]
}
r.data_frame :df, df
r.attach :df
r.plot :x=>:x, :y=>"y/2", :typ=>"'b'", :xlab=>"'Time (s)'"
r.grid
r.abline :h=>[2.5,3.5]
r.detach :df

Constants

EXCHANGE

Attributes

console[R]
r[R]

Public Class Methods

new(r="R") click to toggle source
# File lib/rosar.rb, line 68
def initialize(r="R")
  
  r = @@r unless @@r.empty?
  begin
    @r = OSA.app r
  rescue
    raise "Specify a valid command line to launch R-lang"
    exit
  end
  
  self.sync_dir
  self.activate
  # @console = @r.windows.select {|w| w.name =="R Console"}[0]
end
r=(r_com) click to toggle source
# File lib/rosar.rb, line 64
def self.r= r_com
  @@r = r_com
end

Public Instance Methods

activate() click to toggle source
Brings R windows to foreground.

end

# File lib/rosar.rb, line 101
def activate
  @r.activate
end
data_frame(name, df, keep=false) click to toggle source

Transfers the Hash of Arrays df to a dataframe named name. Uses a FIFO to move values around.

# File lib/rosar.rb, line 120
def data_frame(name, df, keep=false)
  File.open("#{EXCHANGE}", "w") do |f|
    f.puts df.keys*"\t"
    df[df.keys[0]].size.times do |i|
      df.each_key do |k|
        f.print "#{df[k][i] || 'NA'}\t"
      end
      f.puts
    end
  end
  @r.cmd "#{name}<-read.table('#{EXCHANGE}', h=T)"
  @r.cmd "unlink(\"#{EXCHANGE}\")" unless keep
end
maximize() click to toggle source
# File lib/rosar.rb, line 87
def maximize
  @console.miniaturized = false
end
method_missing(method, *args) click to toggle source

Redirects method to the underlaying OSA object.

# File lib/rosar.rb, line 141
def method_missing(method, *args)
  case args[0]
  when Hash
    @r.cmd("#{method}(#{args[0].to_r})")
  when Symbol
    @r.cmd("#{method}(#{args[0]})")
  when String
    @r.cmd("#{method}(#{args[0]})")
  when nil
    @r.cmd("#{method}()")
  end
end
minimize() click to toggle source
# File lib/rosar.rb, line 83
def minimize
  @console.miniaturized = true
end
raw(cmd) click to toggle source
# File lib/rosar.rb, line 134
def raw(cmd)
  @r.cmd(cmd)
end
sync_dir(dir=Dir.getwd) click to toggle source

Sets the R’s working directory to the current dir of the calling script.

# File lib/rosar.rb, line 94
def sync_dir(dir=Dir.getwd)
  @r.cmd "setwd('#{dir}')"
end
transfer(vars) click to toggle source

Transfers an Hash of Arrays to the R workspace. R object names are the Hash symbols.

# File lib/rosar.rb, line 109
def transfer(vars)
  raise "Expecting a Hash" unless vars.class == Hash
  vars.each_pair do |k,v|
    @r.cmd "#{k.to_s}<-#{v.to_r}" if v.class == Array
  end
end