Mus*_*mar 22 java eclipse r rserve
我的应用程序必须执行R操作,例如:
m = matrix(sample(0:1,100, rep=T),ncol=10)
Run Code Online (Sandbox Code Playgroud)
结果应该可供Java应用程序使用.
所述Rserve包桥R 1至其他语言,因为它作为一个TCP/IP服务器.我已阅读该网站,但不知道如何制作可以使用Rserve的最简单的应用程序.
制作一个使用Rserve从Java执行R命令的简单Eclipse应用程序需要哪些步骤?
小智 21
下载部分有一个二进制版本的Rserve(www.rforge.net/Rserve/files/我有版本R 2.13和Windows xp,所以我需要下载Windows二进制文件:Rserve_0.6-8.zip(541.3kb,更新) :Wed Apr 18 07:00:45 2012)).将文件复制到包含R.DLL的目录.从CRAN安装Rserve后
install.packages("Rserve")
Run Code Online (Sandbox Code Playgroud)
在R(我有RStudio - 方便的事情:下载RStudio IDE).开始Rserve来自R内,只需输入
library(Rserve)
Rserve()
Run Code Online (Sandbox Code Playgroud)
Сheck在任务管理器中 - 应该运行Rserve.exe.在Eclipse中创建Java项目之后,在该项目下创建一个名为lib的目录.在这里粘贴2罐RserveEngine.jar和REngine.jar(www.rforge.net/Rserve/files/).不要忘记在属性java-project中添加这个jar.在新的类代码中:
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
Wig*_*moo 14
以下是从头开始创建RServe项目的更详细说明:
对于远程访问:
将以下内容添加到Rserv.conf中
workdir /tmp/Rserv
remote enable
auth required
plaintext disable
port 6311
maxsendbuf 0 (size in kB, 0 means unlimited use)
Run Code Online (Sandbox Code Playgroud)
在R中:运行以下命令
library(Rserve)
Run Code Online (Sandbox Code Playgroud)
对于Windows:
Rserve()
Run Code Online (Sandbox Code Playgroud)
对于Mac:
Rserve(args="--no-save")
Run Code Online (Sandbox Code Playgroud)
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");
}
}
Run Code Online (Sandbox Code Playgroud)
最后,运行HelloWorldApp.java
对于那些使用Maven的人
<dependency>
<groupId>org.nuiton.thirdparty</groupId>
<artifactId>REngine</artifactId>
<version>1.7-3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
<dependency>
<groupId>org.rosuda.REngine</groupId>
<artifactId>Rserve</artifactId>
<version>1.8.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)