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

filename_to_plottype, #plot

Constructor Details

#initialize(commands = [], opts = {:pdf_viewer => PDF_VIEWER}) ⇒ Simpler

PDF_VIEWER)

Parameters:

  • commands (Array) (defaults to: [])

    a list of R commands to execute

  • opts (Hash) (defaults to: {:pdf_viewer => PDF_VIEWER})

    options (currently :pdf_viewer which defaults to



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

#commandsObject

Returns the value of attribute commands.



25
26
27
# File 'lib/simpler.rb', line 25

def commands
  @commands
end

#pdf_viewerObject

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}"

Parameters:

  • object (Object)

    the ruby object

Returns:

  • (String)

    string suitable for R code



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.

Parameters:

  • object (Object)

    a ruby object,

Returns:

  • (String)

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

#eval!(*objects, &block) ⇒ Object

(see Simpler#with)

Examples:

basic

# find the p value of a t-test
r.eval! do 
  %Q{
    x <- c(1,2,3)
    y <- c(4,5,6)
    ttest = t.test(x, y)
    ttest$p.value
  } 
end     # -> "[1] 0.02131164\n"

pass in variables (roughly equivalent)

x = [1,2,3]
y = [4,5,6]
r.eval!(x,y) do |xr,yr| 
  %Q{
    ttest = t.test(#{xr}, #{yr})
    ttest$p.value
  } 
end     # -> "[1] 0.02131164\n"


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.

Returns:



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_message(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.

Examples:

plotting

x = [1,2,3]
y = [4,5,6]
r.show!(x,y) do |xr,yr|
  "plot(#{xr}, #{yr})"
end


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

Examples:

chaining

reply = r.with(dataframe) do |df|
  %Q{ ... something with #{df} ... }
end.with(x,y,z) do |xr,yr,zr|
  %Q{ ... something with #{xy} #{yr} #{zr} ... }
end.eval!
# can also .show!

Parameters:

  • objects (*Objects)

    list of objects

Yields:

  • (*r_varnames)

    yields the R name of each variable (see Simpler.varname)

Returns:

  • (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