Question

I am new to R and have been trying to use JRI. I am faced with the following issue -

  1. I have a set of values that I have received in my Java program which I have stored in a list.
  2. I have multiple such lists - each one representing a feature.
  3. I want to pass these lists to an R script to create a simple regression model. The script uses the lm() function.

I have come across multiple scripts which read from a file and load the data using read.table() (or other equivalent functions) before calling 'lm()'. In this particular case, I do not want to write to a file(create a new file) and again read from that file into memory - as I already have the data in memory.

Is there a way for me to pass this list from Java to the R script directly so that it can be used like a data frame? Can I pass this list as an argument?

I have searched a lot, but couldn't find anything similar. Any pointers would really be appreciated.

Was it helpful?

Solution

Very easy using rJava.

java code side

I create a dummy class that generate some values.

public class test_arr {
    public double[] getValues(int n){
        double[] anArray = new double[n];
        for(int i =0; i <n;i++)
            anArray[i] = Math.random();
        return anArray;
    }
}

r code side

Using rjava package , I create an R object ( a pointer to the java object), I call the generator and I get the numeric values.

library(rJava)
.jinit("PATH_TO_YOR_JAVA_test_arr/bin") # this starts the JVM
object <- .jnew("test_arr")
nn = object$getValues(5L)
[1] 0.3667268 0.3636245 0.6796906 0.3692489 0.4051942

Then you do the regression like this :

lm(vv~nn,data=data.frame(vv,nn=runif(10)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top