Setting up R to talk to Clojure

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.

Getting ready

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.

How to do it…

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.

Setting up R

To set up the system, we first have to configure R to talk to Clojure:

  1. Once R is installed, we'll download the interoperability package, 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.
  2. You'll need to extract the files from the tarball that you've downloaded. On Windows, 7-Zip (http://www.7-zip.org/) can help:
    $ tar xfzv Rserve_1.88-1.tar.gz
    x Rserve/
    x Rserve/configure.win
    x Rserve/cleanup
    …
  3. Change into the src/client/java subdirectory of the Rserve directory:
    $ cd Rserve/src/client/java/
  4. Run make to create the JAR files:
    $ make
  5. Use Maven to install the two JAR files in that directory into your local Maven repository, as shown here:
    $ 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
    …
  6. Start R and install the package, as follows:
    > 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.

  7. While still in R, load and start the Rserver. The code for this chapter has a shell script, 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.

Setting up Clojure

Now we need to turn our attention to Clojure.

  1. First, we must declare the two JAR files that we just installed as dependencies in our Leiningen 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"]])
  2. Then we'll import a couple of namespaces from them into our script or REPL:
    (import '[org.rosuda.REngine REngine]
            '[org.rosuda.REngine.Rserve RConnection])
  3. Finally, we create a connection to the R server. This will be a dynamic variable, so we can swap it out easily later. If the Rserve isn't running, this step will fail, so make sure that you have the Rserver running before you execute this (see step 6 from the previous section), and if this succeeds, you have R and Clojure successfully communicating:
    (def ^:dynamic *r-cxn* (RConnection.))

How it works…

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.37.20