使用Eclipse和Rserve从Java调用R的简单程序 [英] Simple program to call R from Java using Eclipse and Rserve

查看:110
本文介绍了使用Eclipse和Rserve从Java调用R的简单程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

结果应该适用于Java应用程序。



Rserve包将R与其他语言联系起来,因为它行为作为TCP / IP服务器。我已经阅读了该网站,但不知道如何使用可以使用Rserve的最简单的应用程序。



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

解决方案

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

  install.packages(Rserve)

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

 库(Rserve)
Rserve()

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

  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默认端口(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
}

}
}
pre>

My application must perform R operations such as:

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

The results should be available to a Java application.

The Rserve package bridges R to other languages, since it acts as a TCP/IP server. I've read the website but don't know how to make the simplest application that can use Rserve.

What steps are required to make a simple Eclipse application that uses Rserve to execute R commands from Java?

解决方案

There is a binary version of Rserve in the download section (www.rforge.net/Rserve/files/ I have version R 2.13 and Windows xp, so I need download Windows binary: Rserve_0.6-8.zip (541.3kb, updated: Wed Apr 18 07:00:45 2012)). Copy the file to the directory containing R.DLL. After installed Rserve from CRAN

install.packages("Rserve")

in R (I have RStudio - convenient thing: Download RStudio IDE). started Rserve is from within R, just type

library(Rserve)
Rserve()

Сheck in Task Manager - Rserve.exe should be run. After make a Java project in Eclipse, make a directory called lib under that project. Paste 2 jar here RserveEngine.jar and REngine.jar (www.rforge.net/Rserve/files/). Do not forget to add this jars in Properties your java-project. In new class code:

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
        }       

    }
}

这篇关于使用Eclipse和Rserve从Java调用R的简单程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆