Prerequisites

In this book, we’ll be using R and the RStudio IDE. You’ll need to download each of these separately from the Internet to use on your local workstation.

What is R?

R is the name of the programming language itself, based off of S from Bell Labs, which users access through a command-line interpreter (>). Here is a bit of R code:

5 * 2
[1] 10

If you want to learn more about the history of R as a programming language, we recommend reading the original paper by Ihaka. One of our favorite quotes from

“We have named our language R—in part to acknowledge the influence of S and in part to celebrate our own efforts. Despite what has been a nearly all-consuming effort, we have managed to remain on the best of terms and retain our interest in computers and computing”

Ihaka and Gentleman ()

What is the RStudio IDE?

The RStudio IDE is a powerful and convenient user interface that allows you to access the R programming language along with a lot of other bells and whistles that enhance functionality (and sanity). The acronym IDE stands for “integrated development environment” which is, to put it technically, where you do stuff. It is where you will work with R.

You can use R with its built-in GUI (“graphical user interface”), but in this book, we’ll use and show you how to use the RStudio IDE instead.

Installing R and the RStudio IDE

R is currently developed by the R Development Core Team and, at the time of this writing, the latest R release is 4.3. R has a package system that allows users to write and distribute their own packages of functions. The number of user-created packages at this point is well over 10,000 and that figure is part of the reason why R has so much functionality.

There are two pieces of software you’ll need to download. One is R itself and the other is the RStudio IDE. Luckily, you can use both of these in Windows, macOS, and in various distributions of Linux. Depending on your OS there are preferred ways to install both. While we do need both pieces of software installed, the order of installation is not important.

macOS Installation

Here are the steps for obtaining and installing the R programming language.

  1. With your preferred web browser, go to https://cloud.r-project.org/. This is the main site for R and CRAN.
  2. Follow the CRAN link to the left.
  3. Follow the Downloads link to go to the download page then get the installer for the Macintosh computing platform. It downloads as a DMG file.
  4. Open the installer file and follow the installation instructions to completion.

You then have R. If you’d like to be fully certain of that, open the Terminal application and type R and the R command-line console should appear.

Check-in

Launch R. You should see one console with a command line interpreter (>). Close R.

To install the RStudio IDE on macOS:

  1. Go to this Posit webpage https://posit.co/download/rstudio-desktop/ and click the DOWNLOAD RSTUDIO DESKTOP… button; the macOS installation file (DMG) will be downloaded
  2. Open the DMG file, and drag the app to the Applications directory.

Keeping the RStudio app in the macOS Dock is a good idea for easy access to the IDE.

Check-in

Launch RStudio. You should get a window similar to the screenshot you see here, but yours will be empty. Look at the bottom left pane: this is the same console window you saw when you opened R in the previous session.

  • Place your cursor where you see > and type x <- 2 + 2, hit enter or return, then type x, and hit enter/return again.
  • If [1] 4 prints to the screen, you have successfully installed R and RStudio, and you can move onto installing packages.

Windows Installation

The installation of R on Windows is not very different from that on the Mac. There are some subtle differences though. Let’s walk through the entire process. First, we need to get R, so here are the steps:

  1. Go to https://cloud.r-project.org/. This is the entry point to R-as-a-project and CRAN.
  2. Follow the CRAN link to the left.
  3. Follow the Downloads link to go to the download page then get the installer for the Windows computing platform.
  4. Run the installer program; it may ask a number of potentially confusing questions, however, just accept the defaults. Soon thereafter the installation will finish, and R for Windows will be installed.

R will create a few shortcut icons and a good way to check that the installation had succeeded is to open R through the Start Menu.

Check-in

Launch R. You should see one console with a command line interpreter (>). Close R.

The next step is to get RStudio IDE.

  1. Go to this Posit webpage https://posit.co/download/rstudio-desktop/ and click the DOWNLOAD RSTUDIO DESKTOP… button; the Windows installation package will be downloaded
  2. After opening that file, installation will proceed through a number of dialogue boxes, so, ensure that all are followed until completion of the install process.

After installation, try opening RStudio; the IDE will appear on the screen. I suggest keeping the RStudio icon pinned to the Task Bar so that it’s easily accessible.

Check-in

Launch RStudio. You should get a window similar to the screenshot you see here, but yours will be empty. Look at the bottom left pane: this is the same console window you saw when you opened R in the previous session.

  • Place your cursor where you see > and type x <- 2 + 2, hit enter or return, then type x, and hit enter/return again.
  • If [1] 4 prints to the screen, you have successfully installed R and RStudio, and you can move onto installing packages.

Using the RStudio IDE Environment

The RStudio IDE includes:

  • a console (the standard command line interface: >),
  • a syntax-highlighting editor that supports direct code execution, and
  • tools for plotting, history, debugging and workspace management.

This is an integrated development environment (IDE) that makes it easy to do work with R (we’ll simply refer to this IDE as RStudio throughout this book). RStudio and R itself both need to be installed separately (but the order of installation does not matter).

RStudio has a customizable, four-pane workspace. It has a collection of useful features that help with R coding such as syntax highlighting, autocomplete, file and project management, and much more.

The RStudio IDE environment with an Quarto file in focus in top-left (the source pane) and the R console occupying the bottom-left.

The diagram shown in above provides a reference to a cross section of the buttons available in the initial view. The most important takeaways from this first look at RStudio are:

  1. The source pane (top-left) is where you would work on R scripts and Quarto documents.
  2. The console pane (bottom-left) is where you would access the R console and the system’s terminal.
  3. The toolbar (above the source pane) provides useful tools for managing and navigating through documents and projects.
  4. Two panes (to the right) provide access to files, workspace objects, R packages, plots, and much more.
  5. The Project dropdown (upper-right corner) provides access to existing projects, project options, and anything else related to managing projects.

It’s important to note that not all of the features of RStudio are likely to be touched by the average user. Early on, we will only need a very small subset of the functionality that’s provided.

Indeed, a little can go a long way, so, while on the topic of the small things, it’s never too early to learn about a few useful keyboard shortcuts. These are the ones that you’ll use again and again.

  1. Ctrl + Enter — runs the current line or a selection of code from the editor in the console. This is extremely useful as you select even nested functions and run them in the console without copying anything anywhere.
  2. Tab — provides an auto-complete function. Start typing in the editor or even in the console and try hitting the tab key: RStudio will suggest functions, variables, or file names. Up and down with the arrow keys and another Tab (or Enter) will accept the choice.
  3. Control + . — brings up a search bar where you can search for files or functions. Up and down arrows and return allow you to navigate quickly to the file (at the right location) in a new tab.
  4. Command + Option + I (Control + Alt + I on Windows) — creates an empty R code chunk in a Quarto document. As this is one of the key things you’ll be doing, this shortcut does save a lot of time.

With time, it becomes easier to RStudio with less reliance on a pointing device. If you want to get on that road try the RStudio shortcut Shift + Option + K (Shift + Alt + K on Windows), it brings up the (Keyboard Shortcuts Help overlay (which has a lot of keyboard shortcuts).

Ihaka, Ross, and Robert Gentleman. 1996. “R: A Language for Data Analysis and Graphics.” Journal of Computational and Graphical Statistics 5 (3): 299–314.