18

I would like to suppress output in R when I run my R script from the command prompt.

I tried numerous options including --slave and --vanilla. Those options lessens the amount of text outputted.

I also tried to pipe the output to NUL but that didn't help.

zx8754
  • 52,746
  • 12
  • 114
  • 209
defoo
  • 5,159
  • 11
  • 34
  • 39
  • 1
    Maybe R is printing to stderr if `> NUL` doesn't help. Try appending `2>&1` as well. – Joey Mar 23 '10 at 17:10

2 Answers2

35

Look at help(sink) to do that. On Unix I'd do

sink("/dev/null")    # now suppresses
....                 # do stuff
sink()               # to undo prior suppression, back to normal now

and the Windows equivalent (with a tip-of-the-hat to Johannes) is

sink("NUL")
....
sink()
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 3
    Usually the Windows equivalent is `NUL`. However, it may well be that `CreateFile`, &c. won't be able to open it and that it's largely special functionality in the shell which enables its workings. – Joey Mar 23 '10 at 17:08
  • Thank you -- I edited the answer accordingly (after testing that it does indeed work that way with `NUL` quoted properly). – Dirk Eddelbuettel Mar 23 '10 at 17:32
  • 1
    Hi @DirkEddelbuettel, I noted that this doesn't for dumping errors from try() sections, sink(file="NUL", type="message") complains that "NUL" isn't a file connection, in those cases one needs to use the try(stop("error msg"), silent=TRUE) for it to work as expected. Just wanted to add this to your answer in case anyone else stumbles upon this answer. – Max Gordon Mar 23 '13 at 16:04
  • So, is there a universal (platform independent) way to achieve this other than simply supplying a `tempfile()`? – jan-glx Jan 15 '18 at 14:31
3

Since R (>= 3.6.0), there exists a platform-independent alternative to Dirk Eddelbuettel's answer. Simply type

sink(nullfile())    # suppress output
....                # your code
sink()              # end suppressing output

to suppress output on both Linux and Windows.

Lukas D. Sauer
  • 355
  • 2
  • 11
  • 1
    If including this in a function, then can wrap inside `on.exit(sink())` - otherwise unexpected errors in the function may leave you wondering why you have no output to the console. – CCID Feb 08 '23 at 15:33