将绘图文本/二进制写入变量 [英] Write Plot Text/Binary into Variable
本文介绍了将绘图文本/二进制写入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法让 R
设备(postscript 会很棒)将输出写入变量而不是文件?
Is there a way to have an R
Device (postscript would be great) write the output into a variable instead of a file?
例如我知道:
postscript(file="|cat")
plot(1:10)
dev.off()
将 postscript 文本发送到 STDOUT
.如何将该文本放入 R
中的变量中?
Will send the postscript text to STDOUT
. How can I get that text into a variable within R
?
推荐答案
我已经成功地将图的二进制作为字符串转换为 R 变量.它有一些读/写开销.在下面的代码段中,R 将绘图保存为临时文件并将其读回.
I've had success in getting the Binary of a plot into an R variable as a string. Its got some read/write overhead. In the snippet below, R saves the plot as a temp file and reads it back in.
## create a plot
x <- rnorm(100,0,1)
hist(x, col="light blue")
## save plot as temp file
png(filename="temp.png", width=500, height=500)
print(p)
dev.off()
## read temp file as a binary string
plot_binary <- paste(readBin("temp.png", what="raw", n=1e6), collapse="")
也许这对你有帮助.
这篇关于将绘图文本/二进制写入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文