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

In my java project I need to calculate the confidence interval, which is too hard to code myself. So I decided to use the method in R statistics and feed the calculation result to the java project. For this I select the JRI and in my .java file I import Rengine and REXP:

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

      

Now here's the problem. I want to import the "stats4" library so that I can use the function mle()

and I write the following code in the .java file:

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

      

However, I found I was stats4

never imported! In fact, when we import this library into R language, we can write the following:

library("stats4")  

      

Since I'm new to JRIs in java, I don't know how to fix this.

+3


source to share


1 answer


Enter this code 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());
      }
    }

      

List of packages installed in your R version library folder integrated into your Java application (there may be multiple R versions on your machine), look at the result and see if you can find "stats4" in this list, if not then install this library in the correct version of r, that is, in this folder, or just change the JRI setting to something else. 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 I installed it in R version 2.15.3 (yes its old :() which is the version connected to my JRI apps, so this will work for me.

+1


source







All Articles