Search code examples
rr-faq

How can I avoid having my R script printed every time I run it?


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.


Solution

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