1

I am working with a Java and I need to use an add-on R library and use the functions within that library. I tried the answers provided in the following questions

How I can load a R script into JRI and execute from Java?

Problem loading R own created libraries in Java/JRI code

but I still get a NullPointerException. Can anyone point out the error. Thankyou

Here is the code which I am using:

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

public class RConnect { 

public void processFiles(String[] spectrumData)
{
    // new R-engine
    Rengine re=new Rengine (new String [] {"--vanilla"}, false, null);
    if (!re.waitForR())
    {
        System.out.println ("Unable to load R");
        return;
    }
    else
        System.out.println ("Connected to R");

    REXP rexpSetFolder = re.eval("setwd('/home/user/R/x86_64-pc-linux-gnu-library/3.0')");
    REXP rexpFolder = re.eval("getwd()");
    System.out.println(rexpFolder.asString());

    REXP rexpLoad = re.eval("library(PROcess)");
    RVector f1 = (re.eval("read.files(spectrumData)").asVector());
    System.out.println(f1);

    re.end();
}
}
Community
  • 1
  • 1
novicegeek
  • 773
  • 2
  • 9
  • 29

1 Answers1

1

I tried to look for the R package "PROcess" you just mentioned, but i didn't find it, so i cant test your code, but generally add in packages work perfect in JRI, here is an example (using packages "forecast" and "plyr"):

import org.rosuda.JRI.REXP;
import org.rosuda.JRI.RVector;
import org.rosuda.JRI.Rengine;
/**
 *
 * @author yschellekens
 */
public class StackOverfolw {
    private static double[] foreCast;
    private static int i;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // new R-engine
    Rengine re=new Rengine (new String [] {"--vanilla"}, false, null);
    if (!re.waitForR())
    {
        System.out.println ("Unable to load R");
        return;
    }
    else
        System.out.println ("Connected to R");

    re.eval("load(file='C:/Users/yschellekens.INTRANET/Desktop/java projects/count_basic.Rda')  ", false);
        re.eval("library(plyr)");   
        re.eval("library(forecast)");
        re.eval("count_basic<-arrange(count_basic,TKDate)");
        re.eval("ts1<-ts(count_basic$click_count,frequency=7)");         
        re.eval("value<-stl(x=ts1,s.window=7)");
        re.eval("fit <- auto.arima(ts1)");
        re.eval("fit2<-forecast(fit,h=30)");
        re.eval("value3<-as.numeric(fit2$mean)");
        REXP testYvalue = re.eval("c(as.numeric(fit2$fitted),as.numeric(fit2$mean))");
        foreCast=testYvalue.asDoubleArray();


         for (i = 0; i < 10  ; i++) {
             System.out.println(foreCast[i]);;} 
    re.end();
}
    }
  1. Now look at the console:

    run: Connected to R 524.0 597.0 530.0 440.0 406.0 435.0 479.0 523.0 580.0 574.0 BUILD SUCCESSFUL (total time: 4 seconds)

My guess i that you downloaded the package "PROcess" (which i couldn't find) on a earlier version of R (not 3.0), if that is the case, just reload the package into your R 3.0 folder.

Yehoshaphat Schellekens
  • 2,305
  • 2
  • 22
  • 49
  • I tried to do a similar thing and it throws NPE. I have created a [question](http://stackoverflow.com/questions/38660138/rengine-eval-returns-null) for it, could you please take a look? – Rahul Sharma Jul 29 '16 at 13:46