05. Scripts

An R script is a file containing R code that can be run by itself.

Running scripts in RStudio

We have already tried writing an R script in RStudio, and running it in our functions lesson. It's easy to opening up a new page - simply click the +Page icon on the top left, and select R Script.

How to open a new R Script in RStudio.
How to open a new R Script in RStudio.

To run, simply click the Source button.

Running scripts with just the console

If you have an R script but no RStudio installed, then you may do so with R's batch mode. This mode, as opposed to the interactive mode you've seen thus far, simply passes the script as an argument to the R program.

In this way, you can have a record of your analyses and easily repeat them, and share the script with your friends.

For batch mode, use the Rscript command from the command line. Suppose a file, named test.r within your current directory contains the following:

sayHello <- function() {

As you can see, this is a simple function that prints "hello!". Now to run it, use the Rscript command from the command line.

$ Rscript test.r
[1] "hello!"


Another way to run scripts straight from the command line is the R CMD BATCH command. This will not just run the commands listed, but output the results into a file with the .Rout extension.

R version 3.2.1 (2015-06-18) -- "World-Famous Astronaut"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.4.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
  Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> sayHello >- function() {
+   print("hello!")
+ }
> sayHello()
[1] "hello!"
> proc.time()
   user  system elapsed
  0.236   0.039   0.281

You can see that R was initialized and each line of your R script gets run as if you typed them out yourself.

On Windows

On windows, simply pull down a menu in your R.exe and run the script.

Take your Linux skills to the next level!

How Linux Works

Take your Linux skills to the next level! Try Linux & UNIX

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Become a Bioinformatics Whiz!

Bioinformatics Data Skills

Become a Bioinformatics Whiz! Try Bioinformatics

Learn the best practices used by academic and industry professionals. Bioinformatics Data Skills give a great overview to the Linux Command Line, Github, and other essential tools used in the trade. This book bridges the gap between knowing a few programming languages and being able to utilize the tools to analyze large amounts of biological data.

$ Check price
49.9949.99Amazon 4.5 logo(7+ reviews)

More Bioinformatics resources