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
- Check out the source by running:
svn co http://biostat.mc.vanderbilt.edu/svn/rubyR/trunk rubyR
- 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)
- Run
make
- Run
rake test
to make sure everything's working
- Run
make install
as root
- 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
Log
Finished up adding documentation and tidying up.
Files
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
Log
Added copyright information.
Files
Log
Files
Log
Added better comments about how to build rubyR.
Files
Log
Improved extconf.rb to try to figure out where R is.
Files
Log
Added the svn:ignore property to ignore Makefile and *.so
Files
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
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
Log
A few changes:
- Changed most ArgumentError's to TypeError's to be consistent with Ruby
- Changed default values for vectors to be the same as R's defaults
- All attributes setting/getting now use R::Object's methods
- Added various functions to set/get attributes in different ways
- Added functions to make R vectors from Array's
Files
- M trunk/R.c
- M trunk/test/r_data_frame_test.rb
- M trunk/test/r_list_test.rb
- M trunk/test/r_object_test.rb
- M trunk/test/r_test.rb
- M trunk/test/r_vector_test.rb