抑制“空设备” 在批处理模式下使用 R 输出

发布于 2024-07-16 19:14:56 字数 399 浏览 3 评论 0原文

我有许多 bash 脚本,它们调用 R 脚本来绘制内容。 类似这样的:

#!/bin/bash
R --vanilla --slave <<RSCRIPT
cat("Plotting $1 to $2\n")
input <- read.table("$1")
png("$2")
plot(as.numeric(input[1,]))
dev.off()
RSCRIPT

问题是,尽管有 --slave,但对 dev.off() 的调用仍会打印消息 null device 1。 一旦完成了很多绘图,或者对于绘制到多个文件的更复杂的脚本,这就会变得非常麻烦。

有什么办法可以抑制这个消息吗?

I have a number of bash scripts which invoke R scripts for plotting things. Something like:

#!/bin/bash
R --vanilla --slave <<RSCRIPT
cat("Plotting $1 to $2\n")
input <- read.table("$1")
png("$2")
plot(as.numeric(input[1,]))
dev.off()
RSCRIPT

The problem is that despite --slave, the call to dev.off() prints the message null device 1. Once there are a lot of plots being done, or for more complex scripts which plot to a number of files, this gets to be a real hassle.

Is there some way to suppress this message?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

请持续率性 2024-07-23 19:14:56

我不知道有什么充分的理由,dev.off()png() 等设备相关函数不同,它返回一个值:“新设备的编号和名称活动设备。” 该值是回显到标准输出的值。

因此,只需将其放在某个地方即可抑制它,即

garbage <- dev.off()

For no good reason I'm aware of, dev.off(), unlike device related functions like png() returns a value: "the number and name of the new active device." That value is what's being echoed to stdout.

Suppressing it can thus be achieved by just putting it somewhere, i.e.,

garbage <- dev.off()
婴鹅 2024-07-23 19:14:56

R 的好处之一是您可以查看许多函数的源代码:

> dev.off
function (which = dev.cur()) 
{
    if (which == 1) 
        stop("cannot shut down device 1 (the null device)")
    .Internal(dev.off(as.integer(which)))
    dev.cur()
}
<environment: namespace:grDevices>

因此它调用 .Internal(dev.off(...)) 然后返回 dev.cur(),这我想如果您打开了多个设备,这样您就知道哪一个设备处于活动状态,这会很有用。 您可以在脚本中使用 .Internal(dev.off(as.integer(dev.cur()))) ,甚至修补 dev.off 以仅返回dev.cur() 的值(如果不是空设备),并将补丁发送给 R 的维护者。

此外,graphics.off() 调用 < code>dev.off() 适用于所有设备,并且不返回任何内容。

One of the nice things about R is that you can view the source of many functions:

> dev.off
function (which = dev.cur()) 
{
    if (which == 1) 
        stop("cannot shut down device 1 (the null device)")
    .Internal(dev.off(as.integer(which)))
    dev.cur()
}
<environment: namespace:grDevices>

So it calls .Internal(dev.off(...)) and then returns dev.cur(), which I suppose would be useful if you have several devices open so you know which one became active. You could use .Internal(dev.off(as.integer(dev.cur()))) in your script, or even patch dev.off to only return the value of dev.cur() if it is something else than the null device, and send the patch to the maintainers of R.

Also, graphics.off() calls dev.off() for all devices and doesn't return anything.

桃扇骨 2024-07-23 19:14:56

最近遇到了同样的问题,并注意到这里的答案中没有提到另一种可能性:

invisible(dev.off())

这将隐藏 dev.off() 的输出,并且不会像将输出分配给 < code>garbage 变量:garbage <- def.off() 会。

Ran into the same issue recently and noticed that one more possibility is not mentioned in the answers here:

invisible(dev.off())

This will hide the output from dev.off() and will not create additional variables unlike assigning the output to garbage variable: garbage <- def.off() would.

执手闯天涯 2024-07-23 19:14:56

另一种选择是使用 sink() 并将所有内容输出到日志文件,这样您就可以在需要时检查绘图是否有效。

Another option would be to use sink() and output everything to a log file, so you can check up on whether the plots worked if you need to.

染墨丶若流云 2024-07-23 19:14:56

您可以使用 littler 代替,这是 a) 一种更简单的方法写 R '脚本' 和 b) 抑制输出,这样你就会得到 dev.off 静默的副作用:

$ foo.r /tmp/foo.txt /tmp/foo.png
Plotting /tmp/foo.txt to /tmp/foo.png
$ cat /tmp/foo.r
#!/usr/bin/r
cat("Plotting", argv[1], "to", argv[2], "\n")
input <- read.table(argv[1])
png(argv[2])
plot(as.numeric(input[1,]))
dev.off()
$

Rscript 可能也会工作; 我倾向于选择较小的。

You can use littler instead which is a) an easier way to write R 'scripts' and b) suppresses output so you get the side effect of dev.off being silent:

$ foo.r /tmp/foo.txt /tmp/foo.png
Plotting /tmp/foo.txt to /tmp/foo.png
$ cat /tmp/foo.r
#!/usr/bin/r
cat("Plotting", argv[1], "to", argv[2], "\n")
input <- read.table(argv[1])
png(argv[2])
plot(as.numeric(input[1,]))
dev.off()
$

Rscript will probably work too; I tend to prefer littler.

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