1.1 Installing R
R is free, open-source, maintained by a large community, available for all major operating systems, including Windows, macOS, and Linux, and receives regular updates. In its most basic form, it runs in a simple command-line interface (CLI) that, while functional, can be less than intuitive. As a result, integrated development environments (IDEs) are popular, providing a graphical interface with helpful features such as code highlighting, auto-completion, built-in help, plotting windows, and file management. Several IDEs are available for R, including Jupyter (popular for notebooks), R Commander, and RStudio, the latter being the most popular and the one we recommend you use. In fact, we wrote this book with RStudio in mind, and wrote the book using it.
The way you install R and RStudio depends on your system. Note that these instructions may change in the future (especially when it comes to dependencies).
1.1.1 Windows
Install R:
- Go to the CRAN download page for Windows.
- Click the link to download the latest version of R.
- Run the downloaded
.exeinstaller file. - Accept the default installation settings regarding components and the destination folder, as this makes it easier for other programs (like RStudio) to find R. Modern R installers for Windows are 64-bit versions (as most modern Windows systems are 64-bit). If you want a 32-bit version, R Version 4.2.0 is the last that supported it, which you can download from the page containing all the previous releases.
Install RStudio:
- Go to the RStudio Desktop download page.
- Scroll down to the Installers for Supported Platforms section.
- Download the version for Windows.
- Run the downloaded
.exeinstaller file. Again, it is best to accept the default installation settings. RStudio should automatically detect your R installation.
A Note on File Paths in Windows: Unlike Windows, which uses backslashes (\) in file paths (e.g., C:\Users\Documents\data.csv), R (like most Unix systems) uses forward slashes (/) (e.g., C:/Users/Documents/data.csv). Remember to use forward slashes when specifying file paths within R code, regardless of how they appear in your Windows file explorer.
1.1.2 Linux
Install R:
In most cases, R is already part of your Linux distribution. You can check this by opening a terminal and typing R, which will launch it in the terminal if installed. If R is not part of your system, you can install it using your distribution’s package manager (such as APT or snap). However, the version of R in these default repositories may not be the latest. To get the latest version (recommended for compatibility with the latest packages), it’s often best to add the official CRAN repository to your system’s update sources (see here for detailed instructions on how to do so). Alternatively, if you prefer a GUI, you can use the Synaptic Package Manager and look for the r-base-dev and r-base packages, select them, and install them.
Install RStudio:
- Go to the RStudio Desktop download page.
- Scroll down to the Installers for Supported Platforms section.
- Download the installer package appropriate for your distribution (e.g.,
.debfor Debian/Ubuntu,.rpmfor Fedora/CentOS). - Install the downloaded file using your distribution’s package installer (e.g., double-clicking the
.debfile or usingsudo install rstudio-*-amd64.debin the terminal, followed bysudo apt --fix-broken installif there are dependency issues).
Once installed, you should find RStudio in your application menu or by typing rstudio in the terminal.
1.1.3 macOS
Install R:
- Go to the CRAN download page for macOS.
- Download the latest
R-x.y.z.pkgfile. - Run the downloaded
.pkginstaller file. Follow the prompts, and accept the default installation location. - Go to the CRAN macOS tools page.
- Download and install the recommended Clang compiler tools.
- Download and install the recommended GNU Fortran compiler tools.
- You may also need to install the Xcode Command Line Tools. Open the Terminal application (found in
/Applications/Utilities/) and runxcode-select --install. Install all of these after installing R but before attempting to install packages that might require compilation (especially those from GitHub).
Install RStudio:
- Go to the RStudio Desktop download page.
- Scroll down to the Installers for Supported Platforms section.
- Download the recommended version for macOS (
.dmgfile). - Open the downloaded
.dmgfile and drag the RStudio application icon into your Applications folder. - You can then launch RStudio from your Applications folder or Launchpad.
1.1.4 Cloud
An alternative to installing R and RStudio on your computer is a cloud-based service like Posit Cloud (formerly RStudio Cloud). This service provides access to an RStudio environment directly through your web browser.
- Access Posit Cloud: Go to https://posit.cloud/.
- Sign Up/Log In: Click the Sign Up or Login button. You can sign up for a free tier account using your Google or GitHub credentials or an email address.
- Create a Project: Once logged in, you’ll typically start by creating a New Project, which allocates a virtual machine instance running R and RStudio for you.
- Working in the Cloud: The interface is essentially the same as the desktop version of RStudio. You can write code, install packages, and manage files within your project.
Posit Cloud can be a good option for getting started quickly, working on projects that don’t require extensive computing resources, or collaborating with others. However, a local installation is a better choice if you have adequate hardware, especially for more intensive tasks or long-term projects.