Class: RInterface
- Inherits:
-
Object
- Object
- RInterface
- Defined in:
- lib/rocker/rinterface.rb
Overview
Constant Summary collapse
- @@R_BIN =
"R"
Instance Attribute Summary collapse
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ RInterface
constructor
A new instance of RInterface.
- #run(cmd, type = nil) ⇒ Object
Constructor Details
#initialize ⇒ RInterface
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
#handler ⇒ Object (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 |