1

I am trying to execute a R script from Java file.

Java code

public static void main(String a[]) {
    RConnection connection = null;

    try {
        /* Create a connection to Rserve instance running on default port
         * 6311
         */
        connection = new RConnection();

        /* Note four slashes (\\\\) in the path */
        connection.eval("source(\'D:/r script/arima with upper limit final.R\')");
        //connection.eval("Rserve()");
        int lim1=2500;
        int lim2=2700;
        REXP sum=connection.eval("testfunction()");
       //System.out.println("The sum is=" +);
    } catch (RserveException e) {
        e.printStackTrace();
    } //catch (REXPMismatchException e) {
        //e.printStackTrace();
    //}
}

R Script

testfunction = function(){
   lim1 = 2500
   lim2 = lim1+400
   start =lim2
   modeldata = as.vector(x$Mean.F3Amp)
   alarm = as.vector(x$Mean.F3HW)
   alarmpart = as.ts(alarm[lim1:lim2])
   predictalarm = alarmpart
   datapart = as.ts(modeldata[lim1:lim2])
   fit = Arima(datapart,order = c(3,1,1))
   modelforecast = forecast(fit,30)

   uppervaluemean = mean(modelforecast$upper[,2])

   prevMean = mean(tail(datapart,30))
   newMean =mean(modelforecast$mean)
   alarmMean = mean(tail(alarmpart,30))
   if(abs(alarmMean-uppervaluemean)>=0.01)
   {
   uppervaluemean =uppervaluemean+0.005
   predictalarm = c(predictalarm,rep.int(uppervaluemean,30))
   } else
   {
   predictalarm = c(predictalarm,rep.int(alarmMean,30))
   print("Else")
   }

   plot(modelforecast,xlim=c(0,lim2-lim1+30),ylim=c(0,0.6))
   lines(alarmpart,col=3)

   lines(predictalarm,col=5)

  filename = paste("D:/Plots123/plot",toString(iteration),".jpg",sep="")
  dev.copy(jpeg,filename=filename)
  dev.off()
  TRUE
}

I am new to RServe. The following is the error I get.

org.rosuda.REngine.Rserve.RserveException: eval failed, request status: error code: 127 at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:234) at pkg.Temp.main(Temp.java:23)

Line 23: REXP sum=connection.eval("testfunction()");

Vini
  • 1,978
  • 8
  • 40
  • 82

1 Answers1

0

The problem is that it isn't recommended to call user-defined functions just-like that as they are hard to debug. Right now it is hard to say where the R-script evaluation fails.

But right-off-the-bat it's easy to see that the script might not be evaluated properly since there are external variables such as x, that might not be present in the Rserve working environment. Also libraries such as forecast, fpp might not have been imported, but are being used inside the function. You probably need to make some code changes in the script, which might include lines like:

library(forecast)
library(fpp)
x<-5 #or whatever x is

and then define your function below all the above.

Try using this which might help point out the error:

REXP sum = connection.parseAndEval("try("+testfunction()+",silent=TRUE)");
if (sum.inherits("try-error")) System.err.println("Error: "+sum.asString());
brijs
  • 525
  • 6
  • 18
  • Arima is not a user defined function.. I will try adding the libraris which i am missing in the code – Vini Aug 26 '16 at 07:38