抑制“空设备” 在批处理模式下使用 R 输出
我有许多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不知道有什么充分的理由,
dev.off()
与png()
等设备相关函数不同,它返回一个值:“新设备的编号和名称活动设备。” 该值是回显到标准输出的值。因此,只需将其放在某个地方即可抑制它,即
For no good reason I'm aware of,
dev.off()
, unlike device related functions likepng()
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.,
R 的好处之一是您可以查看许多函数的源代码:
因此它调用
.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:
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 patchdev.off
to only return the value ofdev.cur()
if it is something else than the null device, and send the patch to the maintainers of R.Also,
graphics.off()
callsdev.off()
for all devices and doesn't return anything.最近遇到了同样的问题,并注意到这里的答案中没有提到另一种可能性:
这将隐藏
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:
This will hide the output from
dev.off()
and will not create additional variables unlike assigning the output togarbage
variable:garbage <- def.off()
would.另一种选择是使用
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.您可以使用 littler 代替,这是 a) 一种更简单的方法写 R '脚本' 和 b) 抑制输出,这样你就会得到 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:
Rscript will probably work too; I tend to prefer littler.