Class: ROSAR

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rosar.rb

Overview

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

Constant Summary collapse

EXCHANGE =
"rosar.exc"
@@r =

command line to launch R

""

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r = "R") ⇒ ROSAR

Returns a new instance of ROSAR.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Redirects method to the underlaying OSA object.



141
142
143
144
145
146
147
148
149
150
151
152
# 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

Instance Attribute Details

#consoleObject (readonly)

Returns the value of attribute console.



61
62
63
# File 'lib/rosar.rb', line 61

def console
  @console
end

#rObject (readonly)

Returns the value of attribute r.



61
62
63
# File 'lib/rosar.rb', line 61

def r
  @r
end

Class Method Details

.r=(r_com) ⇒ Object



64
65
66
# File 'lib/rosar.rb', line 64

def self.r= r_com
  @@r = r_com
end

Instance Method Details

#activateObject

Brings R windows to foreground.



101
102
103
# File 'lib/rosar.rb', line 101

def activate
  @r.activate
end

#data_frame(name, df, keep = false) ⇒ Object

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

to move values around.


120
121
122
123
124
125
126
127
128
129
130
131
132
# 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

#maximizeObject



87
88
89
# File 'lib/rosar.rb', line 87

def maximize
  @console.miniaturized = false
end

#minimizeObject



83
84
85
# File 'lib/rosar.rb', line 83

def minimize
  @console.miniaturized = true
end

#raw(cmd) ⇒ Object



134
135
136
# File 'lib/rosar.rb', line 134

def raw(cmd)
  @r.cmd(cmd)
end

#sync_dir(dir = Dir.getwd) ⇒ Object

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



94
95
96
# File 'lib/rosar.rb', line 94

def sync_dir(dir=Dir.getwd)
  @r.cmd "setwd('#{dir}')"
end

#transfer(vars) ⇒ Object

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

Hash symbols.


109
110
111
112
113
114
# 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