Talking R through Java

[This article was first published on binfalse » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Today I played a bit with JRI as part of rJava, a Java-R-interface. Here you can learn how to setup for Debian/Ubuntu/akins.

Installation

Assuming you have a running version of Java and GNU’s R, you have to install r-cran-rjava:

1
aptitude install r-cran-rjava

Shell environment

To talk to R through Java you have to specify three more environmental variables. First of all you need to publish you R installation path, my R is found in /usr/lib64/R:

1
export R_HOME=/usr/lib64/R

If you didn’t or the path is wrong you’ll fall into trouble:

1
R_HOME is not set. Please set all required environment variables before running this program.

Second the $CLASSPATH needs to get an update. Precisely you have to add the archives JRIEngine.jar, JRI.jar and REngine.jar. In my case all of them can be found in /usr/lib/R/site-library/rJava/jri/, so the $CLASSPATH should be set like that:

1
export CLASSPATH=.:/usr/lib/R/site-library/rJava/jri/

If the $CLASSPATH isn’t defined correctly you won’t be able to compile your Java code.

Last but not least you have to add the native JRI-library to your $LD_LIBRARY_PATH, by default this lib is located in the same directory like the jar’s:

1
export LD_LIBRARY_PATH=/usr/lib/R/site-library/rJava/jri/

If the $LD_LIBRARY_PATH isn’t proper you’ll experience errors like this:

1
2
3
4
5
6
7
8
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
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
        at java.lang.Runtime.loadLibrary0(Runtime.java:823)
        at java.lang.System.loadLibrary(System.java:1028)
        at org.rosuda.JRI.Rengine.<clinit>(Rengine.java:19)

To not always do the same you might write these export stuff to your .bashrc or .zshrc respectively.

Eclipse setup

Of course in Eclipse you’ll also have to define these three things.
Where are the jar’s located? Add them to your libraries in Project > Properties > Java Build Path > Libraries.
Instead of the $LD_LIBRARY_PATH you can set the java.library.path in Run > Run Configurations > Arguments. Add -Djava.library.path=.:/usr/lib/R/site-library/rJava/jri/ to the VM arguments (modify the path to match your criteria).
The R_HOME can be published in Run > Run Configurations > Environment. Create a new variable with the name R_HOME and the value /usr/lib64/R (or an equivalent path).
That’s it, see the section above to identify what went wrong if something fails.

Netbeans setup

Two of these three parts are also straight forward in Netbeans.
First publish the location of the jar’s. Right-click on your project and choose Properties > Libraries. In the Compile-tab click Add JAR/Folder and search for the jar files.
Next task is to adjust the library-path. Right-click on your project and choose Properties > Run. Add -Djava.library.path=.:/usr/lib/R/site-library/rJava/jri/ to the VM Options (modify the path to match your criteria).
The third step is a little tricky. As far as I know there is no way to change the environment from within Netbeans, so you can’t create the variable R_HOME after Netbeans is started. In my opinion you have two options:

  1. Export the variable before starting Netbeans:
    1
    2
    usr@srv $ export R_HOME=/usr/lib64/R
    usr@srv $ netbeans

    you might want to write a wrapper script that does this step for you, or include the export in any of the resource files that are called before Netbeans starts (e.g. your .bashrc).

  2. Change the environment from within your project. At stackoverflow you can find a workaround, but I think this is a very lousy solution..

If you have further suggestions please let me know!

Testcase

If you defined your environment properly, you should be able to utilize the REngine. I have a small script for you to test whether all things are fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package de.binfalse.martin;

import org.rosuda.JRI.Rengine;

public class JRItest
{
    public static void main (String[] args)
    {
        // new R-engine
        Rengine re=new Rengine (new String [] {“–vanilla”}, false, null);
        if (!re.waitForR())
        {
            System.out.println (“Cannot load R”);
            return;
        }
       
        // print a random number from uniform distribution
        System.out.println (re.eval (“runif(1)”).asDouble ());
       
        // done…
        re.end();
    }
   
}

You should be able to compile and run it, afterwards you’ll see a random number from an uniform distribution. Congratulations, well done 😛

For more information see the JRI and rJava sites at RForge.net.

Download:
Java: JRItest.java
(Please take a look at the man-page)

To leave a comment for the author, please follow the link and comment on their blog: binfalse » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)