R.version.string[1] "R version 4.4.3 (2025-02-28 ucrt)"
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.
| 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.
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.
R-4.x.y-win.exe).C:\Program Files\R\ and register it so that RStudio finds it automatically..pkg file and follow the installer, accepting the defaults.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.
Open R (from the Start menu on Windows or the Applications folder on macOS) and type:
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.
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 (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.
If you cannot install software, for example on a locked-down computer, two options need only a web browser:
A few settings make RStudio work the way this book recommends. Open Tools > Global Options:
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.
| 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).
Elaf’s data is in the data2thesis package, which is installed from this book’s website rather than from CRAN:
The package contains only data, so it installs on every system without compiling anything. Check that it works:
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 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 |
GPArotation, not gparotation). Install it with install.packages("name"), then load it with library(name).
xcode-select --install in the macOS Terminal).
C:\research\, avoids most of these problems.
chooseCRANmirror()), another network, or ask your IT service whether a proxy must be set.
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.