Calling R from Ruby

In article <596cfa1a67d6859b2795eb3d624e754e@ruby-forum.com>,

···

Edwin van Leeuwen <edder@tkwsping.nl> 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

Edwin van Leeuwen 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)

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

http://linuxcapacityplanning.com

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

http://linuxcapacityplanning.com

Phil Tomson 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

http://linuxcapacityplanning.com

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

Meino Christian Cramer 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

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

···

From: Hal Fulton <hal9000@hypermetrics.com>
Subject: Re: Most simple way to do "array + array => hash"
Date: Fri, 31 Mar 2006 14:39:58 +0900

Meino Christian Cramer 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 Fulton wrote:

Meino Christian Cramer 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

I think a flatten is in needed as well:

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

/kel

Meino Christian Cramer wrote:

From: Hal Fulton <hal9000@hypermetrics.com>
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

"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.

···

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

Dave Baldwin 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

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 3/31/06, coachhilton@gmail.com <coachhilton@gmail.com> wrote:

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?

* is the unary unarray operator

basically what it does delete the square brackets:

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

···

On Mar 31, 2006, at 5:28 PM, coachhilton@gmail.com 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?