0

I am trying to integrate R code inside java code.Primary objective is to be able to use predictions on models created in R. I am able to set up the environment properly and basic R commands are working fine. Issue: trying to push data from java to R function.

code snippet:

REXP data=re.eval("newdata <- with(cbpp, expand.grid(period=unique(period), herd=unique(herd)))");
REXP fit=re.eval("predict(gm1,newdata,type=\"response\")");   //Want to pass data created in java as newdata
System.out.println(fit.asDoubleArray().length);

in above code the "newdata is being populated by R and used what i want is to be able to create my own data in java and pass to predict function in R. Note: The R code i am trying to execute is an example for lme4 package for generalised linear mixed mmodel(glmm) model

1 Answers1

0

It's been a long time since I used JRI but I think you can use assign(). Here's a simple example passing an array of doubles:

re.assign("newdata", new double[] {1.5, 2.5, 3.5});
Ciarán Tobin
  • 7,306
  • 1
  • 29
  • 45
  • Hi MS, I have tried passing the double array as data using assign, But it doesnt accept that as valid data. I checked in R the class type of new Data is consists of Factors which is not compatible with double[]. I want to know what data type in Java thats compatible with "newdata" class type. Appreciate you response very much :) – devendra singh Aug 26 '15 at 16:30
  • I don't have the JRI set up on my computer, but I think you should be able to pass a string array to R using `re.assign()` and then convert that to a factor in R using `re.eval()`. – Ciarán Tobin Aug 27 '15 at 14:45
  • Thanks MS, today I solved the issue. Just for future use: Solution: There is a higher level API for Rengine which i used to create a dataframe out of integer array. Then I passed this dataframe to R and there i coverted the datafram of integer to dataframe of factors and it worked. Will try finding a more elegant solution and update here. – devendra singh Aug 28 '15 at 18:57