Appendix A: Installation and Setup

This appendix explains how to install everything the book uses, and how to fix the problems that most often get in the way. If you only want to start, the short version is: install R, then RStudio, then run the package installation command in Section 1.5. If you are not ready to install anything yet, every chapter’s exercises also run in your web browser, in the playground.

Installers and websites change their appearance from year to year, so this appendix describes each step in words rather than with screenshots. The choices that matter are the same in every version.

What you need

Table 1.1: The software used in this book
Software What it is Required?
R The programming language and the engine that runs your code Yes
RStudio Desktop The program you work in: editor, console, plots, and help in one window Yes (or Positron)
Positron A newer editor from the makers of RStudio, an alternative to it Optional
Quarto Turns documents with code into reports and books (Chapter 17) Comes with RStudio
R packages Add-ons for specific tasks, installed from within R Yes, as needed
Rtools (Windows) or Xcode command line tools (macOS) Compilers for building packages from source Only if asked

All of it is free. R must be installed first, because RStudio and Positron are programs for R: they need R to run your code. Install R, then the editor.

Installing R

R is downloaded from CRAN, the Comprehensive R Archive Network, at cran.r-project.org. Always download the latest release. This book was built with R 4.4.3; any later version works.

Windows

  1. On the CRAN page, choose Download R for Windows, then base, then the link to download the latest version (a file such as R-4.x.y-win.exe).
  2. Run the downloaded file. If Windows asks whether to allow the app to make changes, choose Yes. If you have no administrator rights on a university computer, the installer offers to install for your user only, which works just as well.
  3. Accept the default settings on every screen. The defaults install R in C:\Program Files\R\ and register it so that RStudio finds it automatically.

macOS

  1. On the CRAN page, choose Download R for macOS.
  2. Choose the right package for your Mac’s processor. Newer Macs (from late 2020) have Apple silicon (M1, M2, and later): use the file marked arm64. Older Macs have Intel processors: use the file marked x86_64. The Apple menu, About This Mac, shows which you have.
  3. Open the downloaded .pkg file and follow the installer, accepting the defaults.

Linux

R is available in the package manager of every major Linux distribution, but the version there is often out of date. CRAN’s Download R for Linux page gives up-to-date instructions for Ubuntu, Debian, Fedora, and others. On Ubuntu, for example, the instructions add CRAN’s repository and then install R with sudo apt install r-base r-base-dev. Follow the page for your distribution, because the exact commands change with each release.

Checking the installation

Open R (from the Start menu on Windows or the Applications folder on macOS) and type:

R.version.string
[1] "R version 4.4.3 (2025-02-28 ucrt)"

If it prints a version number, R is working. You will rarely open R on its own again; from now on you will work in RStudio.

Installing RStudio

  1. Go to posit.co/download/rstudio-desktop. The page detects your operating system; step 1 on the page (installing R) is already done.
  2. Download RStudio Desktop (the free version) and install it like any other program, accepting the defaults. On macOS, drag RStudio into the Applications folder.
  3. Open RStudio. It finds R automatically and shows the R version in the Console pane, in the lower left (Chapter 1 takes you on a tour of the panes).

RStudio includes Quarto, so the reports of Chapter 17 work without installing anything else. For PDF output, Quarto also needs LaTeX; install a small version once by typing quarto install tinytex in RStudio’s Terminal tab.

Positron, an alternative

Positron (positron.posit.co) is a newer editor from Posit, the company behind RStudio, built for both R and Python. Everything in this book works in Positron as well. RStudio is used in the book’s instructions because it is still the most widely used, and most tutorials and university courses assume it.

No installation at all

If you cannot install software, for example on a locked-down computer, two options need only a web browser:

  • The book’s playground runs R in your browser, with exercises for every chapter.
  • Posit Cloud (posit.cloud) offers RStudio in the browser, with a free plan that is limited but enough to follow most chapters.

Installing the book’s packages

Packages are installed once, with install.packages(), and loaded in every session with library() (Chapter 1). Each chapter lists the packages it uses at the start; Table 1.2 collects them all.

Table 1.2: Packages used in each chapter
Chapters Packages
1-3 here, readxl, haven, writexl, dplyr, tidyr, stringr, readr (or the whole tidyverse)
4, 6-7 ggplot2, psych
8 broom, car
9 psych, GPArotation, corrplot, factoextra
10 lme4, lmerTest
11-13 tidymodels, kknn, ranger, kernlab, themis, glmnet, xgboost
14 mclust, dbscan, factoextra, cluster
15 tidymodels (nnet comes with R)
16 tsibble, fable, feasts, urca
17 rmarkdown, knitr, shiny, bslib, renv, usethis
18 ellmer, stringr, yardstick
19 patchwork, broom, lme4

To install everything at once, copy this command into the Console. It downloads a few hundred megabytes and can take ten minutes or more, so it is best done on a good connection:

install.packages(c(
  "tidyverse", "here", "readxl", "haven", "writexl",
  "psych", "GPArotation", "corrplot", "factoextra", "broom", "car",
  "lme4", "lmerTest",
  "tidymodels", "kknn", "ranger", "kernlab", "themis", "glmnet", "xgboost",
  "mclust", "dbscan", "patchwork",
  "tsibble", "fable", "feasts", "urca",
  "rmarkdown", "shiny", "bslib", "renv", "usethis",
  "ellmer"
))

The tidyverse package installs dplyr, tidyr, stringr, readr, ggplot2, and several others in one go. The first time you install packages, R may ask you to choose a CRAN mirror (choose 0-Cloud, which is fast everywhere) and whether to use a personal library (answer Yes).

The student wellbeing data: the data2thesis package

Elaf’s data is in the data2thesis package, which is installed from this book’s website rather than from CRAN:

install.packages("https://polla-fattah.github.io/data2thesis_r/downloads/data2thesis_1.1.0.tar.gz",
                 repos = NULL, type = "source")

The package contains only data, so it installs on every system without compiling anything. Check that it works:

library(data2thesis)
nrow(students)
[1] 600

The same data is also available as ordinary files (CSV, Excel, and SPSS) on the book’s website, for readers who prefer to import files, as Chapter 2 does.

The versions used in this book

The results in this book were produced with these versions. Newer versions usually give the same results, but if a number in your output differs slightly from the book, a different package version is a likely reason.

Software Version
R 4.4.3
dplyr 1.2.1
tidyr 1.3.2
ggplot2 4.0.3
lme4 1.1.37
tidymodels 1.5.0
fable 0.5.0
ellmer 0.4.0
data2thesis 1.1.0

Common problems and solutions

“There is no package called …”
The package is not installed, or the name is misspelled (names are case-sensitive: GPArotation, not gparotation). Install it with install.packages("name"), then load it with library(name).
“Package … is not available for this version of R”
Either the name is misspelled, or your R is too old for the current version of the package. Update R (see below). A few packages are not on CRAN at all; their documentation says how to install them.
“Do you want to install from sources the package which needs compilation?”
A newer version exists as source code than as a ready-made (binary) package. Answer No to install the slightly older binary version, which is fine for this book. Answering Yes needs Rtools on Windows (from CRAN’s Download R for Windows page, choosing the Rtools version that matches your R) or the Xcode command line tools on macOS (run xcode-select --install in the macOS Terminal).
“Lib … is not writable” or permission errors
R cannot write to the system’s package folder, which is common on university computers. When R offers to create a personal library, answer Yes.
Problems with folder names on Windows
If your Windows user name or project path contains non-English characters, or your files are in a synchronised folder such as OneDrive, installation and file reading can fail in confusing ways. Keeping R projects in a simple local folder, such as C:\research\, avoids most of these problems.
Downloads fail at the university
Some networks block or slow down downloads. Try the 0-Cloud mirror (chooseCRANmirror()), another network, or ask your IT service whether a proxy must be set.
RStudio cannot find R
Install R before RStudio, or reinstall R with the default settings. On Windows, Tools > Global Options > General lets you choose the R version RStudio uses.
Everything worked yesterday, and today nothing does
Restart R (Session > Restart R), then run your script from the top. Many problems come from objects left over from earlier work, which a clean start removes (see the settings above).

Keeping R up to date

Update your packages every few months with update.packages(), or the Update button in RStudio’s Packages pane. Update R itself once or twice a year by installing the new version exactly as the first time; your scripts keep working, but packages must be reinstalled for the new version, which the command in Section 1.5 does in one go. Tools such as rig (github.com/r-lib/rig) can install and switch between several R versions, useful if an old project needs an old version. For projects that must keep exactly the same package versions, use renv (Chapter 17).

Two things do not belong in any script. API keys for AI services (Chapter 18) go in your personal .Renviron file, opened with usethis::edit_r_environ(); and passwords never go into code at all.