Data Communication Between R and Java using JRI


This page describes how to install the eclipse plugin that helps calling R from Java using JRI. It is possible to transfer data between R and Java. In this tutorial we look at the objects in java that hold data returned from R.


RBool holds the boolean value. R boolean can take three states – true, false and NA. RBool has three methods isNA(), isTRUE() and isFALSE(). Here’s an example that shows how to get the return value from R into RBool

        RBool booleanValue = re.eval("1==1").asBool();

		booleanValue = re.eval("1==2").asBool();


RFactor stores the R factor variables. factors in R are used to store categorical values. Here’s an example of factors in R

> days=c('Monday','Tuesday','Wednesday','Thursday','Friday','Monday','Tuesday');
> factor(days)
[1] Monday    Tuesday   Wednesday Thursday  Friday    Monday    Tuesday  
Levels: Friday Monday Thursday Tuesday Wednesday

This is how we do it from java

RFactor factors =



RList is of type entry=value. The entry is stored in “head” and the value is stored in “body”. It is possible to retrieve body for a particular head using the method at(String).

RList list = re
String[] keys = list.keys();
for (String key : keys) {
	System.out.println("key->" + key + "::value->" +;
//key->name::value->[STRING "studytrails"]
//key->website::value->[STRING ""]
//key->number::value->[REAL* (3.0)]

To get the value’s string or number, use the asInt(), asString() or asDouble() method on the value object. The type can be obtained using the getType() method.


RVector represents the named generic vectors in R. It has all methods that java.util.Vector has plus methods at() and at(String)

RVector vector = (re.eval("data.frame(c(1:3),c(2:4))").asVector());


If none of the above solves your problem, then look at REXP. This encapsulates all R objects. RList, RVector, RBool and RFactor have all been converted from REXP, however, if you need final control then this is your class.

4 thoughts on “Data Communication Between R and Java using JRI

  • October 31, 2016 at 4:30 am

    Great tutorial. Any chance that you would be uploading how to use image,plot etc?

    • October 31, 2016 at 8:20 am

      An Easy way to handle images and plots are to let R create the plot in a directory and let java read from that directory. There is no need to pass the image from R to java. R has graphic devices for jpg, png etc.

    • October 31, 2016 at 8:21 am

      Do you have a specific use case where creating an image file in R and just reading that file in Java would not solve the problem?

  • July 21, 2017 at 4:22 pm

    Do you have an example where in java you are reading in an R function and the parameter of the R function is a file? Is this possible with rJava.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.