Simple program to call R from Java using Eclipse and Rserve
Asked Answered
V

4

23

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?

Vassaux answered 18/4, 2012 at 18:53 Comment(0)
A
21

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
        }       

    }
}
Ansell answered 19/4, 2012 at 5:8 Comment(0)
S
15

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/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:

  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>
Sideway answered 3/3, 2014 at 13:40 Comment(0)
C
3

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.

Chaff answered 18/4, 2012 at 19:27 Comment(1)
My problem is getting the java clients to work, what exactly should be done? please assume I dont knowVassaux
P
2

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

Precinct answered 29/6, 2013 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.