我正在尝试使用JRI(JAVA R INTERFACE)在Java中使用r读取excel,但它无效。我列出了一些我认为可能是错误的错误 -
R库未正确加载。
文件位置错误(由于分隔符)。
用于加载库的错误方法。
代码 -
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上有关此问题的所有内容,但似乎没有任何效果。
答案 0 :(得分:0)
在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)
}