gpt4 book ai didi

r - 在R中,如何绘制到内存缓冲区而不是文件中?

转载 作者:行者123 更新时间:2023-12-04 06:26:01 24 4
gpt4 key购买 nike

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

我尝试使用Cairo程序包将其绘制到textConnection上,但是没有“R Connections补丁”就无法正常工作,在经过谷歌搜索之后,该补丁被证明是古老的历史。

最佳答案

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

关于r - 在R中,如何绘制到内存缓冲区而不是文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7171523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com