Calling R functions using RCaller

In Calling R functions using RCaller you need the RCaller API. RCaller API is another simple way to call R from Java without JNI. There are lots of queries in the internet about “how to call r from java” or “call r function from java with / without JNI”. There are some solutions about these works, for example, RServe is a server application written in C and it waits for socket connections, then accepts clients and runs the R code that sent from socket streams and returns SEXP ‘s (S / R Expressions). Also, rJava is a JNI solution for calling R from Java. But as i see, users don’t want to struggle this things and they seeks more practical solutions.

RCaller uses neither sockets nor JNI interface for calling R functions from Java. RCaller simply runs RScript executable file using java’s Runtime and Process classes. Then runs R commands using arguments and handles results using streams. RCaller converts R objects to Java’s double or String arrays using a R script and BeanShell interpreter. After these operations R results can be handled by user using getter methods.

You can use it in your Java applications that needs some statistical calculations. Implementation and setting-up processes are easy. You can download source codes as Netbeans project and jars.

To Download the R Platform click Here

Let me explain them with some examples. Suppose that we have a double array with values of {1,4,3,5,6,10} and we want to show a time series plot with this. Firstly we import the needed libraries:

import rcaller.RCaller;
import rcaller.RCode;

We are declaring the double array:

double[] numbers = new double[] {1,4,3,5,6,10};

Creating an object of Rcaller class:

RCaller caller = new RCaller();

RCaller needs the Rscript executable file (Rscript.exe in windows) which is shipped with the R. You must tell the full path of this file in RCaller like this:

caller.setRscriptExecutable("C:\\Program Files\\R\\R-3.1.0\\bin\\Rscript.exe");

But some of the time the program fails to find the path because of the space between Program files so prefer to put it in another directory as follows;


This is the location of my Rscript file in my Windows. We didn’t do much thing, but this code initializes the whole thing:


the cleanRCode() function of RCaller class cleans the code buffer and puts some code in it. You can browse the source code if you want to know more about the initialization. Now, we can add our double array to our R code:

caller.addDoubleArray("x", numbers);

Now we have ‘x’ with the value of numbers[]. Now we are creating the time series plot:

File file = code.startPlot();

Finally we are sending the whole code to R interpreter:


With this code, Rscript runs our code but it does not return anything. After all, if we have’nt got any errors, we can handle the generated image using

ImageIcon ii=caller.getPlot(file);

or we can show it directly using


The source code of entire example is given below:

package test;

import javax.swing.ImageIcon;
import rcaller.RCaller;

public class Test1 {

public static void main(String[] args) {
new Test1();

* Test for simple plots
public Test1() {
try {
RCaller caller = new RCaller();

double[] numbers = new double[]{1, 4, 3, 5, 6, 10};

code.addDoubleArray(“x”, numbers);
File file = code.startPlot();
ImageIcon ii = code.getPlot(file);
} catch (Exception e) {