Simple Program to Call R from Java Using Eclipse and Rserve

Simple program to call R from Java using Eclipse and Rserve

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
}

}
}

How to connect to R with Java (using Eclipse)

I cannot help you with the Java aspect, besides noting that on my Linux machine, simply calling make does the trick:

/tmp/java-new$ make
javac -d . -source 1.4 -target 1.4 MutableREXP.java REngineException.java REngine.java REXPDouble.java REXPEnvironment.java REXPExpressionVector.java REXPFactor.java REXPGenericVector.java REXPInteger.java REXP.java REXPLanguage.java REXPList.java REXPLogical.java REXPMismatchException.java REXPNull.java REXPRaw.java REXPReference.javaREXPS4.java REXPString.java REXPSymbol.java REXPUnknown.java REXPVector.java RFactor.java RList.java
jar fc REngine.jar org
rm -rf org
javac -d . -cp REngine.jar Rserve/RConnection.java Rserve/RFileInputStream.java Rserve/RFileOutputStream.java Rserve/RserveException.java Rserve/RSession.java Rserve/protocol/jcrypt.java Rserve/protocol/REXPFactory.java Rserve/protocol/RPacket.java Rserve/protocol/RTalk.java
Note: Rserve/protocol/REXPFactory.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
jar fc Rserve.jar org
rm -rf org

Likewise with the example directory you mentioned -- it builds fine on my Linux machine just saying 'make'. I do not use Eclipse and can't help with that aspect.

The C++ examples also run fine as well (though I needed to adjust the Makefile to build them).

You may need to ask to the Rosuda list for Rserv.

Java-R integration?

I have successfully used two alternatives in the past.

JRI

  • Pros: probably better performance.
  • Cons: you have to configure some environment variables and libraries, different in Win/UNIX.

RServe

  • Pros: easy to setup, you don't need to initialize R or link against
    any R library, can run in a different machine.
  • Cons: based on TCP/IP (a server is running), no callbacks from R.

Other alternatives I have never used : RCaller

How to use Rserve with JSP on Tomcat? (Windows 7)

Here this might help you out :)

http://www.studytrails.com/R/RServe/RServe-Java-First-Program.jsp



Related Topics



Leave a reply



Submit