将绘图文本/二进制写入变量
有没有办法让 R 设备(postscript 会很棒)将输出写入变量而不是文件中?
例如,我知道这一点:
postscript(file="|cat")
plot(1:10)
dev.off()
将后记文本发送到 STDOUT
。如何将该文本放入 R 中的变量中?
Is there a way to have an R
Device (postscript would be great) write the output into a variable instead of a file?
For example I know this:
postscript(file="|cat")
plot(1:10)
dev.off()
Will send the postscript text to STDOUT
. How can I get that text into a variable within R
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已经成功地将绘图的二进制作为字符串放入 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.
Maybe this is helpful to you.
postscript 采用命令参数,因此 postscript(file="",command="|cat")
postscript takes a command argument, hence postscript(file="",command="|cat")
你到底为什么要这么做? R 并不是一个很好的处理 Postscript 文件的系统。如果没有别的事,您可以使用 tempfile() 将图像写入文件,然后您可以使用标准文件函数读取该文件。如果你想要更花哨,你也许可以使用 fifo() 管道,但我怀疑它会快得多。但我怀疑你最好采用不同的方法。
Why on earth would you want to do that? R is not a very good system for manipulating Postscript files. If nothing else, you can use tempfile() to write the image to a file, which you can then read in using standard file functions. If you wanted to be fancy, you could perhaps use fifo() pipes, but I doubt it'll be much faster. But I suspect you'd be better off with a different approach.
您应该能够按如下方式使用 textConnection。
但
string
仍然为空 - 也许是一个错误?You should be able to use a textConnection as follows.
But
string
remains blank - maybe a bug?