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, 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

#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.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/simpler.rb', line 60

def run!(string=nil)
  @commands.push(string) if string
  reply = nil
  Open3.popen3("Rscript -") do |stdin, stdout, stderr|
    stdin.puts @commands.map {|v| v + "\n"}.join
    stdin.close_write
    reply = stdout.read 
  end
  @commands.clear
  Simpler::Reply.new(reply)
end

#run_with!(*objects, &block) ⇒ Object Also known as: go!



85
86
87
# File 'lib/simpler.rb', line 85

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



81
82
83
# File 'lib/simpler.rb', line 81

def show_with!(*objects, &block)
  with(*objects, &block).show!
end

#with(*objects, &block) ⇒ Object

returns self for chaining



73
74
75
76
77
78
79
# File 'lib/simpler.rb', line 73

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