I am using Rscript to run an R script but I get a lot of output on my screen. Can I run Rscript in silent mode (meaning without any screen output)?
-
What OS are you using? – damienfrancois Aug 05 '14 at 15:09
-
I know that Octave can be called with a --quiet option. I wonder if there is something similar in R. – averageman Aug 05 '14 at 15:11
-
Sure it can, try `R --help`, but that only affects R itself. But there is also `--slave` .... – Dirk Eddelbuettel Aug 05 '14 at 15:13
3 Answers
Several options come to mind:
within R: use
sink()
to divert output to a file, seehelp(sink)
on the shell:
Rscript myscript.R 2>&1 >/dev/null
edit the code :)
on Linux, use our littler frontend as it runs in
--slave
mode by default :)
Options 3 is the most involved but possibly best. You could use a logging scheme where you print / display in "debug" or "verbose" but not otherwise. I often do that, based on a command-line toggle given to the script.

- 360,940
- 56
- 644
- 725
-
1Funny. I tried your options and they more or less worked. Some things were not hidden though. I am using the Amelia II package and its information (like version and copyright) are still being displayed even after using sink and/or 2>&1 >/dev/null... – averageman Aug 07 '14 at 15:08
-
1Feel free to accept or upvote then. Package startup messages are different, and there are some questions here explaining the details, see eg [this other SO question](http://stackoverflow.com/questions/6279808/r-suppress-startupmessages-from-dependency). You can nuke those by wrapping `suppressPackageStartupMessages()` around your `library()` call; I often just use `suppressMessages()`. – Dirk Eddelbuettel Aug 07 '14 at 15:14
-
Option 2 has the output redirection reversed, [see here](http://teaching.idallen.com/cst8207/13w/notes/200_redirection.html#redirecting-both-stdout-and-stderr-using-21). If you test with `Rscript -e "library(tidyverse)" 2>&1> /dev/null` you still see output, but it is all silenced with `Rscript -e "library(tidyverse)" > /dev/null 2>&1` – pgcudahy May 14 '19 at 14:18
You can redirect the output with
Rscript myscript.R >& >/dev/null (linux)
or
Rscript myscript.R >$null (windows)
or use R
directly:
R --quiet --vanilla < myscript.R
or
R CMD BATCH myscript.R
(That last version writes the output to a file myscript.Rout
)

- 52,978
- 9
- 96
- 110
-
I think I can't use R directly because I am expecting some arguments from the command line... – averageman Aug 07 '14 at 15:10
One more option: if you want to separate the output and the error message into different files, which makes it easier to identify the problems, you can use the command on the shell:
Rscript myscript.R >a.Rout 2>a.Rerr
This will write the program output to a.Rout and the error messages to a.Rerr. Note that the files of a.Rout and a.Rerr should be removed beforehand, to avoid an error.

- 577
- 6
- 15