我正在尝试使用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);发布于 2016-09-05 19:20:24
在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返回一个文件对象,该对象可能是在临时目录中创建的。通常的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());
}您可以查看此处给出的示例的链接,并进一步阅读:
Journal research paper
https://stackoverflow.com/questions/38465052
复制相似问题