2

I am using Rserve to access an R script through my Java project. The java code asks for a user input to enter the file location and stores in a String variable. This variable is then passes through to the R function which should read the file location perform some processes and then create a new folder and write the processed data in individual files and then print out on the console that all the files have been generated. I initially checked the R connection with a smaller version of the program and it worked. But, when I include the steps to write data to files, it shows the following error: Enter the file path:

/home/workspace/TestR/test_file
Exception in thread "main" org.rosuda.REngine.Rserve.RserveException: eval failed, request status: error code: 127
    at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:234)
    at testMain.main(testMain.java:23)

Moreover, the code also does not print any statements on the console which have to be printed via R from the Rscript. Here is the Java code:

import java.util.Scanner;

import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.REngineException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;

public class testMain {
    static String dirPath;
    public static void main(String[] args) throws REXPMismatchException, REngineException {

        // For user input
        Scanner scanner = new Scanner(System.in );
        System.out.println("Enter the file path: ");

        dirPath = scanner.nextLine();

        RConnection c = new RConnection();
        // source the Palindrome function
        c.eval("source('/home/workspace/TestR/Main.R')");

        REXP valueReturned = c.eval("Main(\""+dirPath+"\")");
        //c.eval("Main(\""+dirPath+"\")");
        System.out.println(valueReturned.length());
    }

}

And, here is the R script:

Main <- function(FILE_PATH)
{
  ## load libraries
  library(MALDIquant)
  library(MALDIquantForeign)
  library(dcemriS4)
  require(gridExtra) # also loads grid
  library(lattice)
  library(fields)
  library(matlab)
  library(rJava)

  #Call the source files of the function which this script will use
  source('/home/workspace/TestR/importAnalyzeFormat.R', echo=TRUE)
  source('/home/workspace/TestR/exportFile.R', echo=TRUE)
  source('/home/workspace/TestR/readRecalibratedSpectra.R', echo=TRUE)

  spectralDataObjects <- importAnalyzeFormat(FILE_PATH)

  p <- detectPeaks(spectralDataObjects, method="MAD", halfWindowSize=1, SNR=1)

  # Assign the p to preprocessedDataObjects
  preprocessedDataObjects<-p

  dir.create("PreprocessedSpectra", showWarnings = FALSE)
  setwd("PreprocessedSpectra")

  for(i in 1:length(preprocessedDataObjects))
  {
     coordinateValue<-metaData(preprocessedDataObjects[[i]])
     coordinates<-coordinateValue$imaging$pos 
     mzValues<-mass(preprocessedDataObjects[[i]])
     intensityValues<-intensity(preprocessedDataObjects[[i]])
     exportFile(coordinates,mzValues,intensityValues)
  }

  print("Files exported. Program will now terminate")
  print("############################################################")

  return(preprocessedDataObjects)
}

Can someone please help me?

sgibb
  • 25,396
  • 3
  • 68
  • 74
novicegeek
  • 773
  • 2
  • 9
  • 29

2 Answers2

0

You have an error in your script, a 127 means that there is a parse exception.

If you use something like this it will print out the error in the script.

c is the rserve connection in this case.

  c.assign(".tmp.", myCode);
    REXP r = c.parseAndEval("try(eval(parse(text=.tmp.)),silent=TRUE)");
    if (r.inherits("try-error")) System.err.println("Error: "+r.toString())
    else { // success .. }
Chris Hinshaw
  • 6,967
  • 2
  • 39
  • 65
0

Error code 127 means parsing exception.

Change your line:

c.eval("source('/home/workspace/TestR/Main.R')");

to

c.eval("source(\"/home/workspace/TestR/Main.R\")");

Now it is suppose to work.

David
  • 33
  • 1
  • 2
  • 9