Unable to run string related functions using rJava (JRI) in Java

227 views Asked by At

I have written a small program in Java (eclipse) to run R using JRI (rjava). All paths are set. The problem is that while I can run numeric functions (like add), I can't run a string function like cat. (please excuse any errors; I did Java coding for the first time yesterday).

package com.trial.ss;

import org.rosuda.JRI.Rengine;

public class RScriptConnection {

    public Rengine getRScriptEngine() throws Exception {

        Rengine engine = null;
        try {
            engine = Rengine.getMainEngine();
            if (engine == null) engine = new Rengine(new String[] {
                "--vanilla"
            },
            false, null);

            /*  if (!engine.waitForR()) {
                        System.out.println("Unable to load R");
                        return null;
                    } else*/
            System.out.println("Connected to R");
            String rScriptSourceFile = "source('" + RScriptConstant.RS_FILE_LOCATION + "',verbose=TRUE)";
            engine.eval(rScriptSourceFile);
            System.out.println("loading RScript file || completed");

            //return engine;
        } catch(Exception ex) {
            System.out.println("Exeption while connecting to REngine " + ex.getMessage());
            //throw new Exception("Error while creating REngine in RScriptConnection:getRScriptEngine()");
        }
        return engine;
    }

    public static void main(String[] args) {

        String libpath = System.getProperty("java.library.path");
        System.out.println("##############libpath=" + libpath);
        //      System.out.println("Method to be called in RScript=" + "Add(x1 = " + 10 + ", x2 = " + 20 + ", x3 = " + 30 + ", x4 = " + 50 + ")");
        RScriptConnection rScriptConnection = new RScriptConnection();

        try {
            Rengine rEngine = rScriptConnection.getRScriptEngine();
            String Value1 = "\"Advisory\"";
            String Value2 = "\"Assurance\"";
            double svalue = rEngine.eval("(1+2)").asDouble();
            System.out.println("mvalue=" + svalue);
            System.out.println("method to be called in RScript is " + "cat(" + Value1 + "," + Value2 + ")");
            String value = rEngine.eval("cat(" + Value1 + "," + Value2 + ")").asString();
            System.out.println(value);
            rEngine.end();

        } catch(Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

Please help me understand why my string function like cat doesn't work. Here is the output I am currently getting:

##############libpath=C:\Users\myname\Documents\R\win-library\3.3\rJava\jri\x64
Connected to R
loading RScript file || completed
mvalue=3.0
method to be called in RScript is cat("Advisory","Assurance")
null

Why am I getting null in the end? I should get Advisory Assurance

1

There are 1 answers

0
Eduardo On

Bellow is an example of how to show R output into Java. You basically have to implement RMainLoopCallbacks.

import org.rosuda.JRI.RMainLoopCallbacks;
import org.rosuda.JRI.Rengine;
import java.util.logging.Logger;

public class Runner {

    private static Logger log = Logger.getLogger("Runner");

    static class LoggingConsole implements RMainLoopCallbacks {
        private Logger log;

        LoggingConsole(Logger log) {
            this.log = log;
        }

        public void rWriteConsole(Rengine re, String text, int oType) {
            log.info(String.format("rWriteConsole: %s", text));
        }

        public void rBusy(Rengine re, int which) {
            log.info(String.format("rBusy: %s", which));
        }

        public void rShowMessage(Rengine re, String message) {
            log.info(String.format("rShowMessage: %s",  message));
        }

        public String rReadConsole(Rengine re, String prompt, int addToHistory) {
            return null;
        }

        public String rChooseFile(Rengine re, int newFile) {
            return null;
        }

        public void rFlushConsole(Rengine re) {
        }

        public void rLoadHistory(Rengine re, String filename) {
        }

        public void rSaveHistory(Rengine re, String filename) {
        }
    }

    Rengine engine = new Rengine(new String[] {"--no-save"}, false, new LoggingConsole(log));
    ...
    // Use the engine somewhere to evaluate a R method and see the output
    engine.eval(rScriptSourceFile);
}