【问题标题】:R libraries not loading using JRI in JavaR 库未在 Java 中使用 JRI 加载
【发布时间】:2018-01-17 04:11:55
【问题描述】:

我正在尝试使用 JRI(JAVA R INTERFACE) 在 Java 中使用 r 读取 excel,但它不起作用。我列出了一些我认为可能是原因的错误-

  1. R 库未正确加载。

  2. 文件位置错误(由于分隔符)。

  3. 加载库的方法错误。

代码 -

    package pkg;

    import java.io.File;

    import org.rosuda.JRI.REXP;
    import org.rosuda.JRI.Rengine;
    public class Read {


    public static void main(String args[]) {

    // Start Rengine.
    Rengine engine = new Rengine(new String[] { "--no-save" }, false, null);




    //printing the packages in library
    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());
          }
        }


    //calling required libraries
   String librarylocation= engine.eval(".libPaths()").asString();
   System.out.println("location of the R libraries:" + librarylocation);



   //to check whether engine is actually working
   Double d= engine.eval("x=mean(c(1,2,3))").asDouble();
   System.out.println("Mean obtained:" + d);


   //checking the working directory 
   String workingdirectory= engine.eval("getwd()").asString();
   System.out.println("location of the working directory:"+ workingdirectory);


   //R CODE TO READ EXCEL FILE (NOT WORKING)
   engine.eval("library(rJava)");
   engine.eval("library(xlsxjars)");
   engine.eval("library(xlsx)");
   engine.eval("library(Matrix)");
   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)");



    //PRINTING VALUE TO CHECK IF THE FILE WAS READ (NOT WORKING)       
    REXP data=engine.eval("excelfile[1,1]");
    System.out.println(" value in data frame:"+ data);



   //performing required operations (NOT WORKING BECAUSE OF ABOVE CODE)
   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)");


   //printing the created data frame(NULL OUTPUT)
   REXP datafinal = engine.eval(" finaldata");
   System.out.println("Data table is :" + datafinal);


   //Writing the data frame to an excel file(NO FILE CREATED )
   engine.eval("write.xlsx(finaldata,\"D:\\\\final2.xlsx\"");
   engine.eval("initial=paste('aakash','singhal')");

}

}

获得结果-

R_HOME =C:\Program Files\R\R-3.4.1
Directory base
Directory boot
Directory class
Directory cluster
Directory codetools
Directory compiler
Directory datasets
Directory df2json
Directory foreign
Directory graphics
Directory grDevices
Directory grid
Directory KernSmooth
Directory lattice
Directory MASS
Directory Matrix
Directory methods
Directory mgcv
Directory nlme
Directory nnet
Directory parallel
Directory rJava
Directory rjson
Directory rpart
Directory spatial
Directory splines
Directory stats
Directory stats4
Directory survival
Directory tcltk
Directory tools
Directory translations
Directory utils
Directory XLConnect
Directory XLConnectJars
Directory xlsx
Directory xlsxjars
location of the R libraries:C:/Program Files/R/R-3.4.1/library
Mean obtained:2.0
location of the working directory:D:/Eclipse/RandJava
value in data frame:null
Data table is :null

我已经尝试过 Stack 上有关此问题的所有可用方法,但似乎没有任何效果。

【问题讨论】:

    标签: java r jri


    【解决方案1】:

    在 Java 中使用 R 脚本的更好方法是像使用 R 函数一样使用它。 制作一个 R 脚本并相应地传递参数。它还将为代码提供更多功能。

    我没有附加整个代码,但是在 R 脚本中编写所有 engine.eval(Strings) 并在代码中获取它可以完美地工作。

    Java -

    engine.eval("source(\"testRun2.R\")");  
    engine.eval("testrunfunction(filepath,filelocation)");
    

    R-

    testrunfunction<-function(filePath,filelocation)
    {
    library('rJava')
    library('xlsxjars')
    library('xlsx')
    library('Matrix')
    library('xlsx')
    excelfile<-read.xlsx(filepath,sheetIndex=1,startRow=9,colIndex=c(1,2,3))
    column4<-matrix(nrow(excelfile),1)
    for(i in 1:(nrow(excelfile))){column4[i]=excelfile[i+1,3]-excelfile[i,3]}
    column4<-data.frame(column4)
    finaldata<-cbind(excelfile,column4)
    write.xlsx(finaldata,filelocation)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-29
      • 2012-05-07
      • 2012-03-19
      • 1970-01-01
      • 2011-12-23
      • 2011-12-29
      • 2015-11-18
      相关资源
      最近更新 更多