Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

r-faq

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.

like image 448
John Doucette Avatar asked Jul 22 '13 20:07

John Doucette


People also ask

How do I stop line of code running?

Last chance thing: If you can spot your another chunck of code starting in your console, you can try pressing the esc key to stop the code running.

How do I force stop a script in R?

The shortcut to interrupt a running process in R depends on the R software and the operating system you are using. However, if you are using RStudio on a Windows computer, you can usually use Esc to stop a currently executing R script. Then, we can press Esc to interrupt the loop.


1 Answers

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.

like image 101
John Doucette Avatar answered Nov 05 '22 14:11

John Doucette