[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Calling R from Ruby

AlexG

3/29/2006 9:05:00 AM

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 Gutteridge

22 Answers

Wilson Bilkovich

3/29/2006 2:26:00 PM

0

On 3/29/06, AlexG <alexg@ebi.ac.uk> wrote:
> 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.
>

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


gordon.j.miller@gmail.com

3/29/2006 3:35:00 PM

0


AlexG wrote:
> 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 Gutteridge

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.

Timothy Goddard

3/29/2006 11:07:00 PM

0

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.

Ara.T.Howard

3/29/2006 11:21:00 PM

0

AlexG

3/30/2006 12:49:00 AM

0

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.

AlexG

3/30/2006 1:20:00 AM

0

Timothy Goddard 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.biocond...) 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

M. Edward (Ed) Borasky

3/30/2006 5:19:00 AM

0

gordon.j.miller@gmail.com 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/rforbioin...

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. :)

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. ;)
> 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. :)

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://linuxcapacitypl...



ptkwt

3/30/2006 5:21:00 AM

0

In article <1143673625.994253.121460@i40g2000cwc.googlegroups.com>,
Timothy Goddard <interfecus@gmail.com> 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:
>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.
>

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

ptkwt

3/30/2006 5:22:00 AM

0

In article <Pine.LNX.4.62.0603291619410.32347@harp.ngdc.noaa.gov>,
<ara.t.howard@noaa.gov> wrote:
>On Thu, 30 Mar 2006, Timothy Goddard 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.
>

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

Phil

M. Edward (Ed) Borasky

3/30/2006 5:30:00 AM

0



Timothy Goddard 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:
> Fast matrix library.
> Inbuilt graphing.
> Don't have to teach statisticians OO concepts.
> Specialised.
>
> 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. :) 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://linuxcapacitypl...