Module: R
- Defined in:
- lib/rbbt/util/R.rb,
lib/rbbt/util/R/eval.rb,
lib/rbbt/util/R/model.rb
Defined Under Namespace
Classes: Model
Constant Summary collapse
- LIB_DIR =
File.join(File.(File.dirname(__FILE__)),'../../../share/Rlib')
- UTIL =
File.join(LIB_DIR, 'util.R')
- SESSION =
ENV["RServe-session"] || "Session-PID-" + Process.pid.to_s
Class Attribute Summary collapse
-
.model_dir ⇒ Object
Returns the value of attribute model_dir.
Class Method Summary collapse
- ._eval(cmd) ⇒ Object
- .clear ⇒ Object
- .console(script, options = {}) ⇒ Object
- .eval(cmd) ⇒ Object
- .eval_a(cmd) ⇒ Object
- .eval_run(cmd) ⇒ Object
- .instance ⇒ Object
- .interactive(script, options = {}) ⇒ Object
- .lockfile ⇒ Object
- .pid_file ⇒ Object
- .ruby2R(object) ⇒ Object
- .run(command, options = {}) ⇒ Object
- .socket_file ⇒ Object
- .tsv(file, options = {}) ⇒ Object
- .workdir ⇒ Object
Class Attribute Details
.model_dir ⇒ Object
Returns the value of attribute model_dir.
6 7 8 |
# File 'lib/rbbt/util/R/model.rb', line 6 def model_dir @model_dir end |
Class Method Details
._eval(cmd) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rbbt/util/R/eval.rb', line 105 def self._eval(cmd) Misc.lock lockfile do times = 2 begin instance.eval(cmd) rescue Rserve::Connection::EvalError times = times - 1 if times > 0 clear retry else raise $! end end end end |
.clear ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rbbt/util/R/eval.rb', line 33 def self.clear Log.warn "Clearing Rserver session #{SESSION}, PID #{@@instance_process}" @@instance = nil if defined? @@instance_process and @@instance_process and Misc.pid_exists? @@instance_process begin Process.kill :INT, @@instance_process rescue Exception end end FileUtils.rm_rf pid_file if File.exists? pid_file FileUtils.rm_rf socket_file if File.exists? socket_file FileUtils.rm_rf lockfile if File.exists? lockfile FileUtils.rm_rf workdir if File.exists? workdir end |
.console(script, options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rbbt/util/R.rb', line 52 def self.console(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 pid = Process.fork do |ppid| ENV["R_PROFILE"] = init_file exec("R") end begin Process.waitpid pid rescue Interrupt if Misc.pid_exists? pid Process.kill "INT", pid retry else raise $! end rescue Exception Process.kill 9, pid if Misc.pid_exists? pid raise $! ensure Process.waitpid pid if Misc.pid_exists? pid end end end |
.eval(cmd) ⇒ Object
126 127 128 |
# File 'lib/rbbt/util/R/eval.rb', line 126 def self.eval(cmd) eval_a(cmd).first end |
.eval_a(cmd) ⇒ Object
122 123 124 |
# File 'lib/rbbt/util/R/eval.rb', line 122 def self.eval_a(cmd) _eval(cmd).payload end |
.eval_run(cmd) ⇒ Object
130 131 132 |
# File 'lib/rbbt/util/R/eval.rb', line 130 def self.eval_run(cmd) _eval(cmd) end |
.instance ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rbbt/util/R/eval.rb', line 48 def self.instance @@instance ||= begin clear if File.exists? pid_file and ! Misc.pid_exists?(Open.read(pid_file).strip.to_i) FileUtils.mkdir_p File.dirname(socket_file) unless File.directory?(File.dirname(socket_file)) FileUtils.mkdir_p workdir unless File.directory? workdir at_exit do self.clear end unless defined? @@instance_process begin if not File.exists? socket_file sh_pid = Process.fork do #args = %w(CMD Rserve --vanilla --quiet --RS-socket) args = %w(--vanilla --quiet --RS-socket) args << "'#{socket_file}'" args << "--RS-workdir" args << "'#{workdir}'" args << "--RS-pidfile" args << "'#{pid_file}'" bin_path = File.join(ENV["R_HOME"], "bin/Rserve") cmd = bin_path + " " + args*" " exec(ENV, cmd) end while not File.exists? pid_file sleep 0.5 end @@instance_process = Open.read(pid_file).to_i Log.info "New Rserver session stated with PID (#{sh_pid}) #{@@instance_process}: #{SESSION}" end i = Rserve::Connection.new :hostname => socket_file begin FileUtils.mkdir workdir unless File.exists? workdir i.eval "setwd('#{workdir}');" i.eval "source('#{UTIL}');" i rescue Exception Log.exception $! raise TryAgain end rescue Exception Log.exception $! Process.kill :INT, @@instance_process if defined? @@instance_process and @@instance_process FileUtils.rm socket_file if File.exists? socket_file retry if TryAgain === $! raise $! end 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, = {}) TmpFile.with_file do |init_file| Open.write(init_file) do |f| f.puts "# Loading basic rbbt environment" f.puts "library(utils, quietly=TRUE);\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 |
.lockfile ⇒ Object
21 22 23 |
# File 'lib/rbbt/util/R/eval.rb', line 21 def self.lockfile @@lockfile ||= socket_file + '.lock' end |
.pid_file ⇒ Object
29 30 31 |
# File 'lib/rbbt/util/R/eval.rb', line 29 def self.pid_file @@pidfile ||= File.join(workdir, 'pid') end |
.ruby2R(object) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rbbt/util/R.rb', line 87 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[0] == ":" ? object[1..-1] : "'#{ object }'" when Fixnum, Float object when TrueClass "TRUE" when FalseClass "FALSE" 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, = {}) 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 --quiet', .merge(:in => cmd, :pipe => true, :log => true)) while line = io.gets puts line end nil else CMD.cmd('R --vanilla --slave --quiet', .merge(:in => cmd)) end end |
.socket_file ⇒ Object
17 18 19 |
# File 'lib/rbbt/util/R/eval.rb', line 17 def self.socket_file @@socket_file ||= Rbbt.tmp.R_sockets[R::SESSION].find end |
.tsv(file, options = {}) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/rbbt/util/R.rb', line 110 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 |
.workdir ⇒ Object
25 26 27 |
# File 'lib/rbbt/util/R/eval.rb', line 25 def self.workdir @@workdir ||= socket_file + '.wd' end |