Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rscript in silent mode

Tags:

r

rscript

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

like image 409
averageman Avatar asked Aug 05 '14 15:08

averageman


People also ask

What is Rscript vanilla?

In a newer version of R, Rscript or Rscript. exe is a command mode of R without a terminal and can be used as a shell script. It also provides a more convenient interface than traditional ways as SHELL> R --vanilla < a_rscript. r.

Where is Rscript?

On Mac/Linux, the Rscript path is typically /usr/local/bin/Rscript or /usr/bin/Rscript . On Windows, the path for R version X.Y.Z is typically C:/Program Files/R/R-X. Y.Z/bin/Rscript.exe . If you don't specify a path, rscript will try to find the Rscript executable on its own by searching commonly used paths.


2 Answers

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.

like image 110
Dirk Eddelbuettel Avatar answered Sep 19 '22 22:09

Dirk Eddelbuettel


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)

like image 35
damienfrancois Avatar answered Sep 18 '22 22:09

damienfrancois