Another major statistical processing environment is R. It's an open source programming language and environment designed for statistical analysis. It's widely used and has an active community as well as a huge and growing body of useful add-on packages.
While there's no Clojure-specific interoperability library, there is one for Java, and we can use that to pass calls to R and to get results back. In this recipe, we'll set this system up.
We'll need to have R installed. We can download it from http://www.r-project.org/ by following the link to CRAN, picking a mirror, and downloading the correct version of R for our platform.
You'll also need to have Maven (http://maven.apache.org/) installed in order to build and install the libraries to access R.
There are two parts to setting up this system. We'll get the R-side working, and then we'll see what Clojure needs to have in place.
To set up the system, we first have to configure R to talk to Clojure:
Rserve
. In my case, I went to http://www.rforge.net/Rserve/files/ and downloaded Rserver_1.88-1.tar.gz
, but you might have a more recent version available.$ tar xfzv Rserve_1.88-1.tar.gz x Rserve/ x Rserve/configure.win x Rserve/cleanup …
src/client/java
subdirectory of the Rserve
directory:$ cd Rserve/src/client/java/
make
to create the JAR files:$ make
$ mvn install:install-file -Dfile=./REngine.jar -DartifactId=REngine -Dversion=1.88.1 -DgroupId=local.repo -Dpackaging=jar … $ mvn install:install-file -Dfile=./Rserve.jar -DartifactId=Rserve -Dversion=1.88.1 -DgroupId=local.repo -Dpackaging=jar …
> install.packages("Rserve") --- Please select a CRAN mirror for use in this session ---
At this point, a dialog window will pop up with a list of mirrors. Select one near you, and R will download the package and install it.
start-rserve.sh
, that starts the Rserver. You can also download the script from https://gist.github.com/erochest/4974005.$ ./start-rserve.sh R version 2.15.2 (2012-10-26) -- "Trick or Treat" ... Rserv started in daemon mode.
Now we need to turn our attention to Clojure.
project.clj
file:(defproject interop "0.1.0-SNAPSHOT" :description "" :dependencies [[org.clojure/clojure "1.6.0"] [incanter "1.5.5"] [local.repo/REngine "1.8.1"] [local.repo/Rserve "1.8.1"]])
(import '[org.rosuda.REngine REngine] '[org.rosuda.REngine.Rserve RConnection])
(def ^:dynamic *r-cxn* (RConnection.))
The Rserve package runs a server in the background where the Java library communicates with. From Clojure, we can now feed data to the Rserver and get results. We'll see examples of this in the following recipes.
Because Rserve has to be running, step 6 from the previous section (load and start the Rserver) has to be performed in every session you want to call R from Clojure.