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() {
  print("hello!")
}
 
sayHello()

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!"

R CMD BATCH

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.

Become a Bioinformatics Whiz!

Introduction to Bioinformatics Vol. 2

Become a Bioinformatics Whiz! Try Bioinformatics

This is Volume 2 of Bioinformatics Algorithms: An Active Learning Approach. This book presents students with a light-hearted and analogy-filled companion to the author's acclaimed course on Coursera. Each chapter begins with an interesting biological question that further evolves into more and more efficiently solutions of solving it.

$ Check price
49.9949.99Amazon 5 logo(5+ reviews)

More Bioinformatics resources

Learn to be a Pythonista!

Programming Python

Learn to be a Pythonista! Try Python

Programming Python shows in-depth tutorials on the language's number of application domains including: system administration, GUIs, the Web, networking, front-end scripting layers, and more. This book focuses on commonly used tools and libraries to give you a comprehensive understanding of Python’s many roles in practical, real-world programming.

$ Check price
64.9964.99Amazon 4 logo(56+ reviews)

More Python resources

Ad