Overview
In the previous post I showed how the rscala package (which has replaced the jvmr package) can be used to call Scala code from within R. In this post I will show how to call R from Scala code. I have previously described how to do this using jvmr. This post is really just an update to show how things work with rscala.
Since I’m focusing here on Scala sbt projects, I’m assuming that sbt is installed, in addition to rscala (described in the previous post). The only “trick” required for calling back to R from Scala is telling sbt where the rscala jar file is located. You can find the location from the R console as illustrated by the following session:
> library(rscala) > rscala::rscalaJar("2.11") [1] "/home/ndjw1/R/x86_64-pc-linux-gnu-library/3.2/rscala/java/rscala_2.11-1.0.6.jar"
This location (which will obviously be different for you) can then be added in to your sbt classpath by adding the following line to your build.sbt file:
unmanagedJars in Compile += file("/home/ndjw1/R/x86_64-pc-linux-gnu-library/3.2/rscala/java/rscala_2.11-1.0.6.jar")
Once this is done, calling out to R from your Scala sbt project can be carried out as described in the rscala documentation. For completeness, a working example is given below.
Example
In this example I will use Scala to simulate some data consistent with a Poisson regression model, and then push the data to R to fit it using the R function glm(), and then pull back the fitted regression coefficients into Scala. This is obviously a very artificial example, but the point is to show how it is possible to call back to R for some statistical procedure that may be “missing” from Scala.
The dependencies for this project are described in the file build.sbt
name := "rscala test" version := "0.1" scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature") libraryDependencies ++= Seq( "org.scalanlp" %% "breeze" % "0.10", "org.scalanlp" %% "breeze-natives" % "0.10" ) resolvers ++= Seq( "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/", "Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/" ) unmanagedJars in Compile += file("/home/ndjw1/R/x86_64-pc-linux-gnu-library/3.2/rscala/java/rscala_2.11-1.0.6.jar") scalaVersion := "2.11.6"
The complete Scala program is contained in the file PoisReg.scala
import org.ddahl.rscala.callback._ import breeze.stats.distributions._ import breeze.linalg._ object ScalaToRTest { def main(args: Array[String]) = { // first simulate some data consistent with a Poisson regression model val x = Uniform(50,60).sample(1000) val eta = x map { xi => (xi * 0.1) - 3 } val mu = eta map { math.exp(_) } val y = mu map { Poisson(_).draw } // call to R to fit the Poission regression model val R = RClient() // initialise an R interpreter R.x=x.toArray // send x to R R.y=y.toArray // send y to R R.eval("mod <- glm(y~x,family=poisson())") // fit the model in R // pull the fitted coefficents back into scala val beta = DenseVector[Double](R.evalD1("mod$coefficients")) // print the fitted coefficents println(beta) } }
If these two files are put in an empty directory, the code can be compiled and run by typing sbt run from the command prompt in the relevant directory. The commented code should be self-explanatory, but see the rscala documentation for further details. In particular, the rscala scaladoc is useful.