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!

System Admin Handbook

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

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources

Become a Bioinformatics Whiz!

Introduction to Bioinformatics Vol. 1

Become a Bioinformatics Whiz! Try Bioinformatics

If you're looking for a fun and easy entry point into bioinformatics algorithms, this book it just for you! Filled with graphics, and written in a light-hearted and humorous story-telling persona, Bioinformatics Algorithms guides you through the intricacies of the problems faced in biology, and the clever solutions used to solve them.

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

More Bioinformatics resources