Looking for an options which let me to redirect R diagnostic messages (produces by message()
) to stdout
, not stderr
as it is by defaul
The OP showed the execution happening via the Rscript command and using some I/O redirection. If you want to use redirection to log everything and only show to console on error, the best method I've found is to use ||
to check if the script had non-zero exit status before printing to screen:
Rscript myrscript.R > temp.log 2>&1 || cat temp.log
This method relies strictly on the exit code for printing, which only partly gets around message()
going to stderr, but I thought this example helpful to mention since messages won't necessarily trigger a non-zero exit status and you can continue to log quietly with this method.
If you'd like to go one step further and keep appending to a single log file, then this will work:
Rscript myrscript.R > temp.log 2>&1 || cat temp.log && cat temp.log >> persistent.log && rm temp.log
The pseudocode for this command is:
stderr
and stdout
into temp.log