org.rosuda.REngine.Rserve.RserveException:从java运行R脚本时eval失败

时间:2017-06-19 10:43:48

标签: java r svm

我收到名为org.rosuda.REngine.Rserve.RserveException的RserveExcpetion:运行以下代码时eval失败。

import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;

public class Temp {

public static void main(String a[]) {
    RConnection connection = null;

    try {
        /* Create a connection to Rserve instance running on default port
         * 6311
         */
        connection = new RConnection();

        /* Note four slashes (\\\\) in the path */
        connection.eval("source('F:\\testFiles\\svm.R')"); //Here getting the exception
        int num1=10;
        int num2=20;
        int sum=connection.eval("myAdd("+num1+","+num2+")").asInteger();
        System.out.println("The sum is=" + sum);

    } catch (RserveException e) {

        e.printStackTrace();

    } catch (REXPMismatchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
}
}

如何解决这个问题

1 个答案:

答案 0 :(得分:0)

这是因为通过读入源文件未正确初始化R函数myAdd()。从这里尝试答案:Calling R from Java using RServe weird error使用try-catch语句查看实际的R错误。