我的应用程序必须执行 R 操作,例如:
m = matrix(sample(0:1,100, rep=T),ncol=10)
结果应该可用于 Java 应用程序。
Rserve 包将R 连接到其他语言,因为它充当 TCP/IP 服务器。我已经阅读了该网站,但不知道如何制作可以使用 Rserve 的最简单的应用程序。
制作一个使用 Rserve 从 Java 执行 R 命令的简单 Eclipse 应用程序需要哪些步骤?
下载部分有 Rserve 的二进制版本(www.rforge.net/Rserve/files/ 我有版本 R 2.13 和 Windows xp,所以我需要下载 Windows 二进制:Rserve_0.6-8.zip(541.3kb,更新: 2012 年 4 月 18 日星期三 07:00:45))。将该文件复制到包含 R.DLL 的目录。从 CRAN 安装 Rserve 后
install.packages("Rserve")
在 R 中(我有 RStudio - 方便的事情:下载 RStudio IDE)。开始 Rserve 来自 R 内部,只需键入
library(Rserve)
Rserve()
在任务管理器中检查 - 应该运行 Rserve.exe。在 Eclipse 中创建一个 Java 项目后,在该项目下创建一个名为 lib 的目录。在此处粘贴 2 jar RserveEngine.jar 和 REngine.jar (www.rforge.net/Rserve/files/)。不要忘记在你的 java 项目的 Properties 中添加这个 jars。在新的类代码中:
import org.rosuda.REngine.*;
import org.rosuda.REngine.Rserve.*;
public class rserveuseClass {
public static void main(String[] args) throws RserveException {
try {
RConnection c = new RConnection();// make a new local connection on default port (6311)
double d[] = c.eval("rnorm(10)").asDoubles();
org.rosuda.REngine.REXP x0 = c.eval("R.version.string");
System.out.println(x0.asString());
} catch (REngineException e) {
//manipulation
}
}
}
以下是从头开始创建 RServe 项目的一些更详细的说明:
对于远程访问:
将以下内容添加到 Rserv.conf
workdir /tmp/Rserv
remote enable
auth required
plaintext disable
port 6311
maxsendbuf 0 (size in kB, 0 means unlimited use)
在 R 中:运行以下命令
library(Rserve)
对于 Windows:
Rserve()
对于 Mac:
Rserve(args="--no-save")
Rserve 的一个实例现在在 localhost 端口 6311 上运行。
为此,我将使用 eclipse:
将此代码添加到类
package com.sti.ai;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
public class HelloWorldApp {
public static void main(String[] args) throws RserveException, REXPMismatchException, FileNotFoundException, IOException {
RConnection c = new RConnection("<host/ip>", 6311);
if(c.isConnected()) {
System.out.println("Connected to RServe.");
if(c.needLogin()) {
System.out.println("Providing Login");
c.login("username", "password");
}
REXP x;
System.out.println("Reading script...");
File file = new File("<file location>");
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
for(String line; (line = br.readLine()) != null; ) {
System.out.println(line);
x = c.eval(line); // evaluates line in R
System.out.println(x); // prints result
}
}
} else {
System.out.println("Rserve could not connect");
}
c.close();
System.out.println("Session Closed");
}
}
最后,运行 HelloWorldApp.java
对于那些正在使用 Maven 的人
<dependency>
<groupId>org.nuiton.thirdparty</groupId>
<artifactId>REngine</artifactId>
<version>1.7-3</version>
</dependency>
<dependency>
<groupId>org.rosuda.REngine</groupId>
<artifactId>Rserve</artifactId>
<version>1.8.1</version>
</dependency>
快速的,试图分离任务:
Rserve 可以自行安装。从那里开始。
Rserve 有示例客户端。尝试使用 Java 示例来工作。
从那里,编写你的新程序。
Eclipse 完全是可选的。您不必使用它。如果这是要学习的更多步骤,请考虑跳过它。一旦 1 到 3 没问题,学习如何在 Eclipse 中表达构建依赖关系。