在 Java 中使用 R 编程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4034936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using R programming in java
提问by Prashant
We are working on a complex statistical project on Java
. We did the original code in the R programming language. Is there a way to convert this code to Java
code (converter) or otherwise how can we use R
in a Java
project?
我们正在开展一个复杂的统计项目Java
。我们用R 编程语言编写了原始代码。有没有办法将此代码转换为Java
代码(转换器)或者我们如何R
在Java
项目中使用?
回答by Seidr
While I'm unaware of a 'converter', there is an interface called rJava which will allow you to run R code directly from Java.
虽然我不知道“转换器”,但有一个名为 rJava 的接口,它允许您直接从 Java 运行 R 代码。
rJava is a simple R-to-Java interface. It is comparable to the .C/.Call C interface. rJava provides a low-level bridge between R and Java (via JNI). It allows to create objects, call methods and access fields of Java objects from R.
In a sense the inverse of rJava is JRI (Java/R Interface) which provides the opposite direction - calling R from Java. JRI is now shipped as a part of the rJava package, although it still can be used as a separate entity (especially for development). Currently rJava is used as a part of JGR, iPlots and JavaGD software/packages.
rJava 是一个简单的 R 到 Java 接口。它类似于 .C/.Call C 接口。rJava 提供了 R 和 Java(通过 JNI)之间的低级桥梁。它允许从 R 创建对象、调用方法和访问 Java 对象的字段。
从某种意义上说,rJava 的逆是 JRI(Java/R 接口),它提供了相反的方向——从 Java 调用 R。JRI 现在作为 rJava 包的一部分提供,尽管它仍然可以用作单独的实体(特别是用于开发)。目前 rJava 被用作 JGR、iPlots 和 JavaGD 软件/包的一部分。
回答by Hymanson Cassimiro
You can try Renjin (http://www.renjin.org):
你可以试试人津(http://www.renjin.org):
"Renjin is a JVM-based interpreter for the R language for statistical computing. This project is an initiative of BeDataDriven, a company providing consulting in analytics and decision support systems."
“Renjin 是用于统计计算的 R 语言的基于 JVM 的解释器。该项目是 BeDataDriven 的一项倡议,BeDataDriven 是一家提供分析和决策支持系统咨询的公司。”
回答by Venkat
As @Seidr said, using rJava we can run R code directly from Java methods.
正如@Seidr 所说,使用 rJava 我们可以直接从 Java 方法运行 R 代码。
Open R console and install rJava package
install.packages("rJava")
Now you will find rJava in "R home\library"
Now in eclipse, add
JRI.jar
,JRIEngine.jar
,REngine.jar
to project build path. These jars are available in "R home\library\rJava\jri"- Create Rengine object.
Now create two vectors, add them and store in another variable. Now print that result variable in console.
Rengine engine = new Rengine(new String[]{"--no-save"},false,null); String aVector = "c(1,2,3)"; String bVector = "c(4,5,6)"; engine.eval("a<-"+aVector); engine.eval("b<-"+bVector); engine.eval("c<-a+b"); System.out.println("Sum of two vectors : c = "+engine.eval("c"));
打开 R 控制台并安装 rJava 包
install.packages("rJava")
现在你会在“ R home\library”中 找到rJava
现在在Eclipse中,加
JRI.jar
,JRIEngine.jar
,REngine.jar
项目的构建路径。这些 jars 在“ R home\library\rJava\jri” 中可用- 创建 Rengine 对象。
现在创建两个向量,将它们相加并存储在另一个变量中。现在在控制台中打印结果变量。
Rengine engine = new Rengine(new String[]{"--no-save"},false,null); String aVector = "c(1,2,3)"; String bVector = "c(4,5,6)"; engine.eval("a<-"+aVector); engine.eval("b<-"+bVector); engine.eval("c<-a+b"); System.out.println("Sum of two vectors : c = "+engine.eval("c"));
Hope below link helps (step-by-step procedure to integrate R in Java).
希望下面的链接有帮助(在 Java 中集成 R 的分步过程)。
http://www.codophile.com/how-to-integrate-r-with-java-using-rjava/
http://www.codophile.com/how-to-integrate-r-with-java-using-rjava/
回答by Gleb Kosteiko
The another option to run R on JVM is FastR. According FastR's documentation:
在 JVM 上运行 R 的另一个选项是FastR。根据 FastR 的文档:
FastR is an implementation of the R Language in Java atop Truffle, a framework for building self-optimizing AST interpreters.
FastR is currently available in two forms:
- As a pre-built binary with the Truffle implementations of Ruby and JavaScript for Linux and Mac OS X. Unfortunately, there is no Windows version available yet.
- As a source release on GitHub. This option does not come with Ruby or JavaScript.
FastR 是在 Truffle 之上用 Java 实现的 R 语言,Truffle 是一个用于构建自我优化 AST 解释器的框架。
FastR 目前有两种形式:
- 作为 Linux 和 Mac OS X 的 Ruby 和 JavaScript 的 Truffle 实现的预构建二进制文件。不幸的是,目前还没有可用的 Windows 版本。
- 作为 GitHub 上的源代码发布。此选项不随 Ruby 或 JavaScript 提供。