Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的R_Java_R_Plot - Fatal编程技术网

Java中的R

Java中的R,java,r,plot,Java,R,Plot,从Java内部调用R功能的最佳方式是什么 我正在寻找一种快速、简单、可靠的方法,使用我的Java应用程序在R中创建标准的2d散点图和直方图。我想知道谷歌快速搜索中出现的哪些软件包/界面最方便使用 我期待着您的建议 使用JRI:。它与rJava捆绑在一起,包括一些使用示例 一个非常简单的例子如下: import java.io.*; import java.awt.Frame; import java.util.Enumeration; import org.rosuda.JRI.Rengine

从Java内部调用R功能的最佳方式是什么

我正在寻找一种快速、简单、可靠的方法,使用我的Java应用程序在R中创建标准的2d散点图和直方图。我想知道谷歌快速搜索中出现的哪些软件包/界面最方便使用

我期待着您的建议

使用JRI:。它与rJava捆绑在一起,包括一些使用示例

一个非常简单的例子如下:

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");
        }
    }
}

我发现将R作为一个进程进行分叉,附加到进程的stdin、stdout和stderr流,并通过输入流发送R命令是非常有效的。我使用文件系统在R和Java进程之间进行通信。这样,我可以让多个R进程从Java中的不同线程运行,并且它们的环境不会相互冲突。

有一种新的称为


JRI
相比,我更喜欢它的一点是部署,而
JRI
要求应用程序用户下载R,而
renjin
不需要,它只使用
JVM
来运行。

使用Java的R的包或库

从Java调用R

  • RCaller
  • R服务
  • JRI
  • 会话
  • 仁津
从R调用Java

  • rJava

FastR是基于GraalVM的R实现。将其嵌入JVM应用程序非常简单:

Context ctx = Context.newBuilder("R").allowAllAccess(true).build();
ctx.eval("R", "sum").execute(new int[] {1,2,3});
对于您的具体示例,此示例使用
lattice
R包绘制散点图,但输出被绘制到
Graphics2D
对象中

    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);

在Swing窗口中有一个显示绘图的窗口


您可以在本文中找到有关FastR的更多详细信息:

Duplicate:您可以在不同的线程中运行多个JRI实例,并且不会发生冲突。Rcaller已经以更高效的方式执行这些操作。您从哪里获得作为参数传递给Rengine的TextConsole()?我不能编译这个程序,因为它。我不认为一个不完整的例子(没有必要的
import
语句)应该被选为一个可接受的答案@Shane:如果可以,请填补您提供的示例的空白。JRI有一个
examples
子目录,您可以在其中看到更完整的示例。在
rtest.java
中,您将发现上面包含了大量额外的内容,还可以找到缺少的
类TextConsole实现了RMAILoopCallbacks{…}