问题
My application must perform R operations such as:
m = matrix(sample(0:1,100, rep=T),ncol=10)
The results should be available to a Java application.
The Rserve package bridges R to other languages, since it acts as a TCP/IP server. I've read the website but don't know how to make the simplest application that can use Rserve.
What steps are required to make a simple Eclipse application that uses Rserve to execute R commands from Java?
回答1:
There is a binary version of Rserve in the download section (www.rforge.net/Rserve/files/ I have version R 2.13 and Windows xp, so I need download Windows binary: Rserve_0.6-8.zip (541.3kb, updated: Wed Apr 18 07:00:45 2012)). Copy the file to the directory containing R.DLL. After installed Rserve from CRAN
install.packages("Rserve")
in R (I have RStudio - convenient thing: Download RStudio IDE). started Rserve is from within R, just type
library(Rserve)
Rserve()
Сheck in Task Manager - Rserve.exe should be run. After make a Java project in Eclipse, make a directory called lib under that project. Paste 2 jar here RserveEngine.jar and REngine.jar (www.rforge.net/Rserve/files/). Do not forget to add this jars in Properties your java-project. In new class code:
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
}
}
}
回答2:
Here are some more detailed instructions for creating an RServe project from scratch:
First Install and get Rserve running in R.
- Install R
- Add package RServe from CRAN.
- In R type: install.packages("Rserve")
For remote access:
- Create file: /etc/Rserv.conf
Add the following to Rserv.conf
workdir /tmp/Rserv
remote enable
auth required
plaintext disable
port 6311
maxsendbuf 0 (size in kB, 0 means unlimited use)
In R: run the following commands
library(Rserve)
For Windows:
Rserve()
For Mac:
Rserve(args="--no-save")
An instance of Rserve is now running on localhost port 6311.
Next Create an Rserve project (i'm using eclipse)
For this I'm going to use eclipse:
- Download RserveEngine.jar and REngine.jar from here.
- Create a java project in eclipse.
- Create a lib folder under your project directory. (same level as your src folder)
- Copy RserveEngine.jar and REngine.jar into the lib folder.
- Add jars to build path: Instructions
- Add a package then a main class: call it something like HelloWorldApp.
Add this code to the class
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");
}
}
Finally, run HelloWorldApp.java
For those who are using Maven
REngine
<dependency>
<groupId>org.nuiton.thirdparty</groupId>
<artifactId>REngine</artifactId>
<version>1.7-3</version>
</dependency>
RServe
<dependency>
<groupId>org.rosuda.REngine</groupId>
<artifactId>Rserve</artifactId>
<version>1.8.1</version>
</dependency>
回答3:
Quick ones, trying to separate tasks:
Rserve can be installed by itself. Start there.
Rserve has sample clients. Try to the Java samples to work.
From there, write your new program.
Eclipse is entirely optional. You do not have to use it. If this is one more step to learn, consider skipping it. Once 1 to 3 are fine, learn how to express build dependencies in Eclipse.
回答4:
There are two ways to call R from Java - JRI and RServe. This is a plugin that will help you setup RJava on windows. If you are looking for a more production level solution then Rserve serves a better purpose. This example shows how to run a sample RServe program. If you are using RServe then run your command in eval function
REXP m = c.eval("matrix(sample(0:1,100, rep=T),ncol=10)")
There are some default datastructures that you can use to convert m (REXP).
来源:https://stackoverflow.com/questions/10216014/simple-program-to-call-r-from-java-using-eclipse-and-rserve