Calling R Script in Java


You can view this article in forum, click this link Using R in Java

This article will provide technique how Java calling and using data from R.

R is a language and environment for statistical computing and graphics. It is a GNU project which is similar to the S language and environment which was developed at Bell Laboratories (formerly AT&T, now Lucent Technologies) by John Chambers and colleagues. R can be considered as a different implementation of S. There are some important differences, but much code written for S runs unaltered under R.

R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, ...) and graphical techniques, and is highly extensible. The S language is often the vehicle of choice for research in statistical methodology, and R provides an Open Source route to participation in that activity.

One of R's strengths is the ease with which well-designed publication-quality plots can be produced, including mathematical symbols and formula where needed. Great care has been taken over the defaults for the minor design choices in graphics, but the user retains full control.

R is available as Free Software under the terms of the Free Software Foundation's GNU General Public License in source code form. It compiles and runs on a wide variety of UNIX platforms and similar systems (including FreeBSD and Linux), Windows and MacOS.

R script can be called from other programming tools, Java, VB, C#, etc.

1. Download and install R. (from http://www.r-project.org/)

2. Set environment R_HOME

3. Download JRI library. (from http://www.rforge.net/JRI/)

4. Open Java IDE. (I use Eclipse)

5. Create new Java project

6. Select JRE. (I use 64 bit)

7. Add JRI library to the Java project

8. Copy DLL to Java project folder (depend on JRE 32 or 64 bit)

This is source folder

This is Java project folder

Note: Someone may be able to set the IDE to call the DLL from the source folder.

9. Now you are ready to use R script in Java.

// Checking for the right version

if (!Rengine.versionCheck()) {

System.err.println("** Version mismatch - Java files don't match library version.");

System.exit(0);

}

System.out.println("Creating Rengine");

// Initial the Rengine

// 1) Set the the arguments from the command line as null

// 2) I don't use the main loop at first, so that I pass the second argument as "false"

// 3) I don't require call back, but will process them in Java command.

re = new Rengine(null, false, null);

System.out.println("Rengine created");

// Waiting until R start ready

if (!re.waitForR()) {

System.out.println("Cannot load R");

return;

}

System.out.println("R is ready for use");

// You are ready to run R script now

You can see full source code in forum, click this link Using R in Java

หมายเลขบันทึก: 569784เขียนเมื่อ 4 มิถุนายน 2014 18:10 น. ()แก้ไขเมื่อ 5 มิถุนายน 2014 08:39 น. ()สัญญาอนุญาต: ครีเอทีฟคอมมอนส์แบบ แสดงที่มา-ไม่ใช้เพื่อการค้า-ไม่ดัดแปลงจำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (0)

ไม่มีความเห็น

อนุญาตให้แสดงความเห็นได้เฉพาะสมาชิก
พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท