You are here: Vanderbilt Biostatistics Wiki>Main Web>RubyR (revision 18)EditAttach

RubyR

RubyR is a project to embed R into Ruby. At present, the motivation for this project is to provide the use of R data structures (vectors, lists, data frames, etc.) in Ruby for the purpose of serializing those structures to an Rdata file. R can then use the load() function to load those objects into its environment.

Currently this C extension is being used by Rhubarb, a Ruby on Rails application, to export data. The Ruby classes that represent the R objects provide a limited library of methods to operate on the R objects.

Installation

  1. Check out the source by running: svn co http://biostat.mc.vanderbilt.edu/svn/rubyR/trunk rubyR
  2. Run ruby extconf.rb to create the Makefile (NOTE: you'll need to add --with-R-include=/usr/share/R/include for (K)ubuntu or Debian flavors of Linux)
  3. Run make
  4. Run rake test to make sure everything's working
  5. Run make install as root
  6. Enjoi

Usage

Just put require 'R' in your Ruby scripts, and you're good to go. See below for examples.

RDoc

RubyR's RDoc can be found here.

Caveats

  • The R::Vector classes have support for NA values, but they are returned as the :NA symbol in Ruby. Unfortunately this breaks some of the enumerability of vectors (like sort) unless you explicitly handle :NA in your Ruby code.
  • To avoid problems, make sure the R version you're using to load() the Rdata files produced by RubyR is the same as the R version used to compile RubyR. In particular, R-2.4.0 stores data frames differently than R-2.3.1.

Examples

In IRB: require 'R'

int = R::Vector::Integer.new(rand(100)) { |i| rand(1000) } str = R::Vector::String.new(rand(100)) { |i| %w{foo bar baz}[rand(3)] } lgl = R::Vector::Logical.new(rand(100)) { |i| rand(2) == 1 } num = R::Vector::Real.new(rand(100)) { |i| rand } fac = R::Vector::Factor.new(100, %w{one two three four}) { |i| rand(4) }

R.save_to_file("foo.Rda", {"a" => int, "b" => str, "c" => lgl, "d" => num, "e" => fac})

Then in R:
load("foo.Rda")
a
b
c
d
e

Your R output should look something like this:
> load("foo.Rda")
> a
 [1] 586 213 984 322 554 763 486  43 223 672 450 875 169 716 546 954 981 442 826
[20] 156 814 842 131 438
> b
 [1] "foo" "baz" "baz" "foo" "baz" "baz" "baz" "foo" "baz" "foo" "foo" "bar"
[13] "bar" "foo" "baz" "foo" "foo" "baz" "foo" "bar" "bar" "baz" "bar" "bar"
[25] "bar" "bar" "foo" "bar" "baz" "baz" "foo" "foo" "bar" "baz" "baz" "baz"
[37] "baz" "foo" "foo" "baz" "foo" "baz" "foo" "bar" "bar" "baz" "baz" "baz"
[49] "baz" "foo" "foo" "baz" "bar" "bar" "foo" "baz" "foo" "baz" "foo" "bar"
[61] "bar" "bar" "bar" "bar" "baz" "bar" "bar" "bar" "baz" "bar" "bar" "baz"
[73] "foo" "foo" "foo" "bar" "foo" "baz" "bar" "bar" "foo" "foo" "foo" "baz"
[85] "bar" "bar" "baz" "foo" "bar" "bar" "foo" "bar" "bar" "foo" "foo"
> c
 [1]  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE
[13]  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
[25] FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE
> d
 [1] 0.80481245 0.15358652 0.21822570 0.40420726 0.63773041 0.95687706
 [7] 0.47109874 0.47078500 0.59065318 0.36784217 0.42267056 0.77195579
[13] 0.15261541 0.34295031 0.74020996 0.39298423 0.60418865 0.59777790
[19] 0.16961347 0.44924464 0.33090525 0.40109886 0.93959718 0.57462252
[25] 0.67694589 0.15027198 0.87078322 0.98481915 0.19631427 0.51006537
[31] 0.61422378 0.59355667 0.40173387 0.11081440 0.04072399 0.41344588
[37] 0.06217526 0.53537958 0.63638303 0.10726867 0.32925314 0.66092668
[43] 0.90042121 0.55546814 0.24175240 0.28035570 0.87056120 0.25695185
[49] 0.69791153 0.19975474 0.35160219 0.74634894 0.22443477 0.31664235
[55] 0.76884788 0.72293448 0.40119049 0.58142959 0.25967512 0.80700920
[61] 0.22755507 0.60960641 0.71397685 0.12059757 0.72580224 0.94587553
[67] 0.31582296 0.60482741 0.85659370 0.07748140 0.43626389 0.67175066
[73] 0.26209882 0.32871813
> e
  [1] two   one   four  four  one   one   two   three two   four  three four
 [13] two   four  two   two   two   one   two   one   four  two   four  one
 [25] two   three four  two   four  four  three three three one   four  three
 [37] one   three four  two   four  two   two   two   four  one   three three
 [49] one   two   one   one   three three two   four  four  three one   two
 [61] three two   three two   one   three four  three one   one   three two
 [73] four  two   four  three three three four  one   one   two   four  two
 [85] three four  three three one   two   two   three four  four  two   one
 [97] four  one   one   one
Levels: one two three four

See also the files in trunk/test for more examples.

Subversion Log

r19 [2007-08-02 10:58:48 -0500 (Thu, 02 Aug 2007)] by JeremyStephens

Log
Slightly changed some doc.

Files
  • M trunk/R.c

r18 [2007-08-02 10:50:13 -0500 (Thu, 02 Aug 2007)] by JeremyStephens

Log
Finished up adding documentation and tidying up.

Files
  • M trunk/R.c

r17 [2007-08-01 15:40:28 -0500 (Wed, 01 Aug 2007)] by JeremyStephens

Log
  • added RDoc to several classes
  • changed formatting for lots of functions for consistency (freakin' tedious)

I'm about halfway through. I can't do anymore of this today.

Files
  • M trunk/R.c

r16 [2007-04-27 14:24:04 -0500 (Fri, 27 Apr 2007)] by JeremyStephens

Log
Added copyright information.

Files
  • A trunk/COPYING

r15 [2007-02-14 14:18:07 -0600 (Wed, 14 Feb 2007)] by JeremyStephens

Log
Fixed a bug with -rpath.

Files
  • M trunk/extconf.rb

r14 [2007-02-14 14:09:14 -0600 (Wed, 14 Feb 2007)] by JeremyStephens

Log
Added better comments about how to build rubyR.

Files
  • M trunk/extconf.rb

r13 [2007-01-25 15:22:33 -0600 (Thu, 25 Jan 2007)] by JeremyStephens

Log
Improved extconf.rb to try to figure out where R is.

Files
  • M trunk/extconf.rb

r12 [2007-01-08 10:59:04 -0600 (Mon, 08 Jan 2007)] by JeremyStephens

Log
Added the svn:ignore property to ignore Makefile and *.so

Files
  • M trunk/

r11 [2006-12-11 16:51:49 -0600 (Mon, 11 Dec 2006)] by JeremyStephens

Log
Added R::Object#print to eval R's print() method on an object. Unfortunately there's no 'clean' way to make R's print() go to a string, so it uses what R thinks is stdout.

Files
  • M trunk/R.c

r10 [2006-12-11 11:29:36 -0600 (Mon, 11 Dec 2006)] by JeremyStephens

Log
R::Object#attrib_set now will accept a Ruby string as the value argument

Files
  • M trunk/R.c
  • M trunk/test/r_object_test.rb
Edit | Attach | Print version | History: r19 < r18 < r17 < r16 | Backlinks | View wiki text | Edit WikiText | More topic actions...
Topic revision: r18 - 02 Aug 2007, SubversionUser
 

This site is powered by FoswikiCopyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback