You are here:
Vanderbilt Biostatistics Wiki
>
Main Web
>
RubyR
(revision 18) (raw view)
Edit
Attach
---+ RubyR %TOC% 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 [[http://www.rubyonrails.org][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= 1 Run =ruby extconf.rb= to create the Makefile (*NOTE*: you'll need to add <code>--with-R-include=/usr/share/R/include</code> for (K)ubuntu or Debian flavors of Linux) 1 Run =make= 1 Run =rake test= to make sure everything's working 1 Run =make install= as root 1 Enjoi ---++ Usage Just put =require 'R'= in your Ruby scripts, and you're good to go. See [[#Examples][below]] for examples. ---++ RDoc RubyR's RDoc can be found [[http://biostat.mc.vanderbilt.edu/rdoc/rubyR/index.html][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:* <coderay> 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}) </coderay> *Then in R:* <verbatim> load("foo.Rda") a b c d e </verbatim> Your R output should look something like this: <verbatim> > 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 </verbatim> See also the files in [[http://biostat.mc.vanderbilt.edu/svn/rubyR/trunk/test][trunk/test]] for more examples. ---++ Subversion Log <!-- svn log start --> <div style="background-color: #BDE8F6; display: inline"><b>r19</b> [2007-08-02 10:58:48 -0500 (Thu, 02 Aug 2007)] by <i> Main.JeremyStephens </i></div><br> <div style="padding-left: 15px"> <div style="color: navy; font-weight: bold">Log</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> <noautolink> Slightly changed some doc. </noautolink> </div> <div style="color: red; font-weight: bold">Files</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> * %BLUE% M %ENDCOLOR% trunk/R.c </div> </div> <div style="background-color: #BDE8F6; display: inline"><b>r18</b> [2007-08-02 10:50:13 -0500 (Thu, 02 Aug 2007)] by <i> Main.JeremyStephens </i></div><br> <div style="padding-left: 15px"> <div style="color: navy; font-weight: bold">Log</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> <noautolink> Finished up adding documentation and tidying up. </noautolink> </div> <div style="color: red; font-weight: bold">Files</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> * %BLUE% M %ENDCOLOR% trunk/R.c </div> </div> <div style="background-color: #BDE8F6; display: inline"><b>r17</b> [2007-08-01 15:40:28 -0500 (Wed, 01 Aug 2007)] by <i> Main.JeremyStephens </i></div><br> <div style="padding-left: 15px"> <div style="color: navy; font-weight: bold">Log</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> <noautolink> * 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. </noautolink> </div> <div style="color: red; font-weight: bold">Files</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> * %BLUE% M %ENDCOLOR% trunk/R.c </div> </div> <div style="background-color: #BDE8F6; display: inline"><b>r16</b> [2007-04-27 14:24:04 -0500 (Fri, 27 Apr 2007)] by <i> Main.JeremyStephens </i></div><br> <div style="padding-left: 15px"> <div style="color: navy; font-weight: bold">Log</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> <noautolink> Added copyright information. </noautolink> </div> <div style="color: red; font-weight: bold">Files</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> * %GREEN% A %ENDCOLOR% trunk/COPYING </div> </div> <div style="background-color: #BDE8F6; display: inline"><b>r15</b> [2007-02-14 14:18:07 -0600 (Wed, 14 Feb 2007)] by <i> Main.JeremyStephens </i></div><br> <div style="padding-left: 15px"> <div style="color: navy; font-weight: bold">Log</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> <noautolink> Fixed a bug with -rpath. </noautolink> </div> <div style="color: red; font-weight: bold">Files</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> * %BLUE% M %ENDCOLOR% trunk/extconf.rb </div> </div> <div style="background-color: #BDE8F6; display: inline"><b>r14</b> [2007-02-14 14:09:14 -0600 (Wed, 14 Feb 2007)] by <i> Main.JeremyStephens </i></div><br> <div style="padding-left: 15px"> <div style="color: navy; font-weight: bold">Log</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> <noautolink> Added better comments about how to build rubyR. </noautolink> </div> <div style="color: red; font-weight: bold">Files</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> * %BLUE% M %ENDCOLOR% trunk/extconf.rb </div> </div> <div style="background-color: #BDE8F6; display: inline"><b>r13</b> [2007-01-25 15:22:33 -0600 (Thu, 25 Jan 2007)] by <i> Main.JeremyStephens </i></div><br> <div style="padding-left: 15px"> <div style="color: navy; font-weight: bold">Log</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> <noautolink> Improved extconf.rb to try to figure out where R is. </noautolink> </div> <div style="color: red; font-weight: bold">Files</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> * %BLUE% M %ENDCOLOR% trunk/extconf.rb </div> </div> <div style="background-color: #BDE8F6; display: inline"><b>r12</b> [2007-01-08 10:59:04 -0600 (Mon, 08 Jan 2007)] by <i> Main.JeremyStephens </i></div><br> <div style="padding-left: 15px"> <div style="color: navy; font-weight: bold">Log</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> <noautolink> Added the svn:ignore property to ignore Makefile and *.so </noautolink> </div> <div style="color: red; font-weight: bold">Files</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> * %BLUE% M %ENDCOLOR% trunk/ </div> </div> <div style="background-color: #BDE8F6; display: inline"><b>r11</b> [2006-12-11 16:51:49 -0600 (Mon, 11 Dec 2006)] by <i> Main.JeremyStephens </i></div><br> <div style="padding-left: 15px"> <div style="color: navy; font-weight: bold">Log</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> <noautolink> 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. </noautolink> </div> <div style="color: red; font-weight: bold">Files</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> * %BLUE% M %ENDCOLOR% trunk/R.c </div> </div> <div style="background-color: #BDE8F6; display: inline"><b>r10</b> [2006-12-11 11:29:36 -0600 (Mon, 11 Dec 2006)] by <i> Main.JeremyStephens </i></div><br> <div style="padding-left: 15px"> <div style="color: navy; font-weight: bold">Log</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> <noautolink> R::Object#attrib_set now will accept a Ruby string as the value argument </noautolink> </div> <div style="color: red; font-weight: bold">Files</div> <img src="/twiki/pub/fading-border.png"/> <div style="margin-top: 3px"> * %BLUE% M %ENDCOLOR% trunk/R.c * %BLUE% M %ENDCOLOR% trunk/test/r_object_test.rb </div> </div> <!-- svn log end -->
Edit
|
Attach
|
P
rint version
|
H
istory
:
r19
<
r18
<
r17
<
r16
|
B
acklinks
|
V
iew topic
|
Edit WikiText
|
More topic actions...
Topic revision: r18 - 02 Aug 2007,
SubversionUser
Main
Department Home Page
Biostatistics Graduate Program
Vanderbilt University Medical Center
Main Web
Main Web Home
Search
Recent Changes
Changes
Topic list
Biostatistics Webs
Archive
Main
Sandbox
System
Register
|
Log In
Copyright © 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