I am new to R and have been trying to use JRI. I am faced with the following issue -
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.
Very easy using rJava
.
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;
}
}
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)))