2

I want to call some R code from within Java, on a Linux machine that has R installed already, so the Java/R Interface (JRI) is needed.

However, I am not sure how to go about this.

The JRI homepage says that the package is bundled in rJava. So, I downloaded rJava-0.9.3.tar.gz from the rJava Files section, and unzipped it, getting (among other things) a lot of Java source files (and their corresponding classes) in the src/java folder, plus 2 JARs (JRIEngine.jar and REngine.jar) in the inst/jri folder.

The JRI Files section, though, in addition to the above 2 JARs, also includes JRI.jar and all three of these libraries are more recent than the 2 in inst/jri.

In addition, none of these 3 JARs includes the binaries for the classes found in src/java, which appear to be necessary for calling R from within Java.

Running "make all" in the java/src folder seems to just execute some tests and no JAR is produced.

I would expect that all necessary classes come bundled in JAR files, so that they could be easily added in the classpath and in IDE projects (e.g., in Eclipse).

How can I get to that point?

PNS
  • 19,295
  • 32
  • 96
  • 143

1 Answers1

4

Use install.packages("rJava") in R - that will install rJava which includes JRI. You'll get the same effect by using R CMD INSTALL rJava_0.9-3.tar.gz. Both are the documented ways to install R packages. Make sure you have JDK installed and configured R with Java support before. Once installed, you can ask R about the location of JRI with system.file("jri",package="rJava")

Note that in order to use JRI you will need to setup the environment properly (typically using R CMD) and set java.library.path to include the JRI location. You may want to scan the stats-rosuda-devel mailing list which is the canonical place to ask about rJava/JRI.

Simon Urbanek
  • 13,842
  • 45
  • 45
  • Thanks for the answer. I read about the install.packages() command but was puzzled as to how I would make the result work with Eclipse. I have now run it and added a User Library in Eclipse that points to the 3 JAR files installed. Given that Eclipse takes care of Java-related path declarations, should that work out of the box? – PNS Mar 01 '12 at 00:45
  • 1
    No, JAR files alone are not enough - you have to make sure you have `java.library.path` setup to find the JRI dynamic library and also set environment variables required by R such as `R_HOME`. I don't use Eclipse so I don't know how it handles those settings. You can have a look at the `run` script and the JRI examples. – Simon Urbanek Mar 01 '12 at 03:09
  • Indeed. R_HOME can be set in the Project > Preferences > Run/Debug Settings, whereas for the ld.library.paht one can edit the "Native library" setting in each of the 3 JARs of JRI (as per this StackOverflow post: http://stackoverflow.com/questions/957700/how-to-set-the-java-library-path-from-eclipse). – PNS Mar 02 '12 at 00:47
  • Two helpful posts on setting up rJava and JRI in Eclipse: http://stackoverflow.com/questions/3311940/r-rjava-package-install-failing and http://binfalse.de/2011/02/talking-r-through-java/ – PNS Mar 02 '12 at 00:50