Shivani Jain
Shivani Jain

Reputation: 11

R - Connecting R and java using Rserve

I have build an application connecting R and java using the Rserve package. In that, i am getting the error as "evaluation successful but object is too big to transport". i have tried increasing the send buffer size value in Rconnection class also. but that doesn't seem to work. The object size which is being transported is 4 MB

here is the code from the R connection file

public void setSendBufferSize(long sbs) throws RserveException {

    if (!connected || rt == null) {
        throw new RserveException(this, "Not connected");
    }
    try {
        RPacket rp = rt.request(RTalk.CMD_setBufferSize, (int) sbs);
        System.out.println("rp is send buffer "+rp);
        if (rp != null && rp.isOk()) {
            System.out.println("in if " + rp);
            return;
        }
    } catch (Exception e) {
        e.printStackTrace();
        LogOut.log.error("Exception caught" + e);
    }

    //throw new RserveException(this,"setSendBufferSize failed",rp);        
}

The full java class is available here :Rconnection.java

Upvotes: 1

Views: 1190

Answers (1)

jfcorugedo
jfcorugedo

Reputation: 10059

Instead of RServe, you can use JRI, that is shipped with rJava package.

In my opinion JRI is better than RServe, because instead of creating a separate process it uses native calls to integrate Java and R.

With JRI you don't have to worry about ports, connections, watchdogs, etc... The calls to R are done using an operating system library (libjri).

The methods are pretty similar to RServe, and you can still use REXP objects.

Here is an example:

public void testMeanFunction() {

    // just making sure we have the right version of everything
    if (!Rengine.versionCheck()) {
        System.err.println("** Version mismatch - Java files don't match library version.");
        fail(String.format("Invalid versions. Rengine must have the same version of native library. Rengine version: %d. RNI library version: %d", Rengine.getVersion(), Rengine.rniGetVersion()));
    }

    // Enables debug traces
    Rengine.DEBUG = 1;

    System.out.println("Creating Rengine (with arguments)");
    // 1) we pass the arguments from the command line
    // 2) we won't use the main loop at first, we'll start it later
    // (that's the "false" as second argument)
    // 3) no callback class will be used
    engine = REngine.engineForClass("org.rosuda.REngine.JRI.JRIEngine", new String[] { "--no-save" }, null, false);
    System.out.println("Rengine created...");

    engine.parseAndEval("rVector=c(1,2,3,4,5)");
    REXP result = engine.parseAndEval("meanVal=mean(rVector)");
    // generic vectors are RVector to accomodate names
    assertThat(result.asDouble()).isEqualTo(3.0);
}

I have a demo project that exposes a REST API and calls R functions using this package.

Take a look at: https://github.com/jfcorugedo/RJavaServer

Upvotes: 0

Related Questions