通过Java程序与R交互

时间:2014-01-27 20:53:35

标签: java r

我正在尝试通过Java程序使用R.我第一次使用JRI工作,但是在你回到Java程序之前,你必须在R中做你需要的所有事情,这让我看看Rserve。

我似乎也遇到了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)

任何帮助都会非常感激。抱歉格式化问题,我是新手!

1 个答案:

答案 0 :(得分:0)

在运行java应用程序之前。你是否尝试先运行Rserve,即在R

1)图书馆(&#34; Rserve&#34;)2)Rserve()