Class: Simpler

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Plot

#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

#commandsObject

Returns the value of attribute commands.



29
30
31
# File 'lib/simpler.rb', line 29

def commands
  @commands
end

#pdf_viewerObject

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_message(error_string, r_code)
  error_message = ""
  error_message << "\n"
  error_message << "*************************** Error inside R *****************************\n"
  error_message << error_string
  error_message << "------------------------- [R code submitted] ---------------------------\n"
  error_message << r_code
  error_message << "------------------------------------------------------------------------\n"
  error_message
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_message(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