Introduction

Designed by Allison Horst

Figure 0.1: Designed by Allison Horst

Why use R?

  • It is a free software environment for statistical computing and graphics.
  • It is platform independent. Runs on Windows, Mac, Linux and others.
  • It has enormous functionality. Over 14,500 packages extend the functionality - some not yet available in any other software.
  • It is open-source. Everyone can take a look at the source code to see the exact computation. This is not the case with commercial software. This also enables users to spot bugs and eases verification.
  • It facilitates reproducible research. If the raw data is available and code is distributed every- one can reproduce and check the results. This enhances trust in analyses.
  • It has state-of-the-art graphics capabilities. You can easily generate publication ready high quality graphics.

How to install and download R

The first thing you need to do is to download and install R from CRAN on your computer. We will also install a software called RStudio, which is also a free and open-source integrated environment for R that makes it much easier for us to code. You should have these both already installed at the University PCs if you wish to work using the University PCs. RStudio is a user-friendly interface that has many functionalities to make the coding easy, such as it allows you to work on multiple scripts at a time.

As the practicals progress, you are encouraged to explore beyond the practicals and practise as much as you can; a willingness to experiment will make you a much better programmer. Before we get to that stage, however, you need to build some basic fluency in R. Today we begin with the fundamental building blocks of R and RStudio: the interface, reading in data, and basic commands.

To Install R (for Windows users)
1. Open an internet browser and go to CRAN website. 2. Click on the “Download R for Windows” link at the top of the page.
3. Click on the “install R for the first time” link at the top of the page.
4. Click “Download R for Windows” and save the executable file somewhere on your computer. Run the .exe file and follow the installation instructions.
5. Now that R is installed, you need to download and install RStudio.
See this video for step-by-step installation instructions if needed. Note that you should install the most recent version.

To install RStudio (for Windows users)
1. Go to RStudio website and click on the “Download RStudio” button.
2. Click on “Download RStudio Desktop.”
3. Click on the version recommended for your system, or the latest Windows version, and save the executable file. Run the .exe file and follow the installation instructions.
See this video for step-by-step installation instructions if needed. Note that you should install the most recent version.

To Install R (for Mac users)
1. Open an internet browser and go to CRAN website 2. Click on the “Download R for (Mac) OS X” link at the top of the page.
3. Click on the file containing the latest version of R under “Files.”
4. Save the .pkg file, double-click it to open, and follow the installation instructions. 5. Now that R is installed, you need to download and install RStudio. See this video for step-by-step installation instructions if needed. Note that you should install the most recent version.

To Install RStudio (for Mac users)
1. Go to RStudio website and click on the “Download RStudio” button. 2. Click on “Download RStudio Desktop.”
3. Click on the version recommended for your system, or the latest Mac version, save the .dmg file on your computer, double-click it to open, and then drag and drop it to your applications folder.

NOTE: If you already have R and RStudio installed, you should still visit these links to confirm that you have the most recent versions of these software. The most recent version of R can be found on the R Project for Statistical Computing page and the most recent version of RStudio can be found on the Download RStudio page. Please install the most recent versions before proceeding.

If you start RStudio you will see a window divided into four panes (please note, you might need to open the editor page by going to Files--> New File --> R Script).

RStudio

  • The Editor pane: R programs (scripts/codes) are a collection of functions. In the editor, we simply write our codes for the analysis. These R scripts can be saved and rerun whenever needed. The commands type in the editor are not executed. To send the currently selected line to the console and execute it simply type CTRL + ENTER or hit the run button (typically at top right corner of the Editor Window).

  • The Console pane: Here you can directly type commands after the > and execute them by hitting ENTER. This is actually R. If you see a + instead, R expects more output to finish and execute the command. This is often the case when you have forgotten a closing bracket. Simply hit ESC to abort the command and return to >.

  • The Environment pane: Here you can see which objects/value/data R knows about. It’s useful, because you can see the name and a summary of the objects. By clicking on them you can view the contents.

  • The Help/Packages/Plot/Files pane: Here is where the plots that you produce are shown. Moreover, you can search within the help or browse through packages.

I also like this analogy demonstrating “what is for what” in RStudio using an example from cooking! (Thanks to Jessica Ward).

You can adjust the size, fonts, and re-arrange these panes if you like. Here are some tips and tricks for you.

Installing packages

The version of R you just downloaded is the base R, which provides the basic statistical computing and graphics power. However, for extending the functionality of R, we need to install add-on packages. Currently, there are more than 14,500 packages available on the main repository CRAN. We will install the ones we need when we are studying the module.

To install a package simply type install.packages("....") into the console and hit ENTER. This will download and install the package to your computer. After installing the package, you need to load the package to use its functionality, which is done with the library() function.

Exercise: Later on, we will use packages called ggplot2 and dplyr. Let’s install and load these packages.

install.packages("ggplot2")
install.packages("dplyr")
library(ggplot2)
library(dplyr)

You can download all of these at once, too:

install.packages(c("dplyr", "ggplot2"), dependencies = TRUE)

Here dependencies= TRUE ensures package dependencies (i.e., what other packages the one you are installing needs to work) are also installed.

Getting Help in R

R comes with a comprehensive set of help files. Every function needs to have an accompanying help file.You can access the help for every function by preceding it with a ?:

?mean

or by using the search field in the help pane. This will open this file:

R Help Documentation