9

JRI を使用して、Java から ggplot2 プロットを生成しています。現在、プロットをディスクに書き込む必要があります。ファイルを介さずに、つまりメモリ内のプロットをレンダリングするだけでこれを行うにはどうすればよいですか?

Cairo パッケージを使用して textConnection にプロットしようとしましたが、「R Connections Patch」がないと機能しません。これは、Google で検索した結果、古い歴史であることが判明しました。

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は raw を処理できます。JRI のものではなく、REngine の抽象化を使用するだけで済みます。]

于 2011-08-24T19:58:18.883 に答える