Calling R from Ruby


#1

Hi,

I’m interested in making (well using, but I might be prepared to make)
a bridge between Ruby and R. The RSPerl and RSPython packages seem to
do pretty much what I want to do (in Perl and Python).

My first question is does such a thing already exist? I could only find
ruby-rmathlib (a wrapper for some of the math functions included in R)
on the RAA and nothing on rubyforge. For obvious reasons searching for
‘R’ related topics is always difficult so I may well have missed
something.

I’ve a basic proof of concept (adapted from the RSPerl source code)
(provisionally called rruby) where by:

require “rruby”
RRuby.new.eval_R(“print”,“me”)

Does what you would expect (fires up an embedded R interpeter and
prints “me”), and it seemed simple enough that I thought someone else
might have already done this and had some more advanced code that I
could borrow/look at/contribute to. If not, I will look at converting a
little more of RSPerl tomorrow to see if I can get something actually
useful working.

Alex G.


#2

On 3/29/06, AlexG removed_email_address@domain.invalid wrote:

something.

How about this?
http://rubyforge.org/projects/r4ruby/
I haven’t used it myself, but from the description, it sounds like
what you’re looking for.


#3

AlexG wrote:

something.
could borrow/look at/contribute to. If not, I will look at converting a
little more of RSPerl tomorrow to see if I can get something actually
useful working.

Alex G.

Have you thought about simply spawning an inferior process and simply
pass R scripts into the process? This is basically what emacs does
with their ESS mode and it works wonderfully well. Depending on the
level of integration that you wanted to achieve between R and Ruby this
could work very well. I’m not sure how well suited the R system is to
be treated as a library, though I’ll confess I haven’t really looked
into it.

If one were to simply use an inferior process that you passed straight
R scripts into you could write a set of functions to translate between
Ruby and R speak (array notation, using <- instead of =, parsing
through the response from R to set a global hash of R variables, etc).

I’d be willing to contribute and test stuff as I use R quite a bit for
data analysis. Its a very, very good language for doing mathematics,
statistics, and modelling that I don’t want to redo in Ruby. Not that
I couldn’t I just don’t want to repeat the effort. The biggest problem
is R has a huge lack of string processing features that make setting up
model runs and generating reports that Ruby is quite good at.


#4

May I ask what this is for? I’ve had to learn R recently after working
with Ruby for a while and find myself thinking constantly “This would
be much simpler if I could only use Ruby!”. As far as I’ve worked out
so far:

R’s advantages:
Fast matrix library.
Inbuilt graphing.
Don’t have to teach statisticians OO concepts.
Specialised.

R’s disadvantages:
Procedural, with highly limited function support.
Slow loops.
Depends on blas (and as a result libfortran).
Specialised.

For most purposes, you could do better using Ruby with a good graphing
system (such as gnuplot). There are, of course exceptions, but I would
consider all options first.


#5

Yeah, from the description it is exactly what I was thinking of.
Unfortunately there’s no code to download and the last message on
either the rubyforge site or the wiki is from February 2005 - saying
that something would get released in July 2005. Sadly it looks like a
dead project, which kind of implies the whole idea is harder than I
first imagined. Thanks anyway.


#6

On Thu, 30 Mar 2006, Timothy G. wrote:

May I ask what this is for? I’ve had to learn R recently after working
with Ruby for a while and find myself thinking constantly “This would
be much simpler if I could only use Ruby!”. As far as I’ve worked out
so far:

R’s advantages:
Don’t have to teach statisticians OO concepts.
Specialised.

you really can subtract

Fast matrix library.
Inbuilt graphing.

because narray and gnuplot are very good. narray is very fast.

regards.

-a


#7

Timothy G. wrote:

May I ask what this is for? I’ve had to learn R recently after working
with Ruby for a while and find myself thinking constantly “This would
be much simpler if I could only use Ruby!”.

Like you, I spend most of my time in R wishing I could use Ruby.
However, whatever I’m doing, I always seem find a few R
functions/libraries that are really useful. I could rewrite the
functions in Ruby, but by then I’d have found some more functions that
I needed and so on. A universal .evalR function that I could call from
Ruby would make all that pain go away. The inbulit graphing is also
nice, though as Ara (and yourself) say, gnuplot could replace that
fairly easily.

For me specifically the ability to use some of the code/classes in the
Bioconductor project (http://www.bioconductor.org/) would really be
nice as well - though I don’t know how practical that is.

For most purposes, you could do better using Ruby with a good graphing
system (such as gnuplot). There are, of course exceptions, but I would
consider all options first.

Sound advice. I will have a look at trying to get a Ruby-R bridge
working, but perhaps moving everything to Ruby is the simpler option. I
will ponder on this further.

AlexG


#8

removed_email_address@domain.invalid wrote:

Have you thought about simply spawning an inferior process and simply
pass R scripts into the process? This is basically what emacs does
with their ESS mode and it works wonderfully well. Depending on the
level of integration that you wanted to achieve between R and Ruby this
could work very well. I’m not sure how well suited the R system is to
be treated as a library, though I’ll confess I haven’t really looked
into it.

The “guts” of how to interface R with C, C++ and Fortran, and how to
build R as a shared library and call it from the “outside world” are
documented in the “Writing R Extensions” manual that comes with the
source (or the Windows installer). I took the course up at Fred
Hutchinson Cancer Research Center in January and we covered this in a
fair amount of detail. The sample code is on line at

http://www.bioconductor.org/workshops/2006/rforbioinformatics/

If you want to do any of this magic on Windows, be prepared to download
a lot of tools and be prepared to put aside your creative urges and just
follow directions to the letter. :slight_smile:

I have never used “RSPerl”; I usually call R scripts from Perl via

system("R < script.R");

passing parameters as environment variables – Perl exports them and R
imports them.

If one were to simply use an inferior process that you passed straight
R scripts into you could write a set of functions to translate between
Ruby and R speak (array notation, using <- instead of =, parsing
through the response from R to set a global hash of R variables, etc).

“=” works in R now as an assignment operator. I’m not sure when that
happened; I’ve gotten into the habit of “<-” and probably don’t intend
to switch. :wink:

I’d be willing to contribute and test stuff as I use R quite a bit for
data analysis. Its a very, very good language for doing mathematics,
statistics, and modelling that I don’t want to redo in Ruby. Not that
I couldn’t I just don’t want to repeat the effort. The biggest problem
is R has a huge lack of string processing features that make setting up
model runs and generating reports that Ruby is quite good at.

R isn’t as bad at “scripting” and “regular expression handling” as all
that. It ain’t Perl and it ain’t Ruby, and if you’re at all an
object-oriented programmer, you’ll find the way R does objects and
classes totally different from the “real world”. Then again, so is the
way Perl does it – Ruby is a lot more like a “real object-oriented
language.”

Of course, when I first started using R, it really sucked at the
character handling, and I wrote a lot of Perl making “CSV” files for R
to handle. Nowadays I just load things into a database and let RODBC do
all the work. :slight_smile:

I’m sorta kinda looking at a project that would use Rails to manage
the database(s), shelling out to “R” for the analysis. I’ve got very
little lead time, though, so it might have to be just a refactoring of
the hundreds of lines of Perl and R I have now. And I don’t have any
Ruby-aware cow-orkers … that’s a risk too.


M. Edward (Ed) Borasky

http://linuxcapacityplanning.com


#9

Timothy G. wrote:

R’s disadvantages:
Procedural, with highly limited function support.

Au contraire! R is at heart a functional language with a Lisp ancestry!
What’s different about R (and S) is two different ways of doing objects
and classes, neither one of which is remotely like a Java or Ruby
programmer’s idea of objects and classes.

Slow loops.

Yeah … R programmers don’t usually use loops – we use vector and
matrix operations that have the loops written in C. :slight_smile: R is probably
more like APL than it is like Ruby, although it does have some Lisp-like
and Scheme-like constructs.

Depends on blas (and as a result libfortran).

Depends on blas for linear algebra only. Can hook up to the
machine-tuned Atlas linear algebra library if necessary.

Specialised.

True, R is not a general-purpose environment, although you can do
anything in it. If you look at two of its most successful application
areas, bioinformatics and computational finance, you’ll find that the
“non-numeric” parts of the job aren’t always done outside of the R
code. That’s the way most of us learned to use it, and old habits die
hard, but I’ve seen web servers written in R. Check out the “Rpad”
library for some really nifty things you can do.

For most purposes, you could do better using Ruby with a good graphing
system (such as gnuplot). There are, of course exceptions, but I would
consider all options first.

Actually, I think that’s been done … one thing Ruby could use is
something along the lines of the Perl Data Language/Library. But an “R
Server” for numerical work would be easy to do.


M. Edward (Ed) Borasky

http://linuxcapacityplanning.com


#10

In article removed_email_address@domain.invalid,
removed_email_address@domain.invalid wrote:

you really can subtract

Fast matrix library.
Inbuilt graphing.

because narray and gnuplot are very good. narray is very fast.

what are the alternatives to gnuplot? What does R use for it’s graphing
engine?

Phil


#11

In article removed_email_address@domain.invalid,
Timothy G. removed_email_address@domain.invalid wrote:

R’s disadvantages:
Procedural, with highly limited function support.
Slow loops.
Depends on blas (and as a result libfortran).
Specialised.

For most purposes, you could do better using Ruby with a good graphing
system (such as gnuplot). There are, of course exceptions, but I would
consider all options first.

This has come up before, but maybe we need a Ruby-based DSL that could
be aimed
squarely at the likes of Matlab and R. We’d need to distribute it with
a
graphing system so that it would look like graphing was ‘built-in’. We
could
use narray as the basis for fast matrix math. One could stay at the DSL
level
or one could delve more deeply and learn the underlying Ruby in order to
get
the greatest advantage of this sort of math-oriented DSL: users wouldn’t
have
to know Ruby, but if they did it would become a more powerful tool. irb
could
be used for an interactive mode…

What should we call this?

rcalc
rmath
mathgem
rubysci
?

… a good name can help something like this get started.

Phil


#12

On Thu, 30 Mar 2006, Phil T. wrote:

rcalc
rmath
mathgem
rubysci
?

… a good name can help something like this get started.

http://theory.kitp.ucsb.edu/~paxton/tioga_doc/classes/Tioga/Tutorial/Sample.html

i realize it’s not totally numerical… but he’s thinking along the same
lines. fyi.

-a


#13

Phil T. wrote:

This has come up before, but maybe we need a Ruby-based DSL that could be aimed
squarely at the likes of Matlab and R.
When you say “we”, who are you referring to? People who only know Ruby?
As far as I am concerned, R, Axiom and Maxima are (open source) Domain
Specific Languages for mathematics. Ruby is a general-purpose
object-oriented language. Sure, Ruby is good for building
domain-specific languages, but you just aren’t going to reinvent all
those wheels in Ruby.
rcalc
rmath
mathgem
rubysci
?

… a good name can help something like this get started.

Phil

There are a few things out there, but nothing with as much horsepower as
R and the two main general purpose symbolic engines, Axiom and Maxima.
There are hundreds of person-decades involved in building those three
packages. In fact, the latter two weren’t originally open source. Axiom
was a commercial product, and Maxima was a US government funded project.
R was always an open source project, however.


M. Edward (Ed) Borasky

http://linuxcapacityplanning.com


#14

On Thu, 30 Mar 2006, Phil T. wrote:

what are the alternatives to gnuplot?

you can make production quality graphics using

http://theory.kitp.ucsb.edu/~paxton/tioga.html

regards.

-a


#15

In article removed_email_address@domain.invalid,
M. Edward (Ed) Borasky removed_email_address@domain.invalid wrote:

Phil T. wrote:

This has come up before, but maybe we need a Ruby-based DSL that could be aimed
squarely at the likes of Matlab and R.
When you say “we”, who are you referring to? People who only know Ruby?
As far as I am concerned, R, Axiom and Maxima are (open source) Domain
Specific Languages for mathematics. Ruby is a general-purpose
object-oriented language. Sure, Ruby is good for building
domain-specific languages, but you just aren’t going to reinvent all
those wheels in Ruby.

Where reinvention would take too long, then perhaps it would be best to
build
bridges. I guess in my case I’d like to see something that could take
on
Matlab (a closed source product).

Hey, a couple of years ago we were here in this group discussing how
Ruby could
have an impact in the Web Programming arena since it looked like Ruby
was an
also-ran in that area (there were so many tools that were already
dominant in
that space - or so it seemed). Now Rails has changed all of that.
Scientific
computing/ mathematics is another area where there’s room for
innovation.

Maybe we take this proposed tool in a slightly different direction.
Make
it easy to make things web-enabled for example, or maybe make it easy to
use in
a cluster environment (taking advantage of drb). A LaTeX equation to
RubyMathDSL translator. Maybe a Rails, RubyMathDSL, LaTeX mashup where
your
‘paper’ is some sort of interactive document where you can interact with
equations and graphs and maybe something that would make it easier to do
collaborative research where the data in your paper can be referenced by
other interactive papers which can make use of that data in interesting
ways
and add to it and… Ok, it’s getting a bit
wierd, I’ll admit. I’m just trying to spark a bit of brainstorming
here…

However that bit about data-sharing between interactive papers seems
kind of
cool. It would give a whole new meaning to a ‘bibliography’ and
‘references’.
It’s not easy right now to share data like that. When I’m reading
papers I
sometimes think “They spent all this time gathering or generating all of
this
wonderful data and putting it into nice little tables or graphs, but
there’s
really no way for me to access it”. Of course this would require a
whole new
infrastructure for publishing papers as web apps, but this is 2006
afterall.

Phil


#16

On Thurs, Mar 30 2006 2:54 pm Ara wrote:

On Thu, 30 Mar 2006, Phil T. wrote:

what are the alternatives to gnuplot?

you can make production quality graphics using

http://theory.kitp.ucsb.edu/~paxton/tioga.html

Wow. That’s nice. Really nice! Thanks for the link - not seen it
before.

For those following along at home. I have extended my toy R/Ruby bridge
to be able to directly call any R function with any number of
parameters (as long as they’re Integers, Floats or Strings!). Or any
function at all via parse(eval(“cmd”)). The next step is getting some
conversion code for arrays, etc… And then a lot of tidying up.


#17

In article removed_email_address@domain.invalid,
removed_email_address@domain.invalid wrote:

http://theory.kitp.ucsb.edu/~paxton/tioga_doc/classes/Tioga/Tutorial/Sample.html

i realize it’s not totally numerical… but he’s thinking along the same
lines. fyi.

Looks interesting. Hadn’t run across it before. Maybe Tioga would be a
good
candidate for the graphics component of what I’m talking about.

Phil


#18

For an R bridge I use a simple class that writes stuff to a script file
and then executes the script with R. It is far from perfect but doable.
But I would certainly welcome a better bridge.

If you’re on windows, you could quite easily use ole for accessing R.

thomas


#19

unknown wrote:

Where reinvention would take too long, then perhaps it would be best to
build
bridges. I guess in my case I’d like to see something that could take
on
Matlab (a closed source product).

You can already do a lot of what matlab does with ruby-gsl. There are
also a couple of gnuplot and plotutils bridges to make plotting easy.
(plotutils bridge is part of ruby-gsl)

For an R bridge I use a simple class that writes stuff to a script file
and then executes the script with R. It is far from perfect but doable.
But I would certainly welcome a better bridge.

Edwin


#20

In article removed_email_address@domain.invalid,
Edwin van Leeuwen removed_email_address@domain.invalid wrote:

unknown wrote:

Where reinvention would take too long, then perhaps it would be best to
build
bridges. I guess in my case I’d like to see something that could take
on
Matlab (a closed source product).

You can already do a lot of what matlab does with ruby-gsl. There are
also a couple of gnuplot and plotutils bridges to make plotting easy.
(plotutils bridge is part of ruby-gsl)

True, these things already exist to some degree, but what we need is
something
that’s packaged in a very nice way so that someone who doesn’t know Ruby
can
download it and start using it without even suspecting that they’re
using
Ruby… then as they’re getting hooked on it they can start learning
Ruby
without even suspecting. Another gateway product, kind of like Rails
:wink:

Phil