Talking R through Java
来源:互联网 发布:js循环数组赋值 编辑:程序博客网 时间:2024/06/05 20:16
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
:
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
:
export R_HOME=/usr/lib64/R
If you didn’t or the path is wrong you’ll fall into trouble:
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
andREngine.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:
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:
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:
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:
- Export the variable before starting Netbeans:
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
).
- 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 Windowshosts. 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:
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 :-P
For more information see the JRI and rJava sites at RForge.net.
- Talking R through Java
- Talking with mentor R
- java RMI communcate through firewall
- Sustainability through Energy Load Shaping for Buildings using R
- 暑假集训第三周 STL R - What Are You Talking About
- R java
- R.java
- Getting Flex 3 talking to Java via JSON
- Talking palce
- Google Talking
- Talking Calculator
- Generating Java classes dynamically through Java compiler API
- 丢失R.java文件
- JAVA Properties R/W
- JAVA notebook-by R
- 找不到R.java错误
- java /n /r /t
- R.java文件生成
- Mybatis源码分析(一)
- 分布式锁
- CDN网络访问原理
- SDK手动创建窗口
- STUN/TURN/ICE协议在P2P SIP中的应用(二)
- Talking R through Java
- 谷歌约束控件(ConstraintLayout)扁平化布局入门
- Spring注解和非注解实现声明时事务处理功能
- 编写一个程序,把较长的输入行“折”成短一些的多行,折行的位置在输入行的第N列之前的最后一个非空格之后
- halcon中的分类器之GMM算子全讲解
- Android系统中Bitmap是否有调用recycle方法的必要性
- CRC8 测试程序
- ModSecurity OWASP 规则集说明
- 将一个字符串转换为整数