How can I redirect R warning messages to STDOUT?

泪湿孤枕 提交于 2019-12-18 15:19:24

问题


I'm using a grid engine to run R scripts. The STDERR is taken seriously under this setup, so I would like to keep it clean and have only real/serious/fatal errors printed to STDERR.

The problem is my R script generate various STDERR messages which are not really serious warnings... for example, scan seems to print to STDERR the number of items it read.

Can I redirect (from within R) STDERR to STDOUT?


回答1:


Look at the help page for sink():

‘sink’ diverts R output to a connection. If ‘file’ is a character string, a file connection with that name will be established for the duration of the diversion.

Normal R output (to connection ‘stdout’) is diverted by the default ‘type = "output"’. Only prompts and (most) messages continue to appear on the console. Messages sent to ‘stderr()’ (including those from ‘message’, ‘warning’ and ‘stop’) can be diverted by ‘sink(type = "message")’ (see below).




回答2:


@Dirk already provided the answer, but I would just add that you can use stdout() to get a connection to the STDOUT. You can use this in any output function to direct output there.



来源:https://stackoverflow.com/questions/4112896/how-can-i-redirect-r-warning-messages-to-stdout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!