20

Suppose I have an R script:

library('nnet')    
something <- runif(50); 
print(something) 

When I run this script from the command line, it prints:

> library('nnet')
> something <- runif(5); 
> print(something)
 [1] 0.04665518 0.93574275 0.96387299 0.07410239 0.92834019

I would like it to print only:

[1] 0.04665518 0.93574275 0.96387299 0.07410239 0.92834019

and I cannot figure out how to do this. sink("/dev/null") doesn't do anything, redirecting stderr manually doesn't do anything, and I can't find any useful information on this.

Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
John Doucette
  • 4,370
  • 5
  • 37
  • 61
  • 5
    How are you running it "at the command line"? `Rscript foo.R` only prints what you want... – Joshua Ulrich Jul 22 '13 at 20:26
  • 4
    And within R `source("foo.R")` would normally not show anything of the script unless you ask it to via `source("foo.R", echo = TRUE)`. Please show us **exactly** what you are trying/doing. – Gavin Simpson Jul 22 '13 at 20:27
  • 1
    Rscript seems to have been what I was missing. Other posts on SE and elsewhere run scripts with "R < foo.R", which is evidently not the way to do it. Thanks. – John Doucette Jul 22 '13 at 20:29

5 Answers5

22

Resolution is to run with Rscript, and not with R. Examples elsewhere (e.g. How can I read command line parameters from an R script?), run scripts from the command line with

R --args args1 args2... < foo.R

running with

Rscript foo.R args1 args2 ...

produces only the output, and not the script. It's also a much cleaner way to run scripts.

Community
  • 1
  • 1
John Doucette
  • 4,370
  • 5
  • 37
  • 61
  • 1
    +1 Minor point, "poorly done" is uncalled-for. R hasn't always had `RScript` & the Q&A you link to is over 3 years old now. I suspect the usage of `RScript` had not embedded itself in the psyche of many R users at that point. Note also that those examples were not answering your question - you don't want your script echoed, which is a different matter and it is wrong to criticise for a lack of precognitive skills. – Gavin Simpson Jul 22 '13 at 22:44
  • 1
    @GavinSimpson Fair point. I've updated the answer accordingly. – John Doucette Jul 22 '13 at 22:48
  • 3
    @JohnDoucette Excellent John. I wish more users on [so] and the [tag:r] tag were as civic minded as you have been today. – Gavin Simpson Jul 22 '13 at 22:53
  • Not sure if this is separate question: I was directed here when I was looking to turn *on* source echoing (like 'set -x' in bash) for Rscript -- I'm guessing the way to to do that is just to run ` – mpettis Apr 03 '17 at 21:28
2

Not an R user myself, but is this something that might be helpful to you? How can I run an 'R' script without suppressing output?

From the linked question:

specify print.eval parameter set to TRUE if you want to get only the output (and not the commands). If you would need the commands too, you should set echo to TRUE (which implies setting print.eval to TRUE).

For example:

source('myscript.R', print.eval = TRUE)
Community
  • 1
  • 1
Ben J. Boyle
  • 306
  • 1
  • 12
2
source( 'path/name/filnam.R' , verbose=FALSE)
IRTFM
  • 258,963
  • 21
  • 364
  • 487
2

For running in the terminal directly:

R --slave --args dense 12 0.98 < foo.R

For running R script from Python:

process = subprocess.Popen(["R --slave --args %s %d %.2f < /path/to/your/rscript/foo.R" % (, 12, 0.98) ], shell=True)
process.wait()

For running R script in terminal / command line and in the background, while suppress / avoid printing every line of the scripts and the output of the program, using R CMD BATCH as follow:

R CMD BATCH--slave foo.R 2>&1  foo.out &

See also this reference

Good Will
  • 1,220
  • 16
  • 10
2

For RStudio IDE (Version 1.1.383) in Windows:

Pressing Ctrl+Shift+Enter keys run entire script with echo (verbose)

Pressing Ctrl+Shift+S keys run entire script without echo (non-verbose)

M.A. Khan
  • 21
  • 2
  • Upvoted because I was looking for the Ctrl+Shift+S shortcut. But, at least for RStudio version 2022.07.0, the shortcut for running the entire script with echo is **Ctrl+Alt+R**. The shortcut **Ctrl+Shift+Enter** only runs the current chunk. – jorvaor Apr 27 '23 at 07:00