Using R Script in Java
หน้า 1 จาก 1
Using R Script in Java
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. This article will provide technique how Java calling and using data from R.
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.
10. Result from Java printout
For RCaller need to download library from http://code.google.com/p/rcaller/
Then add library to the project.
Some more RCaller code example https://code.google.com/r/ring801112-rcaller/source/browse/RCaller/src/main/java/examples/?r=0149fd675061a1941b6ae9f64d55947a9b3d54f8
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. This article will provide technique how Java calling and using data from R.
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.
- Code:
package jri.demo;
import java.util.Enumeration;
import org.rosuda.JRI.Rengine;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.RList;
import org.rosuda.JRI.RVector;
/**
* @author psupawa@gmail.com
*/
public class TestJRI {
public static void main(String[] args) {
new TestJRI();
}
private Rengine re;
private REXP result;
public TestJRI() {
// Checking for the right version
if (!Rengine.versionCheck()) {
System.err.println("Error: 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
re.eval(String.format("greeting <- '%s'", "Hello R World"));
result = re.eval("greeting");
System.out.println("Greeting from R: "+result.asString());
result = re.eval("x <- c(1,2,3,8,9)");
System.out.println("Dataset = "+result);
result=re.eval("mean(x)");
System.out.println("mean = "+result.asDouble());
System.out.println("mean+1.5 = "+(result.asDouble()+1.5));
re.end();
System.out.println("Rengine closed");
}
}
10. Result from Java printout
- Code:
Creating Rengine
Rengine created
R is ready for use
Greeting from R: Hello R World
Dataset = [REAL* (1.0, 2.0, 3.0, 8.0, 9.0)]
mean = 4.6
mean+1.5 = 6.1
Rengine closed
For RCaller need to download library from http://code.google.com/p/rcaller/
Then add library to the project.
- Code:
package jri.demo;
import rcaller.RCaller;
import rcaller.RCode;
/**
* @author psupawa@gmail.com
*
*/
public class TestRCaller {
public static void main(String[] args) {
new TestRCaller();
}
public TestRCaller() {
try {
RCaller caller = new RCaller();
caller.setRscriptExecutable("C:/Program Files/R/R-3.1.0/bin/x64/Rscript.exe");
caller.cleanRCode();
RCode code = caller.getRCode();
code.clear();
code.addRCode("x <- c(1,2,3,8,9)");
code.addRCode("m <- mean(x)");
caller.setRCode(code);
caller.runAndReturnResult("m");
double[] mean = caller.getParser().getAsDoubleArray("m");
System.out.println("mean = "+mean[0]);
System.out.println("Complete");
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
Some more RCaller code example https://code.google.com/r/ring801112-rcaller/source/browse/RCaller/src/main/java/examples/?r=0149fd675061a1941b6ae9f64d55947a9b3d54f8
หน้า 1 จาก 1
Permissions in this forum:
คุณไม่สามารถพิมพ์ตอบ
|
|