Importing,cleaning and saving files using R in Java

45 Views Asked by At

I am trying to parse an excel file using r in Java, perform some operation and save it but the code isn't working and the final data frame (after the operation) is null and no file is made. The Same code works if I run it in R.

The code is attached-

package pkg;

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

public class Read {

public static void main(String args[]) {

    // Start Rengine.
    Rengine engine = new Rengine(new String[] { "--no-save" }, false, null);
    //calling required libraries
   String libloc="\"C:/Program Files/R/R-3.4.1/library\"";
   System.out.println(libloc);
   engine.eval(".libPaths("+libloc+")");
   engine.eval("library(rJava);library(xlsxjars);library(xlsx)");
   engine.eval("excelfile<-read.xlsx(\"C:/Users/aakashs/Desktop/File_Parse/LT257-Refuel 3 March2017.xlsx\",sheetIndex=1,startRow=9,colIndex=c(1,2,3)");
   engine.eval("column4<-matrix(nrow(excelfile),1)");
   engine.eval("for(i in 1:(nrow(excelfile))){column4[i]=excelfile[i+1,3]-excelfile[i,3]}");
   engine.eval("column4<-data.frame(column4)");
   engine.eval("finaldata<-cbind(excelfile,column4)");
   REXP datafinal = engine.eval("finaldata");
   System.out.println("Data table is :" + datafinal);
   engine.eval("write.xlsx(finaldata,\"D:/final2.xlsx\"");
   engine.eval("initial=paste('aakash','singhal')");
}

}
0

There are 0 best solutions below