9

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)?

averageman
  • 893
  • 3
  • 12
  • 19

3 Answers3

15

Several options come to mind:

  1. within R: use sink() to divert output to a file, see help(sink)

  2. on the shell: Rscript myscript.R 2>&1 >/dev/null

  3. edit the code :)

  4. 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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 1
    Funny. 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
  • 1
    Feel 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
6

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)

damienfrancois
  • 52,978
  • 9
  • 96
  • 110
0

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.

Huanfa Chen
  • 577
  • 6
  • 15