Introduction to R and RStudio

Chapter 3 Installation and usage

Installation

In this tutorial, you will use the package Swirl to interactively learn programming in R via the console (see section 1.1). To follow the tutorial:

  1. Download the file Intro_to_R.swc;
  2. Sart RStudio and install the package “swirl”, either using the packages-tab in the bottom-right panel, or by executing the command install.packages("swirl") (see also chapter 2);
  3. Load the installed library by executing the command library(swirl);
  4. Execute the command install_course(), and select (in the popup-screen) the downloaded file “Intro_to_R.swc”. Swirl will now install the exercises for this tutorial.

Usage

Once you have installed the swirl package as well as the tutorial lessons via the steps above, then start the tutorial by executing the command swirl(). Swirl will ask you to enter your name: this is to monitor your progress: should you close RStudio and resume the tutorial later, you will continue where you left off if you use the same name as entered before (note that this is not case-sensitive). Then, select the course “Intro to R”, and go through these lessons:

  1. Basic Building Blocks
  2. Sequences of Numbers
  3. Vectors
  4. Missing Values
  5. Subsetting Vectors
  6. Matrices and Data Frames
  7. Logic
  8. Looking at Data
  9. Functions

You can exit swirl and return to the R prompt (>) at any time by pressing the Esc key. If you are already at the prompt, type bye() to exit and save your progress. When you exit properly, you’ll see a short message letting you know you’ve done so.

When you are at the R prompt (>):

  • Typing skip() allows you to skip the current question.
  • Typing play() lets you experiment with R on your own; swirl will ignore what you do…
  • UNTIL you type nxt() which will regain swirl’s attention.
  • Typing bye() causes swirl to exit. Your progress will be saved.
  • Typing main() returns you to swirl’s main menu.
  • Typing info() displays these options again.

The lessons included in this tutorial cover only the basic use of the R programming environment with ‘base-R’ (and thus not the functionality that e.g. the packages tidyverse and ggplot2 provide). See Chapter 13 for some tips to optimize management of files when working in a project, and Chapter 17 for some links to other websites that provide good introductions to R.

The following chapters in this section contain the same contents as what is shown interactively to you in the console in the swirl lessons. Thus: you do not have to keep track of your code in a script, nor do you have to bother with copying the information given to your written or digital notebook! Rather, you can use this document as a copy of it, or, if you prefer a version stored locally, you can download a pdf of this document using the download button in the top menu bar.