我正在使用JRI从Java生成ggplot2图.目前我必须将图表写入磁盘.如何在不经过文件的情况下完成此操作,即只在内存中渲染图形?
我尝试使用Cairo包绘制到一个textConnection,但是如果没有"R Connections Patch",这在谷歌搜索结果是古老的历史之后就不行了.
Yan*_*ang 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
Run Code Online (Sandbox Code Playgroud)
[更新:JRI 可以处理原始数据,你只需要使用REngine抽象而不是JRI抽象.]