在 RCaller 中,startPlot()和endPlot()方法的工作方式与 R 中的对应方法类似,例如用于启动文件设备的 png()、pdf()、bmp() 和用于完成绘图的dev.off() 。
使用startPlot()后,您应该使用 R 的图形函数绘制一些东西。
这个非常基本的示例将提供使用 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());
}
您可以查看此处给出的示例的链接并进一步阅读:
使用 RCaller 进行基本绘图
期刊研究论文
RCaller 3 未发表的研究论文