Module: R
- Defined in:
- lib/rbbt/util/R.rb
Constant Summary collapse
- LIB_DIR =
File.join(File.(File.dirname(__FILE__)),'../../../share/Rlib')
- UTIL =
File.join(LIB_DIR, 'util.R')
Class Method Summary collapse
- .interactive(script, options = {}) ⇒ Object
- .ruby2R(object) ⇒ Object
- .run(command, options = {}) ⇒ Object
- .tsv(file, options = {}) ⇒ Object
Class Method Details
.interactive(script, options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rbbt/util/R.rb', line 38 def self.interactive(script, = {}) 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
51 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 51 def self.ruby2R(object) case object when nil "NULL" when TSV #"as.matrix(data.frame(c(#{object.transpose("Field").collect{|k,v| "#{k}=" << R.ruby2R(v)}.flatten * ", "}), row.names=#{R.ruby2R object.keys}))" "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
9 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 |
# File 'lib/rbbt/util/R.rb', line 9 def self.run(command, = {}) 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 .delete :monitor io = CMD.cmd('R --vanilla --slave --quiet', .merge(:in => cmd, :pipe => true)) while line = io.gets puts line end nil else CMD.cmd('R --vanilla --slave --quiet', .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, = {}) = Misc.add_defaults :header_hash => '', :sep => / +/, :type => :list, :key_field => 'ID' key_field = Misc. , :key_field clean = CMD.cmd('grep -v WARNING', :in => file, :pipe => true) TSV.open(clean, ).tap{|tsv| tsv.key_field = key_field } end |