3

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.

1 Answer 1

3

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)))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Not having my dev env. with me right now but will try this asap. This also means that rJava is a must. If I have an existing R script, then it has to be modified to use rJava now. Is my understanding correct?- Milind
Yep you should instal rjava package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.