javarcaller

RCaller get matrix of unknown size


I'm using RCaller in Java in order to execute an external R program.

The problem is, that I don't know the exact size of the matrix but the .getAsDoubleMatrix() method wants to have the size.

double[][] matrix = caller.getParser().getAsDoubleMatrix("result", DONT_KNOW, DONT_KNOW);

Is there a way to keep the size dynamic?


Solution

  • By the revision e263b5393d43, RoutputParser class has now getDimensions() method for getting unknown dimensions of a matrix. Here is the passed test file:

    int n = 21;
    int m = 23;
    double[][] data = new double[n][m];
    for (int i=0;i<data.lengthi++){    
        for (int j=0;j<data[0].length;j++){
            data[i][j] = Math.random();
        }
    }
    RCaller caller = new RCaller();
    Globals.detect_current_rscript();   
    caller.setRscriptExecutable(Globals.Rscript_current);
    
    RCode code = new RCode();
    code.addDoubleMatrix("x", data);
    caller.setRCode(code);
    
    caller.runAndReturnResult("x");
    
    int[] mydim = caller.getParser().getDimensions("x");
    
    Assert.assertEquals(n, mydim[0]);
    Assert.assertEquals(m, mydim[1]);
    

    the latest compiled jar does not include this update, however, I plan to prepare a version 2.3 next week or you can download the source and compile yourself immediately.

    Visit the official blog page of RCaller for the latest release here