以外と面倒だったのでメモ
環境
- Mac OS X 10.10
- eclipse 4.4
手順
Rのインストール
1 | brew install r |
scalaプロジェクトの作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $ tree . ├── build.sbt ├── lib ├── project │ ├── build.properties │ └── plugins.sbt └── src ├── main │ ├── resources │ └── scala └── test ├── resources └── scala |
rjavaを使うためのライブラリをコピー
1 2 | $ cp -rp ~/Library/R/3.2/library/rJava/jri/JRI.jar lib $ cp -rp ~/Library/R/3.2/library/rJava/jri/libjri.jnilib lib |
テストを作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import org.specs2.mutable._ import org.specs2.runner.JUnitRunner import org.junit.runner.RunWith import org.rosuda.JRI.{ REXP, Rengine } @RunWith(classOf[JUnitRunner]) class RTest extends Specification { "r " should { "r check" in { val engine = new Rengine(Array("--no-save"), false, null) val result = engine.eval("sum(c(1, 2, 3, 4))") engine.end() println(result.asDouble) result.asDouble mustEqual 10.0 } } } |
build.sbt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | name := "sample" version := "1.0" scalaVersion := "2.11.6" javaOptions in run += "-Djava.library.path=" + System.getProperty("java.library.path") + ":lib" libraryDependencies ++= Seq( "org.specs2" %% "specs2-core" % "3.6.2" % "test", "org.specs2" % "specs2-junit_2.11" % "3.6.2" ) scalacOptions in Test ++= Seq("-Yrangepos") retrieveManaged := true // lib_managedへjarをコピーする |
plugin.sbt
1 | addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0") |
build.properties
1 | sbt.version=0.13.8 |
eclipse用変換
1 | sbt eclipse |
Rの設定
1 | install.package("rJava") |
Eclipseの設定
- Eclipseを起動し、プロジェクトを取り込む
- プロジェクトのプロパティからjava build path > libraries > add jarsでJRI.jarを追加
- JRI.jarの左三角マークをクリックしnative library locationにlibディレクトリを追加
- RTest.scalaを右クリックから、Debug Configuration > environment にR_HOMEを設定
1 | R_HOME=/usr/local/Cellar/r/3.2.1_1/R.framework/Resources |
これで実行できます