RackR - Use R in your Rack stack
RackR is a Rack middleware which integrates R into a Rack based web application (like Rails). With RackR in place displaying graphs is as simple as rendering R code into your output. E.g.
<script type='text/r'> png('sinus.png') plot(sin) </script>
RackR will answer to this reqyest with processing the R code in a temp directory, then searching this directory for displayable content and finally return the html code for display. So the example above will eventually turn into something like this.
<div> <img src='/path/to/sinus.png' /> <pre> content of Rout file (stdout of R) </pre> </div>
Almost everything can conveniently be configured in a YAML file. RackR
will create a sample config file in
config/rack-r.yml or any other
Install in Rails
Put the following in your Gemfile and run
bundle install or let
Guard kick in.
gem 'rack-r', :require => 'rack_r'
In Rails 3.2 you will have to put this somewhere
Using RackR outside of Rails
require 'rack_r/middleware' use RackR::Middleware, :config => 'path/to/config/rack-r.yml'
These instructions are for Debian Squeeze. Install R.
apt-get install r-base r-cran-dbi
Alternatively you can use the
apt-get install r-cran-rodbc
The RackR-Header is a pice of R code that gets prepended to every R
script which is processed by RackR. Idealy it will read you database
config and provide a seperate connection to your database via a DBI
If you want RackR to automatically connect R script to yoyr Rails database it is a good idea to install Jeremy Stephens' YAML for R.
wget http://cran.r-project.org/src/contrib/yaml_2.1.4.tar.gz R CMD INSTALL yaml_2.1.4.tar.gz
wget http://cran.r-project.org/src/contrib/RSQLite_0.11.1.tar.gz R CMD INSTALL RSQLite_0.11.1.tar.gz
apt-get install r-cran-rmysql
The whole RackR-Header is a work in progress, if you have to adjust it to your database config, please consider to contribute your addition.
Browse to /rack-r/ (including the trailing slash!) to see if RackR is working. If so it should respond with "RackR OK."
Patches and the like
If you run into bugs, have suggestions or patches feel free to drop me a line.
RackR is released under MIT License, see LICENSE.