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
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 ="# Loading basic rbbt environment\nsource('\#{UTIL}');\n\n EOF\n\n case\n when IO === command\n cmd << command.read\n when File.exists?(command)\n cmd << File.open(command, 'r') do |f| f.read end\n else\n cmd << command\n end\n\n Log.debug{\"R Script:\\n\#{ cmd }\"}\n\n if options.delete :monitor\n io = CMD.cmd('R --vanilla --slave --quiet', options.merge(:in => cmd, :pipe => true))\n while line = io.gets\n puts line\n end\n nil\n else\n CMD.cmd('R --vanilla --slave --quiet', options.merge(:in => cmd))\n end\nend\n" |