今天实现了JRI方式–Java调用R方法-不用预先在R客户端启动R就可以实现连接调用
JRI,全名是Java/R Interface,这是一种完全不同的方式,通过调用R的动态链接库从而利用R中的函数等。
需要在install.packages(“rJava”)安装rJava就行,在安装文件夹中,可以看到一个jri的子文件夹,里面有自带的例子可以用来测试。
配置好之后呢,在library里找到测试Java文件,然后在eclipse里创建一个Java工程,测一下,如下:
eclipse代码测试结果如下:
如有错误,多是环境变量配置的问题;检查jdk和r,,rJava环境变量;
修改后重启eclipse再测;出错如下,第一个是环境变量,第二个是Java,r,rJava要么都是32位的,要么都是64位的,须一致;
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: D:\software\myR\R-3.1.2\library\rJava\jri\jri.dll: Can’t find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1965)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1890)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1880)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at org.rosuda.JRI.Rengine.(Rengine.java:19)
at com.jri.testDemo.rtest.main(rtest.java:62)
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: D:\software\myR\R-3.1.2\library\rJava\jri\jri.dll: %1 不是有效的 Win32 应用程序。
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1965)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1890)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1880)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at org.rosuda.JRI.Rengine.(Rengine.java:19)
at com.jri.testDemo.rtest.main(rtest.java:62)
接下来附录rtest.java文件源码注释,里面提到了Java操作R的一些API
public class rtest {
public static void main(String[] args) {
// just making sure we have the right version of everything
//确定R的版本
if (!Rengine.versionCheck()) {
System.err.println("** Version mismatch - Java files don't match library version.");//
System.exit(1);
}
System.out.println("Creating Rengine (with arguments)");
// 1) we pass the arguments from the command line //命令行处理args
// 2) we won't use the main loop at first, we'll start it later
// (that's the "false" as second argument)//第二个参数是false表示不使用main循环
// 3) the callbacks are implemented by the TextConsole class above
//创建R引擎对象,看底层是通过反射方式创建的对象
Rengine re = new Rengine(args, false, new TextConsole());
//new TestConsole()成功会打印R欢迎信息
/**
* R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"
Copyright (C) 2014 The R Foundation for Statistical Computing
R is free software and comes with ABSOLUTELY NO WARRANTY.
......
Type 'q()' to quit R.
*/
System.out.println("Rengine created, waiting for R");
// the engine creates R is a new thread, so we should wait until it's ready
// 新的线程,需要等等它准备好
if (!re.waitForR()) {
System.out.println("Cannot load R");//如果等待超时,报错: 不能加载R
return;
}
/*
* High-level API - do not use RNI methods unless there is no other way
* to accomplish what you want
*/
try {
REXP x;
re.eval("data(iris)", false);
System.out.println(x = re.eval("iris"));
//鸢尾花数据集 x=iris
// generic vectors are RVector to accomodate names
//产生R向量容纳iris数据集的字段名names 5列
RVector v = x.asVector();
if (v.getNames() != null) {
System.out.println("has names:");
for (Enumeration e = v.getNames().elements(); e.hasMoreElements();) {
System.out.println(e.nextElement());
}
}
// for compatibility with Rserve we allow casting of vectors to lists
// 通过 Rserve 把向量转成list集合
RList vl = x.asList();
String[] k = vl.keys();//[Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species]
if (k != null) {
//遍历集合,输出names
System.out.println("and once again from the list:");
int i = 0;
while (i < k.length)
System.out.println(k[i++]);
}
// get boolean array
//产生一个Boolean数组
System.out.println(x = re.eval("iris[[1]]>mean(iris[[1]])"));//[BOOLi* ]
// R knows about TRUE/FALSE/NA, so we cannot use boolean[] this way
// instead, we use int[] which is more convenient (and what R uses internally anyway)
//将R布尔数组转为int数组?R底层处理布尔用是的0,1?NA
int[] bi = x.asIntArray();
{
int i = 0;
while (i < bi.length) {
System.out.print(bi[i] == 0 ? "F " : (bi[i] == 1 ? "T " : "NA "));
i++;
}
System.out.println("");
}
// push a boolean array
boolean by[] = { true, false, false };
re.assign("bool", by);
System.out.println(x = re.eval("bool"));//[BOOLi* ]
// asBool returns the first element of the array as RBool //返回布尔数组的第一个元素作为RBool?
// (mostly useful for boolean arrays of the length 1). is should
// return true
System.out.println("isTRUE? " + x.asBool().isTRUE());
// now for a real dotted-pair list: 对于配对的数据,类似于key=value键值对形式数据
System.out.println(x = re.eval("pairlist(a=1,b='foo',c=1:5)"));
RList l = x.asList();
if (l != null) {
int i = 0;
String[] a = l.keys();//[a, b, c]所有的键
System.out.println("Keys:");
while (i < a.length)
System.out.println(a[i++]);
System.out.println("Contents:");//
i = 0;
while (i < a.length)
System.out.println(l.at(i++));//通过索引位置取值
//[REAL* (1.0)]
//[STRING "foo"]
//[INT* (1, 2, 3, 4, 5)]
}
System.out.println(re.eval("sqrt(36)"));//[REAL* (6.0)]
} catch (Exception e) {
System.out.println("EX:" + e);
e.printStackTrace();
}
// Part 2 - low-level API - for illustration purposes only!
// System.exit(0);
//第二部分--API -low level
// simple assignment like a<-"hello" (env=0 means use R_GlobalEnv)
//参数0表示 use R_GlobalEnv 当前环境,即用户环境,是用户程序运行的环境空间。
long xp1 = re.rniPutString("hello");
re.rniAssign("a", xp1, 0);//赋值,定义变量a<-'hello'
// Example: how to create a named list or data.frame创建list或数据框
double da[] = { 1.2, 2.3, 4.5 };
double db[] = { 1.4, 2.6, 4.2 };
long xp3 = re.rniPutDoubleArray(da);
long xp4 = re.rniPutDoubleArray(db);
// now build a list (generic vector is how that's called in R)
//R里的向量,Java里的list
long la[] = { xp3, xp4 };
long xp5 = re.rniPutVector(la);
// now let's add names 添加names
String sa[] = { "a", "b" };
long xp2 = re.rniPutStringArray(sa);
re.rniSetAttr(xp5, "names", xp2);
// ok, we have a proper list now 带属性的list
// we could use assign and then eval "b<-data.frame(b)", but for now
// let's build it by hand:
String rn[] = { "1", "2", "3" };
long xp7 = re.rniPutStringArray(rn);
re.rniSetAttr(xp5, "row.names", xp7);
long xp6 = re.rniPutString("data.frame");
re.rniSetAttr(xp5, "class", xp6);
// assign the whole thing to the "b" variable
re.rniAssign("b", xp5, 0);
{
System.out.println("Parsing");
long e = re.rniParse("data(iris)", 1);
System.out.println("Result = " + e + ", running eval");
long r = re.rniEval(e, 0);
System.out.println("Result = " + r + ", building REXP");//REXP result = [STRING "iris"]
REXP x = new REXP(re, r);
System.out.println("REXP result = " + x);
}
{
System.out.println("Parsing");
long e = re.rniParse("iris", 1);
System.out.println("Result = " + e + ", running eval");
long r = re.rniEval(e, 0);
System.out.println("Result = " + r + ", building REXP");
REXP x = new REXP(re, r);
System.out.println("REXP result = " + x);//REXP result = [VECTOR ([REAL* (5.1, 4.9, 4.7, 4.6...
double[] doubleArray = x.asDoubleArray();
}
{
System.out.println("Parsing");
long e = re.rniParse("names(iris)", 1);
System.out.println("Result = " + e + ", running eval");
long r = re.rniEval(e, 0);
System.out.println("Result = " + r + ", building REXP");
REXP x = new REXP(re, r);
System.out.println("REXP result = " + x);//[STRING* ("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species")]
String s[] = x.asStringArray();//[Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species]
if (s != null) {
int i = 0;
while (i < s.length) {
System.out.println("[" + i + "] \"" + s[i] + "\"");
i++;
}
}
}
{
System.out.println("Parsing");
long e = re.rniParse("rnorm(10)", 1);
System.out.println("Result = " + e + ", running eval");
long r = re.rniEval(e, 0);
System.out.println("Result = " + r + ", building REXP");
REXP x = new REXP(re, r);
System.out.println("REXP result = " + x);
double d[] = x.asDoubleArray();
if (d != null) {
int i = 0;
while (i < d.length) {
System.out.print(((i == 0) ? "" : ", ") + d[i]);
i++;
}
System.out.println("");
}
System.out.println("");
}
{
REXP x = re.eval("1:10");
System.out.println("REXP result = " + x);
int d[] = x.asIntArray();
if (d != null) {
int i = 0;
while (i < d.length) {
System.out.print(((i == 0) ? "" : ", ") + d[i]);
i++;
}
System.out.println("");
}
}
re.eval("print(1:10/3)");
if (true) {
// so far we used R as a computational slave without REPL
// now we start the loop, so the user can use the console
System.out.println("Now the console is yours ... have fun");
re.startMainLoop();
} else {
re.end();
System.out.println("end");
}
}
具体JRI连接参考链接如下:
http://blog.csdn.net/hongweigg/article/details/45223297