Java 中的 R
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2180235/
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
R from within Java
提问by wtouw
What's the best way to call R functionality from within Java?
在 Java 中调用 R 功能的最佳方式是什么?
I'm looking for a quick, easy and reliable way to make standard 2d scatter plots and histograms in R using my Java applications. I was wondering which packages/interfaces that came up in a quick Google search would be most convenient to use.
我正在寻找一种快速、简单和可靠的方法来使用我的 Java 应用程序在 R 中制作标准的二维散点图和直方图。我想知道在 Google 快速搜索中出现的哪些包/接口最方便使用。
I look forward to your suggestions!
我期待着您的建议!
采纳答案by Shane
Use JRI: http://www.rforge.net/JRI/. It comes bundled with rJava, including some examples of usage.
使用 JRI:http://www.rforge.net/JRI/ 。它与 rJava 捆绑在一起,包括一些使用示例。
A very simple example would be like this:
一个非常简单的例子是这样的:
import java.io.*;
import java.awt.Frame;
import java.util.Enumeration;
import org.rosuda.JRI.Rengine;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.RVector;
import org.rosuda.JRI.RMainLoopCallbacks;
public class rJavaTest {
public static void main(String[] args) {
Rengine re=new Rengine(args, false, new TextConsole());
REXP x;
re.eval("print(1:10/3)");
System.out.println(x=re.eval("iris"));
RVector v = x.asVector();
if (v.getNames()!=null) {
System.out.println("has names:");
for (Enumeration e = v.getNames().elements() ; e.hasMoreElements() ;) {
System.out.println(e.nextElement());
}
}
if (true) {
System.out.println("Now the console is yours ... have fun");
re.startMainLoop();
} else {
re.end();
System.out.println("end");
}
}
}
回答by Yehoshaphat Schellekens
There is something new called http://www.renjin.org/
有个新东西叫http://www.renjin.org/
One thing i like it over JRIis deployment, While jrirequired that your application users will download R, renjindoes not, and it uses only the JVMto run.
我喜欢它的一件事JRI是部署,虽然jri要求您的应用程序用户下载 R,renjin但不会,并且它仅使用JVM来运行。
回答by AnthonyF
I have found that forking R as a process, attaching to the process's stdin, stdout, and stderr streams, and sending R commands via the input stream to be quite effective. I use the filesystem to communicate between R and my Java process. This way, I can have multiple R processes running from different threads in Java and their environments do not conflict with each other.
我发现将 R 作为一个进程分叉,附加到进程的 stdin、stdout 和 stderr 流,并通过输入流发送 R 命令非常有效。我使用文件系统在 R 和我的 Java 进程之间进行通信。这样,我可以让多个 R 进程从 Java 中的不同线程运行,并且它们的环境不会相互冲突。
回答by Steves
FastR is a GraalVM based implementation of R. Embedding it in a JVM application is as simple as:
FastR 是基于 GraalVM 的 R 实现。将其嵌入到 JVM 应用程序中非常简单:
Context ctx = Context.newBuilder("R").allowAllAccess(true).build();
ctx.eval("R", "sum").execute(new int[] {1,2,3});
For your concrete example, this example plots a scatter plot using the latticeR package, but the output is drawn into Graphics2Dobject.
对于您的具体示例,此示例使用latticeR 包绘制散点图,但输出被绘制到Graphics2Dobject.
Context context = Context.newBuilder("R").allowAllAccess(true).build();
// This R function opens FastR graphics device passing it Graphics2D object,
// then it plots the graph and closes the device
String src =
"library(grid); library(lattice); " +
"function(graphics, width, height, x, y) { " +
" awt(width, height, graphics);" +
" print(xyplot(as.vector(x) ~ as.vector(y)));" +
" dev.off();" +
"}";
Value showPlot = context.eval("R", src);
// Create a BufferedImage to use for the plotting
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setBackground(new Color(255, 255, 255));
graphics.clearRect(0, 0, WIDTH, HEIGHT);
// Invoke R plotting code and pass it graphics object
double[] x = new double[] {1.,2.,3.,4.};
double[] y = new double[] {1.,2.,3.,4.};
showPlot.execute(graphics, WIDTH, HEIGHT, x, y);
There is also an examplethat shows the plots inside a Swing window.
还有一个示例显示了 Swing 窗口内的绘图。
You can find more details about FastR in this medium post: https://medium.com/graalvm/faster-r-with-fastr-4b8db0e0dceb
您可以在这篇中等文章中找到有关 FastR 的更多详细信息:https://medium.com/graalvm/faster-r-with-fastr-4b8db0e0dceb
回答by mingxue
Packages or libraries for R with Java
带有 Java 的 R 包或库
Call R from Java
从 Java 调用 R
- RCaller
- Rserve
- JRI
- Rsession
- Renjin
- 调用者
- 保留
- 联合研究所
- 会话
- 人津
Call Java from R
从 R 调用 Java
- rJava
- rJava


