Search code examples
rclojurerjavaclojure-java-interop

rJava from Clojure


I am trying to use R from Clojure with rJava as shown here. I have the following:

(ns prototype.core
  (:gen-class)
  (:require [prototype.dataGraph :as dg])
  (:import [org.rosuda.JRI Rengine]))

(defn -main
  "Program main entry point."
  [& args]
  (new Rengine (into-array String ["--no-save"]) false nil)
  (println "Hello."))

then, I get the stacktrace

Cannot find JRI native library! Please make sure that the JRI native library is in a directory listed in java.library.path.

java.lang.UnsatisfiedLinkError: no jri in java.library.path

So it seems I did not specify the lib location correctly...

And the Java constructor is of the form:

Rengine engine = new Rengine(new String[] { "--no-save" }, false, null);

My project.clj file:

(defproject prototype "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.8.0"]]
  :plugins [[lein-expand-resource-paths "0.0.1"]]
  :resource-paths ["/usr/lib/R/site-library/rJava/jri/JRI.jar" "/usr/lib/R/site-library/rJava/jri/JRIEngine.jar" "/usr/lib/R/site-library/rJava/jri/REngine.jar"]
  :main ^:skip-aot prototype.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

Can rJava be successfully used with Clojure?


Solution

  • Yeah, so the issue was that the shared lib was not correctly specified in the java.library.path. This works:

    (defproject prototype "0.1.0-SNAPSHOT"
      :description "FIXME: write description"
      :url "http://example.com/FIXME"
      :license {:name "Eclipse Public License"
                :url "http://www.eclipse.org/legal/epl-v10.html"}
      :dependencies [[org.clojure/clojure "1.8.0"]]
      :plugins [[lein-expand-resource-paths "0.0.1"]]
      :resource-paths ["/usr/lib/R/site-library/rJava/jri/JRI.jar" "/usr/lib/R/site-library/rJava/jri/JRIEngine.jar" "/usr/lib/R/site-library/rJava/jri/REngine.jar"]
      :jvm-opts [~(str "-Djava.library.path=/usr/lib/R/site-library/rJava/jri/:" (System/getenv "$LD_LIBRARY_PATH"))]
      :main ^:skip-aot prototype.core
      :target-path "target/%s"
      :profiles {:uberjar {:aot :all}})