Saturday, September 20, 2014

Getting ready to use Rserve

This is a quick test to connect to Rserve

I am working on CentOS release 6.5 (Final)

1. Let's set JAVA_HOME to do that find the value of JVM installed
update-alternatives --display java

It shows me that Current 'best' version is /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java accordingly I will set JAVA_HOME to /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/. To do this I add the following line to /etc/profile.d/java.sh. I will be copying the Rserve jar files to the JAVA_HOME/lib/ext/ folder so configuration defining CLASSPATH is also added (see next step for details on this)
export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib/ext/Rserve.jar:$JAVA_HOME/lib/ext/REngine.jar
** Make sure there is no gap between the "=" sign above. Once done you will have to either logout or login again or source using
source /etc/profile.d/java.sh

2. Download the most recent tar file from "Most Recent Snapshots" at http://rforge.net/Rserve/files/, untar them and copy the jar files from the archive into $JAVA_HOME/lib/ext/
wget wget http://rforge.net/Rserve/snapshot/Rserve_1.8-0.tar.gz
tar -xvzf Rserve_1.8-0.tar.gz
cp Rserve/inst/java/*.jar /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/lib/ext/.

3. Now let's write a simple Hello.java
import org.rosuda.REngine.*;
import org.rosuda.REngine.Rserve.*;
class Hello {
    public static void main(String[] args) throws RserveException, REXPMismatchException {
        RConnection c = new RConnection();
        REXP x = c.eval("R.version.string");
        System.out.println(x.asString());
        System.out.println("Hello World!"); // Display the string.
    }
}

4. Now compile and Run
javac Hello.java 
java Hello

No comments:

Post a Comment