2
我試圖通過Java程序與R一起工作。我首先在JRI上工作了一段時間,然而在回到Java程序之前,您必須完成R中所需的所有工作,這讓我看到了Rserve。通過Java程序與R交互
我似乎也遇到了Rserve的問題。我附加了我編寫的RserveTool,並試圖在主要方法中使用它來測試它。
import java.io.File;
import java.io.IOException;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
public class RserveTool {
private RConnection _r;
private Process _p;
public RserveTool() {
System.out.println("Creating R Connection.");
_p = null;
try {
_p = Runtime.getRuntime().exec("Rserve.exe", null, new File("C:\\Program Files\\R\\R-3.0.2\\bin\\x64\\"));
} catch (IOException e) {
e.printStackTrace();
}
try {
_r = new RConnection();
REXP x = _r.eval("R.version.string");
System.out.println("Connection built to: " + x.asString());
} catch (RserveException e) {
e.printStackTrace();
} catch (REXPMismatchException e) {
e.printStackTrace();
}
}
public void endRserveProcess() {
_p.destroy();
}
public REXP readInCSVData(File csvFile, String dataVariableName, boolean isHeaderInData) throws RserveException {
String fileLocation = csvFile.getAbsolutePath();
return _r.eval(dataVariableName + "<-read.csv('"+fileLocation+"', header=" +String.valueOf(isHeaderInData).toUpperCase() + ")");
}
public REXP evalData(String dataVariableName) throws RserveException {
return _r.eval(dataVariableName);
}
public REXP attachData(String dataVariableName) throws RserveException {
return _r.eval("attach("+dataVariableName+")");
}
public REXP runMultiLinearRegression(String responseVariableName, String[] variablesToRegressUpon, String dataVariableName) throws RserveException {
String insidelm = responseVariableName;
for(int i=0; i<variablesToRegressUpon.length; i++) {
if(i==0) {
insidelm+="~";
} else {
insidelm+="+";
}
insidelm+=variablesToRegressUpon[i];
}
insidelm+=", data = " + dataVariableName;
return _r.eval("results=lm("+insidelm+")");
}
public REXP runSummaryOfMultiLinearRegression() throws RserveException {
return _r.eval("summary(results)");
}
public double[] getPValuesFromRegression(int numTermsRun) throws RserveException {
REXP p = _r.eval("summary(results)");
System.out.println(p);
double[] pValues = new double[numTermsRun+1];
// for(int i=0; i<p.asVector().getNames().size(); i++) {
// if(p.asVector().getNames().get(i).equals("coefficients")) {
// REXP results = (REXP) p.asVector().get(i);
// System.out.println(p.asVector().get(i));
// for(int j=0; j<=numTermsRun; j++) {
// pValues[j] = results.asDoubleArray()[3*(numTermsRun+1)+j];
// }
// }
// }
return pValues;
}
}
我得到以下例外。
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at java.net.SocketInputStream.read(SocketInputStream.java:107)
at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:213)
at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:180)
at org.rosuda.REngine.Rserve.protocol.RTalk.request(RTalk.java:250)
at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:231)
org.rosuda.REngine.Rserve.RserveException: eval failed
at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:234)
任何幫助都將不勝感激。對格式問題感到抱歉,我是新手!
'_p.waitFor()'試圖EVAL任何東西之前?你有一個非常有用的名爲[Rsession]的包裝器(http://code.google.com/p/rsession/),可以幫助安全地做到這一點,並小心處理Windows案例(這個過時的操作系統無法分叉進程) - 從'newInstanceTry(...)'函數中查看或獲取靈感。 –