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.