R and Java


In this section we'll set up an Eclipse project to work with rJava, the R-Java bridge library.


First, we need to download the rJava library.

In general, installing libraries in R is very simple. However, because R on our system runs on the network, and we don't have access to the libraries directly, we're going to run through it old-skool -- which is, again, no bad skill to have. The chief reason for our doing it this way is that we're going to need to unzip the library files, which isn't usual, and we can't do on the network version.


First, we need to download the rJava library manually. Go to the rJava CRAN page and download the Windows binary (release version). Put it somewhere you can get at it and unzip it. We'll assume you put it in the same directory structure as the r-scripts you were working on, but in the following directory: M:\r-projects\libs\ such that when you unzip it, all the contained directories ("help"; "html"; "java" etc.) are directly in the directory M:\r-projects\libs\rJava.


Note: if you ever need to install libraries downloaded in this manner in R, you can do so using the following command:

library("rJava",lib.loc="m:\\r-projects\\libs\\")

On our system, however, rJava is already installed. We just need access to the zip files.


Now we'll build our Eclipse project.


Now open up Eclipse, and make yourself a new java project. Make sure you select the 1.7 version of the JVM. Make a new main class called Stats.java. Add in the following jars (here rJava is the directory you've unzipped rJava to):

rJava\jri\JRI.jar
rJava\jri\JRIEngine.jar
rJava\jri\REngine.jar

This is a good start, but these libraries communicate with the code that makes up R, so they can't just use java files. Infact the java in these jars actually communicates with R through some native code dlls. There are various ways to make sure the JVM can find these, however, the easiest in Eclipse is to make sure the location of these are added to the computer's PATH environment variable. Eclipse will add this to a variable the JVM uses called java.library.path. While we're at it, we also need to set up a new environment variable called R_HOME so the JVM can find R.

To set these elements up, you need to right-click on your new project in the package explorer and select Properties. Click in Run/Debug Settings. This area allows you to set up profiles containing different run options. Click the New... button, Java Application, and OK. Fill in the following under the various tabs:

1) Add in the name of the main class you created above Stats (without the .java) in the Main class box of the Main tab.

2) Have a look at the (x)=Arguments tab - we're not going to add anything here now, but you could use this to set up arguments like the classpath for the java compiler, or arguments to pass into the program's String[] args.

3) The source tab does actually allow you to add in native dlls, but we're not going to do this as it is a bit flakey. The other place you can do this is within the build-path dialog, but this only allows one dll, and we need a couple, so setting the PATH is the best option for us.

4) Under the Environment tab, add the following variables and values using the New button:

Name: PATH
Value: L:\R\R-3.1.0\bin\i386;m:\r-projects\libs\rJava\libs\i386;m:\r-projects\libs\rJava\jri\i386;

Name: R_HOME
Value: L:\R\R-3.1.0\

Obviously you'll want to change the paths to whatever you need for your install. Make sure Append environment to native environment is checked on before pushing OK.


Once you've done that lot, Eclipse should be able to find everything. You may well wonder how you'd do all this from the command line. The best thing is to put it all in a DOS BAT (Batch) file in the same directory as your java files, and then run it from the command line just by typing the bat filename. Here's an example file: run.bat (note the use of -Djava.library.path to set the library path). You'd run it by saving it to your .java file directory, moving to this using the command prompt, and then typing run and pushing Enter/Return. It sets the environment variables, compiles the file/s and runs a Stats.java main-class file.


Having got that set up, go on to Part Three where we'll actually do some coding.