如何使用Rcaller在窗口中显示数据帧

时间:2016-07-19 17:36:12

标签: java r rcaller

我试图在java中使用Rcaller库在文件中显示数据帧。但它似乎不起作用。 以下代码是我尝试做的:

   RCaller caller = new RCaller();
   RCode code = new RCode(); 
   code.addRCode("a=table(data$rate, predArbreDecision)");  


   File file = code.startPlot();
   code.addRCode("as.data.frame.matrix(a)");
   caller.runOnly();
   ImageIcon ii = code.getPlot(file);
   code.showPlot(file);

1 个答案:

答案 0 :(得分:0)

在RCaller中,方法 startPlot() endPlot()的工作方式类似于他们的R对应物,如png(),pdf(),bmp(),用于启动文件设备和 dev.off()用于完成绘图。

使用 startPlot()之后,您应该使用R&#39的图形功能绘制一些内容。

这个非常基本的例子将给出使用RCaller生成图的想法:

  double[] numbers = new double[]{1, 4, 3, 5, 6, 10};

  code.addDoubleArray("x", numbers);

  File file = code.startPlot();
  System.out.println("Plot will be saved to : " + file);

  code.addRCode("plot(x, pch=19)");

  code.endPlot();

此示例创建值为1,4,3,5,6,10的双数组,并使用addDoubleArray方法将它们传递给R.方法startPlot返回一个File对象,该对象可能在临时目录中创建。通常的R表达式

plot(x, pch=19)

绘制一个图,但这次不是在屏幕上,而是在方法 startPlot()的生成文件中。

在调用方法endPlot()之后,我们可以通过调用

来完成该过程
caller.runOnly();

所以所有指令都转换为R代码并传递给R.现在我们可以在Java中显示内容:

code.showPlot(file);

以下是整个例子:

try {
  RCaller caller = RCaller.create();

  RCode code = RCode.create();


  double[] numbers = new double[]{1, 4, 3, 5, 6, 10};

  code.addDoubleArray("x", numbers);
  File file = code.startPlot();
  System.out.println("Plot will be saved to : " + file);
  code.addRCode("plot(x, pch=19)");
  code.endPlot();


  caller.setRCode(code);
  System.out.println(code.getCode().toString());

  caller.runOnly();
  code.showPlot(file);
} catch (Exception e) {
  Logger.getLogger(SimplePlot.class.getName()).log(Level.SEVERE, e.getMessage());
}

您可以在此处查看示例的链接并进一步阅读:

Basic plotting using RCaller

Journal research paper

Unpublished research paper for RCaller 3

相关问题