Simple program to call R from Java using Eclipse and Rserve Simple program to call R from Java using Eclipse and Rserve r r

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. Paste2 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        }           }}


Here are some more detailed instructions for creating an RServe project from scratch:

First Install and get Rserve running in R.

  1. Install R
  2. Add package RServe from CRAN.
  3. In R type: install.packages("Rserve")

For remote access:

  • Create file: /etc/Rserv.conf

Add the following to Rserv.conf

workdir /tmp/Rservremote enableauth requiredplaintext disableport 6311maxsendbuf 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:

  1. Download RserveEngine.jar and REngine.jar from here.
  2. Create a java project in eclipse.
  3. Create a lib folder under your project directory. (same level as your src folder)
  4. Copy RserveEngine.jar and REngine.jar into the lib folder.
  5. Add jars to build path: Instructions
  6. 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>


Quick ones, trying to separate tasks:

  1. Rserve can be installed by itself. Start there.

  2. Rserve has sample clients. Try to the Java samples to work.

  3. From there, write your new program.

  4. 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.