首页 > 代码库 > rJava配置

rJava配置

1. 下载安装R-3.1.1-win.exe;

2. 在R中安装rJava

> install.packages("rJava")

3. 设置环境变量:

1 PATH:D:\SoftWare\Java\jdk1.6.0_45\jre\bin\server;D:\SoftWare\Java\R\R311\bin\x642 CLASSPATH:D:\SoftWare\Java\R\R311\library\rJava\jri

4. 在R中调用Java 测试:

> library(rJava)> .jinit()> s <- .jnew("java/lang/String", "Hello World!")> s

一切配置成功则出现:

[1] "Java-Object{Hello World!}"
#加载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!"

rJava优化过的方法调用,用$来调用方法

#同.jcall(s,"I","length")> s$length()[1] 12#同.jcall(s,"I","indexOf","World")> s$indexOf("World")[1] 6

5. 安装Rserve

> install.packages("Rserve")

需要使用时在R控制台下加载该包,然后输入命令Rserve(),开启服务器,就可以供客户端调用。

 

rJava配置