R from within Java R from within Java r r

R from within Java


Use JRI: http://www.rforge.net/JRI/. It comes bundled with rJava, including some examples of usage.

A very simple example would be like this:

import java.io.*;import java.awt.Frame;import java.util.Enumeration;import org.rosuda.JRI.Rengine;import org.rosuda.JRI.REXP;import org.rosuda.JRI.RVector;import org.rosuda.JRI.RMainLoopCallbacks;public class rJavaTest {    public static void main(String[] args) {        Rengine re=new Rengine(args, false, new TextConsole());        REXP x;        re.eval("print(1:10/3)");        System.out.println(x=re.eval("iris"));        RVector v = x.asVector();        if (v.getNames()!=null) {            System.out.println("has names:");            for (Enumeration e = v.getNames().elements() ; e.hasMoreElements() ;) {                System.out.println(e.nextElement());            }        }        if (true) {            System.out.println("Now the console is yours ... have fun");            re.startMainLoop();        } else {            re.end();            System.out.println("end");        }    }}


There is something new called http://www.renjin.org/

One thing i like it over JRI is deployment, While jri required that your application users will download R, renjin does not, and it uses only the JVM to run.


I have found that forking R as a process, attaching to the process's stdin, stdout, and stderr streams, and sending R commands via the input stream to be quite effective. I use the filesystem to communicate between R and my Java process. This way, I can have multiple R processes running from different threads in Java and their environments do not conflict with each other.