bash 在批处理模式下使用 R 抑制“空设备”输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/750703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Suppressing "null device" output with R in batch mode
提问by blahdiblah
I have a number of bash scripts which invoke R scripts for plotting things. Something like:
我有许多 bash 脚本,它们调用 R 脚本来绘制事物。就像是:
#!/bin/bash
R --vanilla --slave <<RSCRIPT
cat("Plotting to \n")
input <- read.table("")
png("")
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.
问题是,尽管如此--slave,调用 来dev.off()打印消息null device 1。一旦完成了大量绘图,或者对于绘制到多个文件的更复杂的脚本,这将是一个真正的麻烦。
Is there some way to suppress this message?
有没有办法抑制这个消息?
采纳答案by Jouni K. Sepp?nen
One of the nice things about R is that you can view the source of many functions:
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>
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.offto 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.
所以它会调用.Internal(dev.off(...))然后返回 dev.cur(),我认为如果您打开了多个设备以便您知道哪个设备处于活动状态,这会很有用。你可以.Internal(dev.off(as.integer(dev.cur())))在你的脚本中使用,甚至补丁dev.off,dev.cur()如果它不是空设备,则只返回值,然后将补丁发送给 R 的维护者。
Also, graphics.off()calls dev.off()for all devices and doesn't return anything.
此外,graphics.off()调用dev.off()所有设备并且不返回任何内容。
回答by blahdiblah
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.
无缘无故我知道dev.off(),与设备相关的功能不同,比如png()返回一个值:“新活动设备的编号和名称”。该值是回显到标准输出的值。
Suppressing it can thus be achieved by just putting it somewhere, i.e.,
因此可以通过将它放在某个地方来抑制它,即,
garbage <- dev.off()
回答by Karolis Koncevi?ius
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 garbagevariable: garbage <- def.off()would.
这将隐藏输出dev.off()并且不会创建额外的变量,这与将输出分配给变量不同garbage:garbage <- def.off()会。
回答by Dirk Eddelbuettel
You can use littlerinstead 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:
您可以使用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 will probably work too; I tend to prefer littler.
Rscript 也可能会起作用;我倾向于更小的。
回答by David Lawrence Miller
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.
另一种选择是使用sink()所有内容并将其输出到日志文件中,这样您就可以根据需要检查绘图是否有效。

