Introduction

RSRuby is a partial conversion of RPy (rpy.sourceforge.net/), the original RSRuby was based on RSPerl (www.omegahat.org/RSPerl/) (hence RSRuby), however almost all the code is now from RPy. RSRuby provides the ability to embed a full R interpreter inside a running Ruby script. R methods can then be called from the Ruby script and data passed between the R interpreter and the Ruby script.

License

Copyright © 2006 Alex Gutteridge

The Original Code is the RPy python module.

The Initial Developer of the Original Code is Walter Moreira. Portions created by the Initial Developer are Copyright © 2002 the Initial Developer. All Rights Reserved.

Contributor(s): Gregory R. Warnes <[email protected]> (RPy Maintainer)

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

Installation

Obviously a working R installation is required. R must have been installed/built with the ‘–enable-R-shlib’ option enabled to provide the R shared library. I have tested on R version 2.2.1, but earlier version might work.

Firstly, on OS X please set the compiler to gcc 3.3

sudo gcc_select 3.3

Then (on all systems) to install:

  1. Set the R_HOME environment variable appropriately:

R_HOME=/usr/lib/R (on my Ubuntu Linux box) R_HOME=/Library/Frameworks/R.framework/Resources (on OS X)

  1. Compile/install the Ruby library using setup.rb. You need to supply the location of your R installation for the libR shared library. This may be the same as R_HOME, e.g. (‘/usr/lib/R’ on Ubuntu, ‘/Library/Frameworks/R.framework/Resources’ on OS X):

cd rsruby ruby setup.rb config – –with-R-dir=/usr/lib/R ruby setup.rb setup sudo ruby setup.rb install

If RSRuby does not compile you may need to configure the path to the R library (this wasn’t required on either of my machines, but your mileage may vary). From the RPy README, anyone of the following should be sufficient:

o make a link to RHOME/bin/libR.so in /usr/local/lib or /usr/lib, then

run 'ldconfig',

o or, put the following line in your .bashrc (or equivalent):

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:RHOME/bin

o or, edit the file /etc/ld.so.conf and add the following line:

  RHOME/bin

and then, run 'ldconfig'.
  1. Test it.

ruby setup.rb test

Should pass all tests.

Installation Notes

If you’re brave then you can combine the config, setup and install steps into ‘sudo ruby setup.rb all – –with-R-dir=/usr/lib/R’.

You can avoid needing root/sudo access in the install step by providing setup.rb with a suitable install directory (such as home). Please run ‘ruby setup.rb –help’ for more details.

A Ruby Gem version of RSRuby is also available.

Usage

To use (read examples and tests for more hints - the RPy manual will also be helpful until I have written something similar myself!):

#Initialize R
require 'rsruby'

#RSRuby uses Singleton design pattern so call instance rather
#than new
r = RSRuby.instance
#Call R functions
data = r.rnorm(100)
r.plot(data)
sleep(2)
#Call with named args
r.plot({'x' => data,
        'y' => data,
        'xlab' => 'test',
        'ylab' => 'test'})
sleep(2)