9

我正在使用 JRI 从 Java 生成 ggplot2 图。目前我必须将绘图写入磁盘。我如何在不通过文件的情况下执行此操作,即仅在内存中渲染绘图?

我尝试使用 Cairo 包绘制到 textConnection,但如果没有“R Connections Patch”,那将无法正常工作,经过一番谷歌搜索后发现这是古代历史。

4

1 回答 1

12

主要来自https://stat.ethz.ch/pipermail/r-devel/2010-August/058253.html

library(Cairo)
library(png)
library(ggplot2)

Cairo(file='/dev/null')

qplot(rnorm(5000)) # your plot

# hidden stuff in Cairo
i = Cairo:::.image(dev.cur())
r = Cairo:::.ptr.to.raw(i$ref, 0, i$width * i$height * 4)
dim(r) = c(4, i$width, i$height) # RGBA planes
# have to swap the red & blue components for some reason
r[c(1,3),,] = r[c(3,1),,]
# now use the png library
p = writePNG(r, raw()) # raw PNG bytes

[更新:JRI可以处理原始数据,您只需要使用 REngine 抽象而不是 JRI 抽象。]

于 2011-08-24T19:58:18.883 回答