Question

I am using Rcaller with Vaadin framework to make a program for running R-scripts from browser. One feature should be the ability to draw plots. This is not terribly difficult in itself, there are ways to do this with Rcaller. However, as far as I have seen, all of these ways involve saving the resulting plot from R to file system and then displaying it in Java.

This is most inconvenient and uses lot of time and resources. I would like a way to get the plot from R as a stream and direct it to Java, without having to save anything in between.

Is this in any way possible?

I looked up a function "grid.cap()". It takes a snapshot of the currently displayed plot and return the colors as a matrix. These can then be converted into RGB values. Would it be possible to stream these values, instead of the plot itself? This approach has at least one problem, it is really slow. Also, I am very doubtful of what exactly would be returned, as it depends on the state of the display device.

Was it helpful?

Solution

It seems it can't be done with grid.cap(), anyway, the problem may be handled with some configuration. I created a test file like that:

    RCaller caller = new RCaller();
    RCode code = new RCode();
    caller.setRscriptExecutable("C:\\Program Files\\R\\R-3.0.2\\bin\\Rscript.exe");
    code.R_require("grid");
    code.addRCode("dev.new(width=.5, height=.5)");
    code.addRCode("grid.rect()");
    code.addRCode("grid.text(\"hi\")");
    code.addRCode("cap <- grid.cap()");
    code.addRCode("returns <- list(cap=cap)");
    code.addRCode("dev.off()");
    caller.setRCode(code);
    caller.runAndReturnResult("returns");
    int[] dims = caller.getParser().getDimensions("cap");
    System.out.println("Names: " + caller.getParser().getNames());
    System.out.println("Returned matrix dimensions: "+dims[0]+" - "+dims[1]);

and this code produces this output:

Names: []
Returned matrix dimensions: 0 - 0

and running the generated script manually produces this:

In grid.Call(L_cap) : raster capture is not available for this device

Rscript can not detect a proper device for plotting, however, the generated RCaller script correctly runs and produces the expected output when it directly run in RGui.

> dim(returns$cap)
[1]  47 116
> head(returns$cap)
 [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]     [,9]     [,10]   
 [1,] "gray67" "gray67" "gray67" "gray67" "gray67" "gray67" "gray67" "gray67" "gray67" "gray67"
 [2,] "gray67" "gray67" "gray67" "gray67" "gray67" "gray67" "gray67" "gray67" "gray67" "gray67"

In my opinion, please forgive me if I wrong, the problem is to find a graphics device or a configuration setting for one of the current devices that runs with Rscript.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top