2

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.

4

1 回答 1

2

好像用grid.cap()是不行的,反正问题可以通过一些配置来处理。我创建了一个这样的测试文件:

    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]);

这段代码产生了这个输出:

Names: []
Returned matrix dimensions: 0 - 0

并手动运行生成的脚本会产生:

在 grid.Call(L_cap) 中:光栅捕获不适用于此设备

Rscript 无法检测到合适的绘图设备,但是,生成的 RCaller 脚本在 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"

在我看来,如果我错了,请原谅我,问题是为当前运行 Rscript 的设备之一找到图形设备或配置设置。

于 2014-04-28T09:47:27.557 回答