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.

Learn to be a Pythonista!

Introducing Python

Learn to be a Pythonista! Try Python

Easy to understand and fun to read, Introducing Python is ideal for beginning programmers as well as those new to the language. Author Bill Lubanovic takes you from the basics to more involved and varied topics, mixing tutorials with cookbook-style code recipes to explain concepts in Python 3. End-of-chapter exercises help you practice what you learned.

$ Check price
39.9939.99Amazon 4.5 logo(37+ reviews)

More Python resources

Take your Linux skills to the next level!

Command Line Kung Fu

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

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources