在R中,如何绘制到内存缓冲区而不是文件中? [英] In R, how to plot into a memory buffer instead of a file?

查看:104
本文介绍了在R中,如何绘制到内存缓冲区而不是文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using JRI to generate ggplot2 plots from Java. Currently I have to write plots to disk. How do I do this without going through files, i.e. just rendering the plots in memory?

我尝试使用Cairo程序包将其绘制到textConnection上,但是如果没有"R Connections补丁",该功能将无法正常工作.

I tried using the Cairo package to plot to a textConnection, but that doesn't work without the "R Connections Patch," which after some Googling turns out to be ancient history.

推荐答案

主要来自 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抽象.]

[Update: JRI can handle raws, you just need to use the REngine abstractions and not the JRI ones.]

这篇关于在R中,如何绘制到内存缓冲区而不是文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆