Search code examples
rstdoutstderrrscript

Suppressing R script's output


I'm creating an R script using Rscript in the shebang and outputting to stdout. This works fine, but R outputs a lot of logging/debugging messages to stderr (e.g., whether its imports worked fine, the status of various functions, how the weather is today, etc.). How do I suppress this?

I'm aware of suppressWarnings and suppressMessages, but these are functions that wrap whatever function you want to silence. I could enclose my whole script into an anonymous function and then pass that to these, but that seems a bit of a weird idiom. (Still, if that's the only way, then I guess that's the way it is...)

Another option would be to embed the R script into a bash script and use bash's IO redirection. Again, that seems to be a roundabout solution, but I'm open to it!

I was just wondering if there's a better way?


Solution

  • If you want to completely suppress all stderr messages, put this line early in your script:

    sink(file("/dev/null", "w"), type="message")
    

    Obviously this isn't going to help with debugging...