You are on page 1of 17

R Programming

through Java
B.Bhuvaneswaran
Assistant Professor (SS)
Department of Computer Science & Engineering
Rajalakshmi Engineering College
Thandalam
Chennai 602 105
bhuvaneswaran@rajalakshmi.edu.in

R Programming Through
Java

Accessing the statistical power of R within


java applications can sometimes be a
powerful technique.
Although one can attempt to automate R
via Runtime.exec, doing so can be
exception prone and can limit how one
accesses R to single calls.
Rserve is a great alternative to facilitate
pluging R into java applications.

Rserve Configuration
The Rserve webpage gives details for how
one goes about installing and starting the
server.
Briefly, one must first download the Rserve
package in R, followed by starting the R
server by loading the library and calling
the Rserve() function, or running Rserve
as a daemon via the command line using
the 'R CMD Rserve' syntax.

The Java Client

From Java, accessing Rserve requires


adding the appropriate Rserve jars to the
classpath, instantiating an instance of
RConnection, then performing evaluations
- the code of which is evaluated with R,
the result of which is passed back to the
java application.

import org.rosuda.REngine.REXP;
import org.rosuda.REngine.Rserve.*;
public class RTest1 {
public static void main(String[] args) throws Exception{
RConnection c = null;
try{
c = new RConnection();
REXP x = c.eval("R.version.string");
System.out.println(x.asString());
}catch(Exception e){
e.printStackTrace();
}finally{
if ( c != null ){
try{
c.close();
}finally{}
}
}
}
}

The above code is a simple demonstration for


accessing Rserve running on the localhost
computer through R, simply printing out the
version of R.
To run on a remote server, simply pass the name
or IP address of the server running Rserve to the
RConnection constructor.
Rserve runs as a single instance of R, allowing
variables within R to be reused, negating the
need to pass the same data twice.

import org.rosuda.REngine.REXP;
import org.rosuda.REngine.Rserve.*;
/**
* Demonstration for accessing R via java
*
*/
public class RTest2 {
public static void main(String[] args) throws Exception{
double[] myvalues = {1.0, 1.5, 2.2, 0.5, 0.9, 1.12};
RConnection c = null;
try{
c = new RConnection();
c.assign("myvalues", myvalues);
REXP x = c.eval("mean(myvalues)");
System.out.println(x.asDouble());
x = c.eval("sd(myvalues)");
System.out.println(x.asDouble());
}catch(Exception e){
e.printStackTrace();
}finally{
if ( c != null ){
try{
c.close();
}finally{}
}
}
}
}

Here, the java code passes an array of


doubles to R, the calculates the mean and
standard deviation of the values.
Of course, more complex calculations can
be made, as well as accessing installed
packages.

Local and Remote R


Instances

Rserve can run locally or on a remote


server accessible by multiple clients.
Pass the name or IP address of the server
to the RConnection construct to access a
remote server.
Of course one should take into
consideration network traffic and
optimization when passing large amounts
of data.

Multithreading

When accessing R through Rserve from a java


application, often one must take into account the
communication and computation delay.
For instance, to prevent a user interface from
freezing up, perform Rserve communications in a
different thread or via a class such as a
SwingWorker (if using swing).
Alternative, if computation is long and can be
parallelized the R library Snow can be utilized.

Disadvantages

Networking

Rserve operates through networks, thus if


transferring large volumes of data the
overhead may be too much.
Often, the solution may transfer data through
other means of store the data on the server
locally to the R server.

R Graph and Charting via Java

Because Rserve runs via a programmatic


interface, accessing the graphical power of R is
more complex.
Two options can be to
a) download the results and plot in java using
an API such as JFreeChart or
b) plot in R and save the plot as a file (for
instance, PDF or jpg) - then read the file from
java (the latter is a bit more complex should
the Rserver be a remote server).

Timing

R does not provide a value in so far as


progress completion.
As a result, when using Rserve via a graphical
user interface one must use indeterminate
progress bars rather than letting a user know
how much of the process has completed.

Steps
install.packages("Rserve")
library(Rserve)
Rserve()

References

http://www.algosome.com/articles/running-r-code-in-java.html

You might also like