Talking R through Java
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:
If the $LD_LIBRARY_PATH
isn’t proper you’ll experience errors like this:
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:
- 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
).
- 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
For more information see the JRI and rJava sites at RForge.net.
Leave a comment
There are multiple options to leave a comment:
- send me an email
- submit a comment through the feedback page (anonymously via TOR)
- Fork this repo at GitHub, add your comment to the _data/comments directory and send me a pull request
- Fill the following form and Staticman will automagically create a pull request for you:
28 comments
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? ThanksHi Sergio, loading libraries works at least for me:
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 theJRI.jar
version you are using? I have not got theparseAndEval
method. Thanks againOk, 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,
it’s hard for me to guess what’s wrong. Please send me your code and I’ll try to help you.
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:
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:
Hi, is there a way to get error messages from R back?
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.
java.lang.UnsatisfiedLinkError: no jri in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1709) at java.lang.Runtime.loadLibrary0(Runtime.java:823) at java.lang.System.loadLibrary(System.java:1030) at org.rosuda.JRI.Rengine.(Rengine.java:9) at com.test.maintest.main(maintest.java:9)
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 thatjri.dll
coulnd’nt be found.now have done this:
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 :i don t find any solution on internet! please help me! thanks a lot
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.
ok, me again, sorry for mass posting… i solved previous issue. I updated everything (JRI and R) and it worked. Updating only JRI won’t do the trick. Anyway…. Cheers :)
Hi Irene, thank you very much for coming back with the fix! :)