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
-
.r_format(object) ⇒ String
formats strings and numbers to fit inside R code (this is for Strings and numbers mainly in inclusion in options) String ==> “"String"” Numeric ==> “Numeric” Other objects => “#objectobject.to_s”.
-
.varname(obj) ⇒ String
The variable name of the object.
Instance Method Summary collapse
- #error_message(error_string, r_code) ⇒ Object
-
#eval!(*objects, &block) ⇒ Object
(see Simpler#with).
-
#initialize(commands = [], opts = {:pdf_viewer => PDF_VIEWER}) ⇒ Simpler
constructor
PDF_VIEWER).
- #open_pdf_viewer(viewer, *files) ⇒ Object
-
#run! ⇒ Simpler::Reply
executes all commands and clears the command array.
-
#show!(*objects, &block) ⇒ Object
(see Simpler#eval!) same as eval! but also opens “Rplots.pdf” with PDF_VIEWER Simpler uses Rscript to run its code and Rscript writes X11 output to the PDF file “Rplots.pdf”.
-
#with(*objects) {|*r_varnames| ... } ⇒ Simpler
Returns self for chaining.
Methods included from Plot
Constructor Details
#initialize(commands = [], opts = {:pdf_viewer => PDF_VIEWER}) ⇒ Simpler
PDF_VIEWER)
31 32 33 34 |
# File 'lib/simpler.rb', line 31 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.
25 26 27 |
# File 'lib/simpler.rb', line 25 def commands @commands end |
#pdf_viewer ⇒ Object
Returns the value of attribute pdf_viewer.
26 27 28 |
# File 'lib/simpler.rb', line 26 def pdf_viewer @pdf_viewer end |
Class Method Details
.r_format(object) ⇒ String
formats strings and numbers to fit inside R code (this is for Strings and numbers mainly in inclusion in options)
String ==> "\"String\""
Numeric ==> "Numeric"
Other objects => "#{object.to_s}"
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/simpler.rb', line 127 def self.r_format(object) case object when String object.inspect when Numeric object.to_s else object.to_s end end |
.varname(obj) ⇒ String
Returns the variable name of the object.
21 22 23 |
# File 'lib/simpler.rb', line 21 def self.varname(obj) "rb#{obj.object_id}" end |
Instance Method Details
#error_message(error_string, r_code) ⇒ Object
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/simpler.rb', line 138 def (error_string, r_code) = "" << "\n" << "*************************** Error inside R *****************************\n" << error_string << "------------------------- [R code submitted] ---------------------------\n" << r_code << "------------------------------------------------------------------------\n" end |
#eval!(*objects, &block) ⇒ Object
(see Simpler#with)
58 59 60 |
# File 'lib/simpler.rb', line 58 def eval!(*objects, &block) with(*objects, &block).run! end |
#open_pdf_viewer(viewer, *files) ⇒ Object
149 150 151 |
# File 'lib/simpler.rb', line 149 def open_pdf_viewer(viewer, *files) system "#{@pdf_viewer} #{files.join(" ")} &" end |
#run! ⇒ Simpler::Reply
executes all commands and clears the command array.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/simpler.rb', line 103 def run! 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 |
#show!(*objects, &block) ⇒ Object
(see Simpler#eval!) same as eval! but also opens “Rplots.pdf” with PDF_VIEWER Simpler uses Rscript to run its code and Rscript writes X11 output to the PDF file “Rplots.pdf”. Simpler#show! merely makes a system call to open up the pdf file for viewing. Obviously, for more interactive sessions one should just use R.
74 75 76 77 78 79 |
# File 'lib/simpler.rb', line 74 def show!(*objects, &block) with(*objects, &block) reply = run! open_pdf_viewer(@pdf_viewer, RPLOTS_FILE) reply end |
#with(*objects) {|*r_varnames| ... } ⇒ Simpler
Returns self for chaining
91 92 93 94 95 96 97 98 99 |
# 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) unless block.nil? @commands << block.call(*var_names) end self end |