Module: R

Defined in:
lib/rbbt/util/R.rb,
lib/rbbt/util/R/eval.rb

Constant Summary collapse

LIB_DIR =
File.join(File.expand_path(File.dirname(__FILE__)),'../../../share/Rlib')
UTIL =
File.join(LIB_DIR, 'util.R')

Class Method Summary collapse

Class Method Details

.eval(cmd) ⇒ Object



10
11
12
# File 'lib/rbbt/util/R/eval.rb', line 10

def self.eval(cmd)
  instance.eval(cmd).payload.first
end

.eval_a(cmd) ⇒ Object



14
15
16
# File 'lib/rbbt/util/R/eval.rb', line 14

def self.eval_a(cmd)
  instance.eval(cmd).payload
end

.instanceObject



2
3
4
5
6
7
8
# File 'lib/rbbt/util/R/eval.rb', line 2

def self.instance
  @@instance ||= begin
                   require 'rserve'
                   @@server_process = CMD.cmd('R CMD Rserve --vanilla', :pipe => true)
                   Rserve::Connection.new
                 end
end

.interactive(script, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rbbt/util/R.rb', line 39

def self.interactive(script, options = {})
  TmpFile.with_file do |init_file|
      Open.write(init_file) do |f|
        f.puts "# Loading basic rbbt environment"
        f.puts "library(utils);\n"
        f.puts "source('#{R::UTIL}');\n"
        f.puts 
        f.puts script
      end
      CMD.cmd("env R_PROFILE='#{init_file}' xterm \"$RHOME/bin/R\"")
  end
end

.ruby2R(object) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rbbt/util/R.rb', line 52

def self.ruby2R(object)
  case object
  when nil
    "NULL"
  when TSV
    "matrix(#{R.ruby2R object.values},dimnames=list(#{R.ruby2R object.keys}, #{R.ruby2R object.fields}))"
  when Symbol
    "#{ object }"
  when String
    "'#{ object }'"
  when Fixnum, Float
    object
  when Array
    "c(#{object.collect{|e| ruby2R(e) } * ", "})"
  else
    raise "Type of object not known: #{ object.inspect }"
  end
end

.run(command, options = {}) ⇒ Object



10
11
12
13
14
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/rbbt/util/R.rb', line 10

def self.run(command, options = {})
  cmd =<<-EOF
# Loading basic rbbt environment
source('#{UTIL}');

  EOF

  case
  when IO === command
    cmd << command.read
  when File.exists?(command)
    cmd << File.open(command, 'r') do |f| f.read end
  else
    cmd << command
  end

  Log.debug{"R Script:\n#{ cmd }"}

  if options.delete :monitor
    io = CMD.cmd('R --vanilla --slave --quiet', options.merge(:in => cmd, :pipe => true))
    while line = io.gets
      puts line
    end
    nil
  else
    CMD.cmd('R --vanilla --slave --quiet', options.merge(:in => cmd))
  end
end

.tsv(file, options = {}) ⇒ Object



71
72
73
74
75
76
# File 'lib/rbbt/util/R.rb', line 71

def self.tsv(file, options = {})
  options = Misc.add_defaults :header_hash => '', :sep => / +/, :type => :list, :key_field => 'ID'
  key_field = Misc.process_options options, :key_field
  clean = CMD.cmd('grep -v WARNING', :in => file, :pipe => true)
  TSV.open(clean, options).tap{|tsv| tsv.key_field = key_field }
end