1

我已经使用 Rserve 包构建了一个连接 R 和 java 的应用程序。在那,我收到错误为“评估成功,但对象太大而无法运输”。我也尝试增加 Rconnection 类中的发送缓冲区大小值。但这似乎不起作用。正在传输的对象大小为 4 MB

这是R连接文件中的代码

公共无效 setSendBufferSize(long sbs) 抛出 RserveException {

    if (!connected || rt == null) {
        throw new RserveException(this, "Not connected");
    }
    try {
        RPacket rp = rt.request(RTalk.CMD_setBufferSize, (int) sbs);
        System.out.println("rp is send buffer "+rp);
        if (rp != null && rp.isOk()) {
            System.out.println("in if " + rp);
            return;
        }
    } catch (Exception e) {
        e.printStackTrace();
        LogOut.log.error("Exception caught" + e);
    }

    //throw new RserveException(this,"setSendBufferSize failed",rp);        
}

完整的 java 类在这里可用:Rconnection.java

4

1 回答 1

0

您可以使用 rJava 包附带的 JRI 来代替 RServe。

在我看来,JRI 比 RServe 更好,因为它不是创建一个单独的进程,而是使用本地调用来集成 Java 和 R。

使用 JRI,您不必担心端口、连接、看门狗等……对 R 的调用是使用操作系统库 (libjri) 完成的。

这些方法与 RServe 非常相似,您仍然可以使用 REXP 对象。

这是一个例子:

public void testMeanFunction() {

    // just making sure we have the right version of everything
    if (!Rengine.versionCheck()) {
        System.err.println("** Version mismatch - Java files don't match library version.");
        fail(String.format("Invalid versions. Rengine must have the same version of native library. Rengine version: %d. RNI library version: %d", Rengine.getVersion(), Rengine.rniGetVersion()));
    }

    // Enables debug traces
    Rengine.DEBUG = 1;

    System.out.println("Creating Rengine (with arguments)");
    // 1) we pass the arguments from the command line
    // 2) we won't use the main loop at first, we'll start it later
    // (that's the "false" as second argument)
    // 3) no callback class will be used
    engine = REngine.engineForClass("org.rosuda.REngine.JRI.JRIEngine", new String[] { "--no-save" }, null, false);
    System.out.println("Rengine created...");

    engine.parseAndEval("rVector=c(1,2,3,4,5)");
    REXP result = engine.parseAndEval("meanVal=mean(rVector)");
    // generic vectors are RVector to accomodate names
    assertThat(result.asDouble()).isEqualTo(3.0);
}

我有一个演示项目,它公开了一个 REST API 并使用这个包调用 R 函数。

看看:https ://github.com/jfcorugedo/RJavaServer

于 2015-09-08T15:54:33.400 回答