将绘图文本/二进制写入变量

发布于 2024-08-08 22:28:04 字数 193 浏览 2 评论 0原文

有没有办法让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蹲在坟头点根烟 2024-08-15 22:28:04

我已经成功地将绘图的二进制作为字符串放入 R 变量中。它有一些读/写开销。在下面的代码片段中,R 将绘图保存为临时文件并将其读回。

## 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="")

也许这对您有帮助。

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="")

Maybe this is helpful to you.

梦明 2024-08-15 22:28:04

postscript 采用命令参数,因此 postscript(file="",command="|cat")

postscript takes a command argument, hence postscript(file="",command="|cat")

下雨或天晴 2024-08-15 22:28:04

你到底为什么要这么做? 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.

烟酒忠诚 2024-08-15 22:28:04

您应该能够按如下方式使用 textConnection。

tc <- textConnection("string", "w")

postscript(tc)
plot(1:10)
dev.off()

string 仍然为空 - 也许是一个错误?

You should be able to use a textConnection as follows.

tc <- textConnection("string", "w")

postscript(tc)
plot(1:10)
dev.off()

But string remains blank - maybe a bug?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文