使用 Eclipse 和 Rserve 从 Java 调用 R 的简单程序

2022-09-01 22:52:18

我的应用程序必须执行 R 操作,例如:

m = matrix(sample(0:1,100, rep=T),ncol=10)

结果应该可供 Java 应用程序使用。

Rserve 包将 R 桥接到其他语言,因为它充当 TCP/IP 服务器。我已经阅读了该网站,但不知道如何制作可以使用Rserve的最简单的应用程序。

创建一个简单的 Eclipse 应用程序需要哪些步骤,该应用程序使用 Rserve 从 Java 执行 R 命令?


答案 1

下载部分有一个二进制版本的 Rserve(www.rforge.net/Rserve/files/ 我有版本 R 2.13 和 Windows xp,所以我需要下载 Windows 二进制文件:Rserve_0.6-8.zip (541.3kb, 更新时间: 星期三 四月 18 07:00:45 2012))。将文件复制到包含 R.DLL的目录中。从 CRAN 安装 Rserve 后

install.packages("Rserve")

在R中(我有RStudio - 方便的事情:下载RStudio IDE)。启动 Rserve 是从 R 内部开始的,只需键入

library(Rserve)
Rserve()

任务管理器中的 Сheck - Rserve.exe应该运行。在 Eclipse 中创建一个 Java 项目后,在该项目下创建一个名为 lib 的目录。在这里粘贴2个罐子 RserveEngine.jar和REngine.jar(www.rforge.net/Rserve/files/)。不要忘记在 Java 项目的属性中添加此 jar。在新的类代码中:

import org.rosuda.REngine.*;
import org.rosuda.REngine.Rserve.*;

public class rserveuseClass {
    public static void main(String[] args) throws RserveException {
        try {
            RConnection c = new RConnection();// make a new local connection on default port (6311)
            double d[] = c.eval("rnorm(10)").asDoubles();
            org.rosuda.REngine.REXP x0 = c.eval("R.version.string");
            System.out.println(x0.asString());
} catch (REngineException e) {
            //manipulation
        }       

    }
}

答案 2

以下是从头开始创建 RServe 项目的一些更详细的说明:

首先安装并让 Rserve 在 R 中运行。

  1. 安装 R
  2. 从 CRAN 添加包 RServe。
  3. 在 R type 中: install.packages(“Rserve”)

对于远程访问:

  • 创建文件:/etc/Rserv.conf

将以下内容添加到 Rserv.conf

workdir /tmp/Rserv
remote enable
auth required
plaintext disable
port 6311
maxsendbuf 0 (size in kB, 0 means unlimited use)

在 R 中:运行以下命令

library(Rserve)

对于 Windows:

Rserve()

对于 Mac:

Rserve(args="--no-save")

Rserve 的实例现在在本地主机端口 6311 上运行。

下一步 创建一个 Rserve 项目 (我正在使用 eclipse)

为此,我将使用eclipse:

  1. 此处下载 RserveEngine.jar 和 REngine.jar。
  2. 在 eclipse 中创建一个 java 项目。
  3. 在项目目录下创建一个 lib 文件夹。(与 src 文件夹的级别相同)
  4. 将 RserveEngine.jar 和 REngine.jar复制到 lib 文件夹中。
  5. 将 jar 添加到构建路径:说明
  6. 添加一个包,然后添加一个主类:称之为HelloWorldApp。

将此代码添加到类中

package com.sti.ai;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
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 HelloWorldApp {

    public static void main(String[] args) throws RserveException, REXPMismatchException, FileNotFoundException, IOException {
        RConnection c = new RConnection("<host/ip>", 6311);
        if(c.isConnected()) {
            System.out.println("Connected to RServe.");
            if(c.needLogin()) {
                System.out.println("Providing Login");
                c.login("username", "password");
            }

            REXP x;
            System.out.println("Reading script...");
            File file = new File("<file location>");
            try(BufferedReader br = new BufferedReader(new FileReader(file))) {
                for(String line; (line = br.readLine()) != null; ) {
                    System.out.println(line);
                    x = c.eval(line);         // evaluates line in R
                    System.out.println(x);    // prints result
                }
            }

        } else {
            System.out.println("Rserve could not connect");
        }

        c.close();
        System.out.println("Session Closed");
    }

}

最后,运行HelloWorldApp.java

对于那些正在使用Maven的人

发动机

<dependency>
    <groupId>org.nuiton.thirdparty</groupId>
    <artifactId>REngine</artifactId>
    <version>1.7-3</version>
</dependency>

断续器

<dependency>
    <groupId>org.rosuda.REngine</groupId>
    <artifactId>Rserve</artifactId>
    <version>1.8.1</version>
</dependency>

推荐