「RSRubyReferenceManualChapter2」の編集履歴(バックアップ)一覧に戻る
RSRubyReferenceManualChapter2 - (2008/06/16 (月) 20:52:49) のソース
第2章 インストールと使い方 2.1 要求 第一に RSRuby をインストールする前に Ruby (http://www.ruby-lang.org/) が必要です。 RSRuby の現在のバージョンでは Ruby 1.8.4 だけでテストされていますが、1.8.x 系以前のバージョンでは動くでしょう。 また、動作確認済みの R 実行環境のも必要です。 R を --enable-R-shlib オプション付きでビルドすることで RSRuby は R の共有ライブラリとインタフェースを取れるようになります。 RSRuby の現在のバージョンでは R 2.2.1 だけでテストされていますが、以前のバージョンでも動くでしょう。 2.2 インストール インストールするには R と標準 ruby 拡張インストーラ setup.rb や rubugems を使って適切な構成でセットアップしていなくてはいけません。 1. まずは、R_HOME 環境変数が設定されていることを確認してください。私の Ubuntu linux ではこうなっています: R_HOME=/usr/lib/R また OS X ではこうです: R_HOME=/Library/Frameworks/R.framework/Resources 2. Compile and install the Ruby ライブラリの setup.rb を使ってコンパイルとインストールをする場合はこうです。 libR 共有ライブラリ付きで R がインストールされている必要があります。 これは通常 R_HOME と同じになるでしょう. cd rsruby ruby setup.rb config -- --with-R-dir=/usr/lib/R ruby setup.rb setup sudo ruby setup.rb install もし、rubygems を利用するなら R のインストールディレクトリを指定する必要があります。 gem install -- --with-R-dir=/usr/lib/R もし RSRuby がコンパイルされないなら R ライブラリの設定パスが必要かもしれません。 Any one of the following should be suficient (taken from the RPy documentation): ? make a link to R_HOME/bin/libR.so in /usr/local/lib or /usr/lib, then run ldconfig, ? or, put the following line in your .bashrc (or equivalent): export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:R_HOME/bin ? or, edit the ?le /etc/ld.so.conf and add the following line: R_HOME/bin and then, run ldconfig. 3. Test it: ruby setup.rb test RSRuby should pass all tests. You can avoid needing root in the install step by providing setup.rb with a suitable install directory (such as home). Please run ruby setup.rb --help for more details. 2.3 Getting Started: A Small Example RSRuby works ?ne with irb. A sample session is shown: \$irb -rrsruby irb> r = RSRuby.instance => #<RSRuby:0xb7cdec04> require-ing rsruby loads the module, but unlike RPy it does not start the R in- terpreter. Calling RSRuby.instance starts the R interpreter, a reference to which is returned and stored in object r here. Since only one R interpreter can be running at a time, RSRuby uses the standard library Singleton module. This replaces RSRuby.new with RSRuby.instance. further calls to RSRuby.instance return the original interpreter object. Calls to R functions are made via the R interpreter object. In the example below the R function wilcox.test is called with two Ruby Arrays as arguments. These Arrays are converted to R by RSRuby. The list returned from the R method is converted to a Ruby Hash. Details of the conversion procedure can be found in Chapter 4. irb> r.wilcox_test([1,2,3],[4,5,6]) => {"p.value"=>0.1, "null.value"=>{"mu"=>0.0}, "data.name"=>"c(1, 2, 3) and c(4, 5, 6)", "method"=>"Wilcoxon rank sum test", "alternative"=>"two.sided", "parameter"=>nil, "statistic"=>{"W"=>0.0}}