解惑rJava R与Java的快速通道
阅读导读:
1.什么是RJava?
2.怎样安装RJava?
3.怎样用RJava实现R调用Java?
![](http://www.aboutyun.com/static/image/hrline/1.gif)
1. rJava介绍
rJava是一个R语言和Java语言的通信接口。通过底层JNI实现调用,同意在R中直接调用Java的对象和方法。
rJava还提供了Java调用R的功能,是通过JRI(Java/R Interface)实现的。
rJava安装
3.
rJava实现R调用Java
在R环境中。使用rJava包编程
rJava优化过的方法调用。用$来调用方法
4. rJava(JRI)实现Java调用R (win7)
在win7中安装rJava
系统环境:
在R中安装rJava
![](http://www.aboutyun.com/forum.php?mod=image&aid=9233&size=300x300&key=d985c6be2e5f416d&nocache=yes&type=fixnone)
在Eclipse启动设置VM參数:
![](http://www.aboutyun.com/forum.php?</p><p>mod=image&aid=9234&size=300x300&key=c3fb7cbb1d59469d&nocache=yes&type=fixnone)
执行结果:
打包DemoRJava.jar
在Eclipse中完毕打包,上传到linux环境,继续測试。
执行Jar包
执行结果
我们完毕了。R和Java的互调。
1.什么是RJava?
2.怎样安装RJava?
3.怎样用RJava实现R调用Java?
![](http://www.aboutyun.com/static/image/hrline/1.gif)
1. rJava介绍
rJava是一个R语言和Java语言的通信接口。通过底层JNI实现调用,同意在R中直接调用Java的对象和方法。
rJava还提供了Java调用R的功能,是通过JRI(Java/R Interface)实现的。
JRI如今已经被嵌入到rJava的包中,我们也能够单独试用这个功能。如今rJava包。已经成为非常多基于Java开发R包的基础功能组件。
正式因为rJava是底层接口,并使用JNI作为接口调用,所以效率很高。在JRI的方案中,JVM直接通过内存直接载入RVM。调用过程性能差点儿无损耗。因此是很高效连接通道,是R和Java通信的首选开发包。
2.
rJava安装
系统环境:
- Linux Ubuntu 12.04.2 LTS 64bit server
- R version 3.0.1 64bit
- Java (Oracle SUN) 1.6.0_29 64bit Server VM
~ uname -a
Linux conan 3.5.0-23-generic #35~precise1-Ubuntu SMP Fri Jan 25 17:13:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
~ cat /etc/issue
Ubuntu 12.04.2 LTS \n \l
~ R --version
R version 3.0.1 (2013-05-16) -- "Good Sport"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
http://www.gnu.org/licenses/.
~ java -version
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02, mixed mode)
#配置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 if installed package can be loaded
* DONE (rJava)
The downloaded source packages are in
‘/tmp/RtmpiZyCE7/downloaded_packages’
在R环境中。使用rJava包编程
#载入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!")
> s
[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,"Ljava/lang/String;","concat",s)
[1] "Hello World!Hello World!"
#打印字符串对象
> print(s)
[1] "Java-Object{Hello World!}"
#打印字符串值
> .jstrVal(s)
[1] "Hello World!"
#同.jcall(s,"I","length")
> s$length()
[1] 12
#同.jcall(s,"I","indexOf","World")
> s$indexOf("World")
[1] 6
在win7中安装rJava
系统环境:
- win7 64bit 旗舰版
- R 3.0.1
- Java 1.6.0_45
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
> install.packages("rJava")
#载入rJava
> library(rJava)
> .jinit()
#R调用Java变量測试
> s <- .jnew("java/lang/String", "Hello World!")
> s
[1] "Java-Object{Hello World!}"
启动Eclipse编写程
package org.conan.r.rjava;
import org.rosuda.JRI.Rengine;
public class DemoRJava {
public static void main(String[] args) {
DemoRJava demo = new DemoRJava();
demo.callRJava();
}
public void callRJava() {
Rengine re = new Rengine(new String[] { "--vanilla" }, false, null);
if (!re.waitForR()) {
System.out.println("Cannot load R");
return;
}
//打印变量
String version = re.eval("R.version.string").asString();
System.out.println(version);
//循环打印数组
double[] arr = re.eval("rnorm(10)").asDoubleArray();
for (double a : arr) {
System.out.print(a + ",");
}
re.end();
}
}
-Djava.library.path="C:\Program Files\R\R-3.0.1\library\rJava\jri\x64"
执行结果:
R version 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,
在Eclipse中完毕打包,上传到linux环境,继续測试。
5.
rJava(JRI)实现Java调用R (Ubuntu)
新建文件夹DemoRJava,上传DemoRJava.jar到DemoRJava
~ mkdir /home/conan/R/DemoRJava
~ cd /home/conan/R/DemoRJava
~ ls -l
-rw-r--r-- 1 conan conan 1328 Aug 8 2013 DemoRJava.jar
~ 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
R version 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,
包含了R通过rJava调用Java,Java通过JRI调用R。
并演示了win和linux中的用法。