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 :

## 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 :

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

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:

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:

## 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:

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 ).

1. 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! Meanwhile George Bull published a setup guide for Netbeans on Windows hosts. Seems to be worthy to take a look at it ;-)

## 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:

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

You can actively support me!

greetings, I have a problem big problem, when I try to execute those steps, all work fine until the IDE configuration, I explain me better, I had installed Rjava, I had copied JAR requested, I had updated the environment variables in /etc/environment and until there all works configurate correctly, I work in Netbeans but I didn’t know if I had to do any configuration in the IDE. Finally when I run the program, it appear this “cannot find system Renviron”. Really I had tried all the possible things but I couldn’t fix it. Sorry for my english, and I will appreciate any help that you can give to me for understand why is the reason of my problem. Thanks

Hi Sebastian,

I added a Netbeans setup section to the article. I hope that solves some of your problems ;-)

Kind regards, Martin

Really, thank you so much for your help, it will be so useful, now I will follow with my project.

Greetings, Now my Java-R connection works, I can make some calculus. But I do not know how I can use R’s libraries like RODBC through Java, I tried with: re.eval("library(RODBC)") , that did not work. Can you help me to use this library? How can I test the library is actually loaded? Thanks

re.eval ("library(datasets)"); System.out.println (re.eval ("beaver1[1,1]").asDouble ());

Maybe your library can’t be found by R itself? Where is it located? Could you please tell me the thrown error!? Maybe changing the R library search path might help you:

re.parseAndEval(".libPaths(/DIR/OF/YOUR/LIB/)");

When I execute within R the command library(RODBC) it works. I just see I have /usr/local/lib/R/site-library/RODBC/libs/RODBC.so (I suppose that is the library is required). What is the JRI.jar version you are using? I have not got the parseAndEval method. Thanks again

Ok, sry, I just copied&pasted the code from another project. Unfortunately I’m not using org.rosuda.JRI.Rengine at this point. Sorry for that. Let’s continue this conversation via mail ;-)

OK, I will appreciate that. But I am interested in knowing about which jar library you are using, I may change it. Can you know my mail or I would publish it?

If you provided a correct mail address you should find my mail in your inbox. Otherwise choose one of these methods to contact me: {{ site.url }}/contact/ I’m sure we’ll fix this issue ;-)

Btw. I’m currently using 0.9-1-1 from Debian unstable. With this version the following should work:

re.eval(".libPaths(/DIR/OF/YOUR/LIB/)");

But let’s discuss via mail. I’ll afterwards post a summary here.

Greetings,

I am trying to use JRI to programming JAve with R. I am testing a simple program but i have a problem with the REngine constructor. The program exit with the value 10, without exception. I have followed the steps before but i cant fix the problem.

Thanks.

Hi Santiago,

Is it possible to invoke r functions or embedding r charts inside jasper’s ireport

It really help. Thank you so much.

hi, i hv the problem for runing this what mean Export the variable before starting Netbeans:

usr@srv $export R_HOME=/usr/lib64/R usr@srv$ netbeans


write at where?

actually my task is to write a java programing that will produce few value. Then i need to pass that value to R. then R will do some process eg sum the value with one. last pass it back to java. How can it work???

Hi sanky, unfortunately I don’t know jasper and so I don’t have answers to such specific questions, please try to ask for support at jasper.

Hi jesslim,

if you’re working on Linux/Unix just open a terminal and export the variable with

export R_HOME=/usr/lib64/R

Afterwards you can start Netbeans in the same session with the command

netbeans

If you’re using Windows I’m sorry, but I’m not able to help you. I’m sure there is some possibility to set this variable in your user-profile, but I recommend to ask someone who is more familiar with Windows ;-)

Kind regards, Martin

The reply is late. I am trying to investigate the best way to run simulations on my socket server with varying distributions of bytes. It looks like r-java can do it instead of expensive load runners.

Do you know of anyone who is doing it ?I asked the r-help list but they disregard generic questions like this.

Dear George, The post of Using JRI , R with NetBeans in Windows was an excellent guide. I am using Eclipse IDE and trying to call R via a Java program using JRI. I have gone through your post and tried to replicate the same thing in Eclipse IDE. I am yet to get a successful output out of the java program (TestR.java). Here is what I did:

1. Set R_Home variable => R_HOME = D:\Program Files\R\R-2.14.0
2. Set PATH variable => PATH = D:\Program Files\R\R-2.14.0\bin\x64;(contains R.dll) D:\Program Files\R\R-2.14.0\library\rJava\jri; (containsJRI.dll,JRI.jar,JRIEngine, REngine), D:\Program Files\R\R-2.14.0\library\rJava\jri\x64 (contains JRI.dll)
3. Added -Djava.library.path=D:\Program Files\R\R-2.14.0\library\rJava\jri\ to the VM arguments, and I executed the program and it gave the following error: “Could not find the main class: Files\R\R-2.14.0\library\rJava\jri. Program will exit. “

Hi, is there a way to get error messages from R back?

• CODE EXAMPLE: Rengine rEngine = new Rengine(new String [] {“–vanilla”}, false, null); rEngine.eval(“library(xts)”); rEngine.assign(“arrayValue”, DOUBLE DATA); rEngine.assign(“arrayDates”, DOUBLE DATA); rEngine.eval(“test = xts(arrayValue, order.by=arrayDates)”);

The last statement not only takes 1 minute to run, it also seems to fail (no test variable is created) -> Is there a way to get some log information?

Thank you and best regards!

Hi,

I’m trying to do this in DrJava but I believe I’m getting stuck with setting the R_HOME environmental variable in DrJava. Does anybody know how to do this?

Thanks

There’s an eclipse plugin that helps set up the R Java environment at http://www.studytrails.com/RJava-Eclipse-Plugin/.

This is not working in my windows machine. It throws exception

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

How can i solve it.My R global path is also right.

Unfortunately I’m at war with Windows. Thus, the only advise I have for you is switching to a proper operation system ;-) However, I hope you can fix this issue. If you found a solution please post it to help other Windows users.

@Swarga Bera For windows operating system, I would suggest to check on the environment variable of the windows operating system. I believe there is something called PATH and jri.dll should be searchable by the environment variable.

after changing the environment variable, do not forget to restart the machine.

hello

i m using this configuration but when i m launching R its stop immediatly with exit code 2

i have set up -Djava.library.path correctly because before setup this, i have the error that jri.dll coulnd’nt be found.

now have done this:

Rengine.DEBUG = 5;

System.out.println("Starting Rengine..");
System.out.println("R_HOME =" + System.getenv("R_HOME"));
System.out.println("java.library.path =" + System.getProperty("java.library.path"));
Rengine re = new Rengine(new String[] { "--vanilla" }, false, null);
if (!re.waitForR()) {
return;
}


and when i m running the programm, on line Rengine re = new Rengine(new String[] { "--vanilla" }, false, null); it stop with exit code 2, in the console outpu i have :

Starting Rengine.. R_HOME =D:\Users\pmancaux\Desktop\Dev\R\GNU_R\R-3.0.2\bin
java.library.path =D:\Users\pmancaux\Desktop\Dev\R\GNU_R\R-3.0.2\library\rJava\jri
Starting R...


Palex

same problem as Palex here…. everything used to work perfectly fine and now application crashes when i set up the Rengine (Rengine re=new Rengine(args, false, new TextConsole()); but i’ve used all syntax possible…)

no idea… its driving me nuts….

any help is dearly appreciated.