Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import R library when using JRI in java Eclipse?

Tags:

java

r

In my java project, I need to compute a confidence interval, which is too hard to write a code by myself. So I decided to use a method in R statistics, and transfer the computing result to my java project. To do this, I choose JRI and in my .java file, I import the Rengine and REXP:

import org.rosuda.JRI.REXP;  
import org.rosuda.JRI.Rengine;

Now, here is the problem. I want to import the "stats4" library so I can use the mle() function, and I write the following code in my .java file:

Rengine re = new Rengine(new String [] {"--vanilla"}, false, null);  
re.eval("library(stats4)"); 

However, I find out that the stats4 has never been imported! Actually, when we import this library in R language, we can write as the following:

library("stats4")  

Since I'm new to use the JRI in java, I don't know how to fix it.

like image 792
LittleYUYU Avatar asked Jan 24 '26 21:01

LittleYUYU


1 Answers

Please type this into your Java code:

System.out.println("R_HOME =" + System.getenv("R_HOME"));
  String path =System.getenv("R_HOME") + "\\library" ;
   File folder = new File(path);
  File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
      if (listOfFiles[i].isFile()) {
        System.out.println("File " + listOfFiles[i].getName());
      } else if (listOfFiles[i].isDirectory()) {
        System.out.println("Directory " + listOfFiles[i].getName());
      }
    }

Its the list of packages that are installed in your library folder of the R version that is integrated into your Java application (you might have several R versions installed in your machine), give a look at the output and see if you can find `stats4' in that list, if not then please install that library in the correct r version i.e, in that folder, or just change your JRI setup to another version. for example here is the output of my library installation:

 R_HOME =C:\Program Files\R\R-2.15.3   
Directory abind
    Directory amap
    Directory animation
    ..........

    Directory stats4
    Directory stringr


    ......

which means that i have it installed in R version 2.15.3 (yes its old :( ) , which is the version that connected to my JRI applications, so for me it will work.

like image 146
Yehoshaphat Schellekens Avatar answered Jan 26 '26 12:01

Yehoshaphat Schellekens