Calling R from Ruby

Yeah … in fact there is an R DCOM server and client in the CRAN
package repository for Windows. There’s also an interesting package
called, IIRC, RExcel, which lets you call an R server inside an Excel
spreadsheet.

Thomas wrote:

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


M. Edward (Ed) Borasky

Phil T. wrote:

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:

Hmmm … the R folks say the same thing about R. :slight_smile: Download it, load
the R Commander GUI front end, look at the R code it’s executing when it
does your analysis, learn to program in R slowly …

Actually, I really like the Ruby Matrix/Mathn/Rational/Complex setup.
It’s a little on the slow side for large problems, but for small
problems, it gets exact answers in a lot of cases and the code is quite
instructive.


M. Edward (Ed) Borasky

Hi,

Is there a ruby-like way to make an hash from two equeal sized
arrays, one containing the keys only and the other the values ?

Thanks a lot for any help in advance !

Ruby!
mcc

Edwin van Leeuwen wrote:

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.

Both Octave and SciLab are more or less “Matlab clones”, if that’s what
you want. Octave is open source. SciLab is “free as in beer” and may
have loosened their license a bit since I last looked.

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.


M. Edward (Ed) Borasky

Meino Christian C. wrote:

Hi,

Is there a ruby-like way to make an hash from two equeal sized
arrays, one containing the keys only and the other the values ?

Thanks a lot for any help in advance !

Would this work?

Hash[*arr1.zip(arr2)]

Hal

Hal F. wrote:

Hash[*arr1.zip(arr2)]

Hal

I think a flatten is in needed as well:

Hash[*keys.zip(vals).flatten]

/kel

From: Hal F. [email protected]
Subject: Re: Most simple way to do “array + array => hash”
Date: Fri, 31 Mar 2006 14:39:58 +0900

Hi Hal,

no, it seems not (from my irb):

a=(“A”…“Z”).to_a
=> [“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”,
“N”, “O”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”]
b=(“A”…“Z”).sort_by{rand}
=> [“D”, “C”, “X”, “P”, “Y”, “R”, “S”, “W”, “N”, “L”, “U”, “A”, “Z”,
“B”, “M”, “T”, “O”, “K”, “Q”, “G”, “I”, “F”, “J”, “H”, “V”, “E”]
c=Hash[*a.zip(b)]
=> {[“E”, “Y”]=>[“F”, “R”], [“Q”, “O”]=>[“R”, “K”], [“A”, “D”]=>[“B”,
“C”], [“W”, “J”]=>[“X”, “H”], [“M”, “Z”]=>[“N”, “B”], [“U”, “I”]=>[“V”,
“F”], [“C”, “X”]=>[“D”, “P”], [“I”, “N”]=>[“J”, “L”], [“O”, “M”]=>[“P”,
“T”], [“G”, “S”]=>[“H”, “W”], [“Y”, “V”]=>[“Z”, “E”], [“S”, “Q”]=>[“T”,
“G”], [“K”, “U”]=>[“L”, “A”]}

by the way: Is there a howto, a piece of text or a webpage or…
which describes all these “nice little things” which are possible
in ruby in a single line of code and which occupy the half of a page
of cryptic code in other languages ?

Especially ruby newbies like me often fall over such “little things”
like
converting a “A” to a 65, cause doing “A”[0] is very simple but not
what a long year C-coder would exspect at first hand.

Ruby!
mcc

“little things” like converting a “A” to a 65

Here’s another way:

$ ruby -w -e ‘puts ?A’
65

Meino Christian C. wrote:

From: Hal F. [email protected]
Subject: Re: Most simple way to do “array + array => hash”
Date: Fri, 31 Mar 2006 14:39:58 +0900

Hi Hal,

no, it seems not (from my irb):

Oops, forgot to flatten.

Hash[*a.zip(b).flatten]

irb(main):004:0> Hash[*a.zip(b).flatten]
=> {“V”=>“V”, “K”=>“U”, “W”=>“S”, “L”=>“K”, “A”=>“Z”, “X”=>“P”,
“M”=>“R”, “B”=>“X”,
“Y”=>“Q”, “N”=>“A”, “C”=>“M”, “Z”=>“O”, “O”=>“B”, “D”=>“I”, “P”=>“H”,
“E”=>“D”,
“Q”=>“E”, “F”=>“G”, “R”=>“W”, “G”=>“F”, “S”=>“N”, “H”=>“L”, “T”=>“Y”,
“I”=>“T”,
“U”=>“C”, “J”=>“J”}

Cheers,
Hal

Dave B. schrieb:

I have always used ‘A’[0] to return the ASCII value of A and as
rickhg12hs wrote ?A will do the same thing. I cannot find any
reference to this in Pickaxe so can anyone explain how this works?

Page 50 (first edition) or here:

Standard Types

Regards,
Pit

On 31 Mar 2006, at 08:38, rickhg12hs wrote:

“little things” like converting a “A” to a 65

Here’s another way:

$ ruby -w -e ‘puts ?A’
65

I have always used ‘A’[0] to return the ASCII value of A and as
rickhg12hs wrote ?A will do the same thing. I cannot find any
reference to this in Pickaxe so can anyone explain how this works?

Thanks,

Dave.

Very cute. I hopped onto irb to check out what each step of
Hash[*keys.zip(vals).flatten] does (I’d not used zip nor flatten in
some time) and I can’t quite figure out what function asterisk, ie,
*keys performs (leaving it out or executing *keys.zip(vals).flatten
results in a ruby error and I don’t see an explanation of this use of *
in the nutshell book.) Can one of you enlighten me?

Thx!

Ken

def foo(a, b, c)
“a:#{a} b:#{b} c:#{c}”
end
=> nil

foo(1, 2, 3)
=> “a:1 b:2 c:3”

x = [4, 5, 6]
=> [4, 5, 6]

foo(x)
ArgumentError: wrong number of arguments (1 for 3)

foo(*x)
=> “a:4 b:5 c:6”

On Mar 31, 2006, at 5:28 PM, [email protected] wrote:

I can’t quite figure out what function asterisk, ie,
*keys performs (leaving it out or executing *keys.zip(vals).flatten
results in a ruby error and I don’t see an explanation of this use
of *
in the nutshell book.) Can one of you enlighten me?

  • is the unary unarray operator

basically what it does delete the square brackets:

f(*[1,2,3]) -> f(1,2,3)

Hi,

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