Introduction
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
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(); System.out.println(booleanValue.toString()); booleanValue = re.eval("1==2").asBool(); System.out.println(booleanValue.toString());
RFactor
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 = re.eval("factor(c('Monday','Tuesday','Wednesday','Thursday','Friday','Monday','Tuesday'))") .asFactor(); System.out.println(factors.toString()); //Output: //{levels=("Friday","Monday","Thursday","Tuesday","Wednesday"),ids=(1,3,4,2,0,1,3)}
RList
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 .eval("list(name=\"studytrails\",website=\"www.studytrails.com\",number=3)") .asList(); list. String[] keys = list.keys(); for (String key : keys) { System.out.println("key->" + key + "::value->" + list.at(key)); } //output //key->name::value->[STRING "studytrails"] //key->website::value->[STRING "www.studytrails.com"] //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
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()); System.out.println(vector); System.out.println(vector.at(0));
REXP
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.
Great tutorial. Any chance that you would be uploading how to use image,plot etc?
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.
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?
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.