Class: Simpler
- Inherits:
-
Object
- Object
- Simpler
- Includes:
- Plot
- Defined in:
- lib/simpler.rb,
lib/simpler/plot.rb,
lib/simpler/reply.rb,
lib/simpler/data_frame.rb
Defined Under Namespace
Modules: Plot Classes: DataFrame, RError, Reply
Constant Summary collapse
- RPLOTS_FILE =
"Rplots.pdf"
- PDF_VIEWER =
"evince"
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#pdf_viewer ⇒ Object
Returns the value of attribute pdf_viewer.
Class Method Summary collapse
-
.filename_to_plottype(name) ⇒ Object
returns it as a symbol, currently recognizes pdf, png, svg.
-
.varname(obj) ⇒ Object
returns the variable name of the object.
Instance Method Summary collapse
- #error_message(error_string, r_code) ⇒ Object
-
#initialize(commands = [], opts = {:pdf_viewer => PDF_VIEWER}) ⇒ Simpler
constructor
A new instance of Simpler.
- #r_format(object) ⇒ Object
-
#run!(string = nil) ⇒ Object
pushes string onto command array (if given), executes all commands, and clears the command array.
- #run_with!(*objects, &block) ⇒ Object (also: #go!)
-
#show!(string = nil) ⇒ Object
displays the Rplots.pdf file at the end of execution.
- #show_with!(*objects, &block) ⇒ Object
-
#with(*objects, &block) ⇒ Object
returns self for chaining.
Methods included from Plot
Constructor Details
#initialize(commands = [], opts = {:pdf_viewer => PDF_VIEWER}) ⇒ Simpler
Returns a new instance of Simpler.
32 33 34 35 |
# File 'lib/simpler.rb', line 32 def initialize(commands=[], opts={:pdf_viewer => PDF_VIEWER}) @pdf_viewer = opts[:pdf_viewer] @commands = commands end |
Instance Attribute Details
#commands ⇒ Object
Returns the value of attribute commands.
29 30 31 |
# File 'lib/simpler.rb', line 29 def commands @commands end |
#pdf_viewer ⇒ Object
Returns the value of attribute pdf_viewer.
30 31 32 |
# File 'lib/simpler.rb', line 30 def pdf_viewer @pdf_viewer end |
Class Method Details
.filename_to_plottype(name) ⇒ Object
returns it as a symbol, currently recognizes pdf, png, svg
25 26 27 |
# File 'lib/simpler.rb', line 25 def self.filename_to_plottype(name) name.match(/\.([^\.]+)$/)[1].downcase.to_sym end |
.varname(obj) ⇒ Object
returns the variable name of the object
20 21 22 |
# File 'lib/simpler.rb', line 20 def self.varname(obj) "rb#{obj.object_id}" end |
Instance Method Details
#error_message(error_string, r_code) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/simpler.rb', line 58 def (error_string, r_code) = "" << "\n" << "*************************** Error inside R *****************************\n" << error_string << "------------------------- [R code submitted] ---------------------------\n" << r_code << "------------------------------------------------------------------------\n" end |
#r_format(object) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/simpler.rb', line 37 def r_format(object) case object when String object.inspect when Numeric object.to_s else object.to_s end end |
#run!(string = nil) ⇒ Object
pushes string onto command array (if given), executes all commands, and clears the command array.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/simpler.rb', line 72 def run!(string=nil) @commands.push(string) if string reply = nil error = nil cmds_to_run = @commands.map {|v| v + "\n"}.join Open3.popen3("Rscript -") do |stdin, stdout, stderr| stdin.puts cmds_to_run stdin.close_write error = stderr.read reply = stdout.read end @commands.clear if error.size > 0 raise Simpler::RError, (error, cmds_to_run) end Simpler::Reply.new(reply) end |
#run_with!(*objects, &block) ⇒ Object Also known as: go!
103 104 105 |
# File 'lib/simpler.rb', line 103 def run_with!(*objects, &block) with(*objects, &block).run! end |
#show!(string = nil) ⇒ Object
displays the Rplots.pdf file at the end of execution
49 50 51 52 53 54 55 56 |
# File 'lib/simpler.rb', line 49 def show!(string=nil) if File.exist?(RPLOTS_FILE) original_mtime = File.mtime(RPLOTS_FILE) end reply = run!(string) system "#{@pdf_viewer} #{RPLOTS_FILE} &" reply end |
#show_with!(*objects, &block) ⇒ Object
99 100 101 |
# File 'lib/simpler.rb', line 99 def show_with!(*objects, &block) with(*objects, &block).show! end |
#with(*objects, &block) ⇒ Object
returns self for chaining
91 92 93 94 95 96 97 |
# File 'lib/simpler.rb', line 91 def with(*objects, &block) var_names = objects.map {|v| Simpler.varname(v) } conversion_code = objects.map {|v| v.to_r } @commands.push(*conversion_code) @commands << block.call(*var_names) self end |