simpler
simpler (“simple R”) is a lightweight wrapper that calls R (really Rscript) from within ruby. It is designed to favor R in syntax.
Why use this rather than rsruby?
You should probably be using rsruby–it’s a fantastic gem. However, if you want to code using more of an R code sytax (e.g., to cut and paste R code and have it just work), or if you can’t get rsruby working, this gem may be useful to you.
Examples
FYI - the API is still unstable.
Basic execution (Raw input, raw output)
require 'simpler'
simpler = Simpler.new
simpler.run!("mean(c(1,2,3))") # -> "[1] 2\n" (a Simpler::Reply object)
Using ruby variables (calculating correlation coefficient):
xv = [1,2,7]
yv = [3,4,8]
reply = simpler.with(xv,yv) {|x,y| "cor(#{x},#{y})" }.run! # -> "[1] 0.9994238\n"
Show a plot
“Rscript” writes plotting commands that would normally go to an X window to “Rplots.pdf”. show! merely executes your code and opens Rplots.pdf with @pdf_viewer. It’s low tech, but it works.
simpler.pdf_viewer = "acroread"
simpler.with(xv,yv) {|x,y| "plot(#{x}, #{y})" }.show!
Using DataFrames
hash = {
:one => [1,2,6,7],
:two => [3,4,2,9],
:three => [3,1,1,7],
}
df = Simpler::DataFrame.new(hash)
simpler.with(df) {|d| "plot(#{d})" }.show!
DataFrame also supports named rows and specifying directly column names.
Credit
Simpler is loosely inspired by the original gnuplot and, of course, the excellent rsruby.
Casting
All replies are of class Simpler::Reply, so casting can be done in a way that works for you by defining your own methods.
Copyright
Copyright © 2010 John Prince. See LICENSE for details.