Class: RInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/rocker/rinterface.rb

Overview

Author:

  • Luis M. Rodriguez-R <lmrodriguezr at gmail dot com>

  • Luis (Coto) Orellana

Constant Summary collapse

@@R_BIN =
"R"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRInterface

Returns a new instance of RInterface.



12
13
14
# File 'lib/rocker/rinterface.rb', line 12

def initialize
   @handler = IO.popen("#{@@R_BIN} --slave 2>&1", "w+")
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



11
12
13
# File 'lib/rocker/rinterface.rb', line 11

def handler
  @handler
end

Class Method Details

.R_BIN=(rbin) ⇒ Object



10
# File 'lib/rocker/rinterface.rb', line 10

def RInterface.R_BIN=(rbin) @@R_BIN=rbin end

Instance Method Details

#run(cmd, type = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rocker/rinterface.rb', line 15

def run(cmd, type=nil)
   @handler.puts cmd
   @handler.puts "cat('---FIN---\n')"
   o = ""
   while true
      l = @handler.gets
	 raise "R failed on command:\n#{cmd}\n\nError:\n#{o}" if l.nil?
	 break unless /^---FIN---/.match(l).nil?
	 o += l
   end
   o.chomp!
   case type
   when :float
	 /^\s*\[1\]\s+([0-9\.Ee+-]+|Inf).*/.match(o).nil? and raise "R error: expecting float, got #{o}"
	 return Float::INFINITY if $1=='Inf'
	 return $1.to_f
   when :int
	 /^\s*\[1\]\s+([0-9\.Ee+-]+).*/.match(o).nil? and raise "R error: expecting integer, got #{o}"
	 return $1.to_i
   else
	 return o
   end
end