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
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.
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.
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.
> 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
--
share your knowledge. it's a way to achieve immortality.
- h.h. the 14th dali lama
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
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. :)
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
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.
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
what are the alternatives to gnuplot? What does R use for it's graphing
engine?
Phil
> what are the alternatives to gnuplot?
you can make production quality graphics using
http://theory.kitp.ucsb.edu/~paxton/tioga.html
> 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.
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
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
> 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.
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
--
Posted via http://www.ruby-forum.com/.
If you're on windows, you could quite easily use ole for accessing R.
thomas
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 ;-)
Phil
A few years ago, I looked at a lot of open source math and graphics
packages for inclusion in a project. The candidates were Octave,
Xlisp-Stat, Perl Data Language (PerlDL or PDL) and R. When the smoke
cleared, I picked R, even though at the time I was primarily a Perl
programmer.
That project is still going strong today, as is R and its community. I
don't think XLisp-Stat has released anything since then, Octave is more
or less stagnant and I lost track of PerlDL -- it's still the package of
choice for things like astronomical image processing.
--
There is a version of my R embedding code at:
http://web.kuicr.kyoto-u.ac.jp/~alexg/files
This is very very rough and early. Think pre-pre-alpha. It is a
conversion of the core part of RSPerl allowing you to call R functions
from ruby scripts and pass simple data between them. Simple data means:
integers, floats and strings and arrays and hashes thereof. So you can
write:
require 'rsruby'
r=RSRuby.new #initialize embedded R interpreter
samples = 100
normal = r.rnorm(samples) #Calls R function 'rnorm'
puts normal #a Ruby array of floats converted from R vector
r.plot({ 'x' => normal, #You can call with named args
'xlab' => 'Hello Ruby',
'ylab' => 'Ruby ga suki desu'})
sleep(4) #Ooooh - pretty!
At this level it doesn't provide anything that Gordon/Ed's suggestion
of using an external R interpreter couldn't do. But perhaps with a bit
more work it might.
Anyway, if you're interested feel free to download and test. I've tried
it on a linux box and OSX, I have no idea how to build it under Windows
(or even if you could!). Documentation is essentially non-existent at
this stage, though there's a README which tells you how to build - you
just need to tell extconf.rb where your libR sits.
You can find my email address for bug reports/contributions in the
DESCRIPTION file.
Cheers,
AlexG