Java and R call each other

Source: Internet
Author: User

https://www.r-project.org/

http://cos.name/2013/08/r-rjava-java/

http://blog.csdn.net/hwssg/article/details/39177959

Directory

    1. Rjava Introduction
    2. Rjava Installation
    3. Rjava implementing R calls to Java
    4. Rjava (Jri) implements Java call R (Win7)
    5. Rjava (Jri) implements Java call R (Ubuntu)
1. Rjava Introduction

Rjava is a communication interface between the R language and the Java language, implemented by the underlying JNI implementation, allowing direct invocation of Java objects and methods in R.

Rjava also provides the functionality of Java invoke R, which is implemented through Jri (JAVA/R Interface). Jri is now embedded in the Rjava package, and we can also try this feature individually. Now Rjava package, has become a lot of Java development based on the basic functional components of the R package.

Formally because Rjava is the underlying interface and is invoked using JNI as an interface, it is very efficient. In the Jri scenario, the JVM directly loads RVM directly through memory, and the call process performance is virtually lossless, making it a very efficient connection channel and a preferred development package for R and Java communications.

2. Rjava Installation

Rjava Installation

  #配置rJava环境 ~ sudo R CMD javareconf  #启动R ~ sudo r>  install.packages (   " Rjava ")  installing via  ' INSTALL.LIBS.R ' To/usr/ local/lib/r/site-library/rjava** r** inst** Preparing package for lazy loading** help*** installing help indices** Building Package indices** Testing Span class= "Hljs-keyword" >if installed package can is loaded* done (Rjava) the downloaded  Source packages is in '/tmp/rtmpizyce7/downloaded_packages ' 
Black as the main operating procedures, to note that I installed due to insufficient software permissions, so the Rjava package failed to install to the R home directory, while the download package is stored in another location, need to remember, and then extract the download package to the location of the Rjava package of their choice.

3. Rjava Implement R call Java

In the R environment, programming with the Rjava package

#加载rJava包 > Library (Rjava) > Search () [1]". Globalenv ""Package:rjava""Package:stats" [4]"Package:graphics""Package:grdevices""Package:utils" [7]"Package:datasets""Package:methods""Autoloads" [10]"Package:base"#启动JVM >. Jinit ()#声明并赋值到字符串 > S <-jnew ("java/lang/string", "Hello world!") [1]"Java-object{hello world!}"#查看字符串长度 >. Jcall (s),"I","Length") [1]12#索引World的位置 >. Jcall (s),"I", "IndexOf",  "world") [1] 6 #查看concat的方法声明 > jmethods (s, "concat ") [1] " public java.lang.String Java.lang.String.concat ( java.lang.String) " #使用concat方法连接字符串 > Jcall (s," concat ", s) [1]  "Hello world! Hello world! "  #打印字符串对象 > Print (s) [1]  " Java-object{hello world!} "  #打印字符串值 > Jstrval (s) [1]                

Rjava an optimized method call, using $ to invoke a method

#同.jcall(s,"I","length")> s$length()[1] 12#同.jcall(s,"I","indexOf","World")> s$indexOf("World")[1] 6



4. Rjava (Jri) implements Java call R (Win7)

Installing Rjava in Win7

System environment:

    • Win7 64bit flagship edition
    • R 3.0.1
    • Java 1.6.0_45

Setting environment variables

PATH: C:\Program files\r\r-3.0.1\bin\x64;d:\toolkit\java\jdk6\bin;d:\toolkit\java\jdk6\jre\bin\server

Java_home: D:\toolkit\java\jdk6

CLASSPATH: C:\Program Files\r\r-3.0.1\library\rjava\jri

Classpath=d:\prosoftware\r-2.15.0\library\rjava\jri;
Path=d:\prosoftware\r-2.15.0\bin\i386
R_home=d:\prosoftware\r-2.15.0\r-2.15.0\

It is important to note that if the Rjava package in the previous step is not in the default location, the above environment variables need to be modified accordingly. The above two methods, the first measurement is feasible, the second one should also have little problem.

Copy the three jar packages (Jriengine.jar,jri.jar,rengine.jar) under D:\proSoftware\R-2.15.0\library\rJava\jri to C:\Program files\java\ Jdk1.6.0_10\lib directory, that is, the JDK directory under the Lib

Open Eclipse and create Java project. The above three packages are also imported into the project.

The D:\proSoftware\R-2.15.0\library\rJava\jri\examples directory has two self-test codes Rtest.java and Rtest2.java.

Run both code, if the result is not an error, the call succeeds.

Before running, remember to add the VM parameter set to-djava.library.path=d:\prosoftware\r-2.15.0\library\rjava\jri in run configurations

In addition, after the package is imported, after all set up, if the program error, you can restart Eclipse, and then try again.

You can also use the following code to test

[1] "Java-Object{Hello World!}"

Start Eclipse Authoring Program

Package Org.conan.r.rjava;import Org.rosuda.JRI.Rengine;PublicClassDemorjava {PublicStaticvoidMainString[] args) {Demorjava demo =New Demorjava (); Demo.callrjava (); }PublicvoidCallrjava () {Rengine re =new rengine (new string[] {false, null); if (!re.waitforr ()) {System. Out.println ( "Cannot load R"); return;} //print variable String version = Re.eval ( "r.version.string"). Asstring ( ); System. out.println (version); //loop print group double[] arr = re.eval ( " Rnorm "). Asdoublearray (); for (double A:arr) {system. Out.print (A +  

Set VM parameters at eclipse Startup: Under Run, run configuration

-Djava.library.path="C:\Program Files\R\R-3.0.1\library\rJava\jri\x64"

Operation Result:

3.0.1 (2013-05-16)0.04051018703700011,-0.3321596519938258,0.45642459001166913,-1.1907153494936031,1.5872266854172385,1.3639721994863943,-0.6309712627586983,-1.5226698569087498,-1.0416402147174952,0.4864034017637044,

Packing Demorjava.jar
Complete the package in Eclipse, upload to the Linux environment, and continue testing.

5. Rjava (Jri) implements Java call R (Ubuntu)

New catalog Demorjava, uploading Demorjava.jar to Demorjava

1 conan conan 1328 Aug  8  2013 DemoRJava.jar

Run the jar package

export R_HOME=/usr/lib/R~ java -Djava.library.path=/usr/local/lib/R/site-library/rJava/jri -cp /usr/local/lib/R/site-library/rJava/jri/JRI.jar:/home/conan/R/DemoRJava/DemoRJava.jar org.conan.r.rjava.DemoRJava

Run results

3.0.1 (2013-05-16)0.6374494596732511,1.3413824702002808,0.04573045670001342,-0.6885617932810327,0.14970067632722675,-0.3989493870007832,-0.6148250252955993,0.40132038323714453,-0.5385260423222166,0.3459850956295771,

We have finished, R and Java intermodulation. Includes R called through Rjava call Java,java through Jri call R. and demonstrates how to use it in Win and Linux.

 

Java and R call each other

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.