Java calls R

Source: Internet
Author: User

Environment: Win7 32-bit jdk1.7 R 2.15.0

Java calls R there are three main ways, Rjava,rserver and Rcaller, the main talk here with Rjava call

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

Before using R3.0.2, do not know whether the version is too high reason,

The process of using Rjava to implement Java calls R, such as printing variables, can be successful
String Version = Re.eval ("r.version.string"). Asstring (); SYSTEM.OUT.PRINTLN (version);
The case of defining variables can also
Rexp x=re.eval ("1:10");
System.out.println ("rexp result =" +x);
Re.eval ("Print (1:10/3)");

But when it comes to invoking basic functions, it is the error of the general report null pointer.
Like what
Re.eval ("Rnorm (10)");
Double[] arr =re.eval ("Rnorm (Ten)"). Asdoublearray ();

Or in the test code that comes with it.
Rexp x;
Re.eval ("Data (Iris)", false);
System.out.println (X=re.eval ("Iris"));


To get to the point, first open R software,

Install.packages ("Rjava")

If the library (Rjava) does not make an error, it indicates that the first step was successful.

Start the JVM

. Jinit ()

S <-. Jnew ("java/lang/string", "Hello world!")

S

[1] "Java-object{hello world!}"


Then set the environment variable
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\


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






Java calls R

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.