In RCaller the methods startPlot() and endPlot() work like their R counterparts like png(), pdf(), bmp() for starting a file device and dev.off() for finalizing the plot.
After using the startPlot() you should plot something using R's graphics functions.
This very basic examples will give an idea for generating plots using 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();
This example creates a double array of values 1, 4, 3, 5, 6, 10 and pass them to R using the method addDoubleArray. The method startPlot returns a File object which is possibly created in your temp directory. The usual R expression
plot(x, pch=19)
draws a plot, but this time not on the screen but in the generated file by method startPlot().
After calling the method endPlot() we can finalize the process by calling
caller.runOnly();
so all the directives are converted to R code and passed to R. Now we can show the content in Java:
code.showPlot(file);
Here is the whole example:
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());
}
You can see the links for the example given here and further reading:
Basic plotting using RCaller
Journal research paper
Unpublished research paper for RCaller 3