2  How to Use R on a Computer

CautionThis chapter is under construction

R will install and run straightforwardly on Windows and Macintosh operating systems as well as Linux;1 however, prior to attempting any install it is important to make a simple distinction first. R is a programming language, which means it is nothing more than a language you use to communicate instructions to a computer. To communicate those instructions, some type of interface is required. This is a basic reality that applies to any language. It is quite difficult to communicate with someone if they have no mouth, eyes, or ears to send and receive communications with. Computers are no different in this respect. Simply understanding the language is not sufficient. For this reason, most operating systems come equipped with a basic way of interfacing with the user via a “terminal window” (see Figure 2.1). This is an application that allows you to type instructions (a.k.a. “commands”) to your computer. Put more simply, when you see people “hacking” in movies, a terminal window is usually what they typing into. On computers using the Windows 11 operating system, this is referred to as the Windows Terminal, on Macintosh and Linux computers this is simple the Terminal application.2

MS-DOS Prompt application in Windows 95
Figure 2.1: The MS-DOS Prompt application in Windows 95 is an example of a terminal window. This allowed users to access the DOS command line which was the foundation of the Windows operating system back in 1995.

For historical reasons, we refer to these applications “terminals” because they are mimicking specific technology of the past. In a bygone era, “terminals” were peices of computer hardware, consisting mainly of a keyboard paired with a monitor or printer, that people could use to send data to and receive data from a computer. Prior to this, information would have to be entered via punch-card (Norsk Teknisk Museum 2014).

IBM 2260 Terminal and mainframe.
Figure 2.2: IBM 2260 terminal used to communicate with an IBM mainframe computer (seen in the background) in the 1960s and 1970s.

Relying on your operating system’s terminal application as a primary interface is often a cumbersome and inefficient experience, and definitely not a recommended course of action - though, for what it is worth, Linux users seem to delight in this sort of thing.3 The preferred means of communicating R to your computer is via the use of what, in programming lingo, is commonly termed an environment or, more garrulously, an integrated development environment (IDE). This is simply a software application providing the user with a more polished visual workspace and feature set to make programming a smoother experience. IDEs exist for almost every language and for nearly any use case you can imagine. Among R users, RStudio and Positron are two of the most popular options. That said, IDEs vary widely in style and capability, and using R does not require the use of these IDEs specifically, or any IDE for that matter. Still, installing one alongside the language itself is probably a good idea. However, before doing that, it is important to install the R language itself first.

2.1 Installing the R Programming Language

The standard installation of R will come with an associated environment for the user - provided they are working with either a Windows or Macintosh operating system. An example of the stock environment R ships with on Windows can be seen in Figure 2.3. Admittedly, as IDEs go, this will get the job done, but offers a pretty lackluster experiance. But don’t worry, that’s what RStudio and Positron are for.

Base R Environment
Figure 2.3: Base R environment for Windows, featuring the default console interface for running R code and viewing outputs (left) and the default scripting window (right) for writing, running, and saving R code.

To install R - both the language and environment simultaneously - simply go to the R Project for Statistical Computing website:

https://www.r-project.org/.
Caution

R’s website makes no apologies for its spartan ’90s aesthetic (and we should all be so principled frankly). Additionally, you’ll encounter lots of technical jargon throughout the site’s various pages, and as a newcomer to R and programming, some (maybe even a lot) of it will feel opaque. That’s not a problem and you shouldn’t feel bad about it. Skim through it, absorb what clicks, maybe look up one or two things, and let the rest wash over you. Doing this builds familiarity with the landscape and overtime, as your skillset and vocabularly grows, the jargon will become more transparent.

Located on the front-page of this website should be a link labelled CRAN. This stands for “Comprehensive R Archive Network” and is a set of servers around the world that distribute R alongside software packages associated with R. The servers are “mirrored,” meaning they all provide the same content. So there is no need to worry about one server providing incomplete, out-of-date, or unofficial versions of R. Technically speaking, the server closest to your home location is the one you should opt to download from; however, the topmost link labeled “0-Cloud” will be sufficient for most users. The install file is only about 80 megabytes large, so unless you live in the remotest areas of Earth, download speed, and thus choice of server, is probably not a concern.

Once you have chosen a suitable server, you will need select your operating system from the list the site provides (Chromebooks and Mobile devices are out of luck - though cloud-based alternatives do exist that can run R through a web-browser).

2.1.1 Windows Installation

Click the “Download for Windows” link to access the R download page (see Figure 2.4). Select the “base” option (labeled “install R for the first time”), which will direct you to the installer for the latest version of R. You’ll see a download link for the current release—for example, Download R-4.5.3 for Windows - which is the newest version available at the time of writing.

Windows download options.
Figure 2.4: Windows download options

Once you have downloaded the (.exe) installation file (check your Downloads folder if you are unsure where on your computer it is located), open it and follow the setup wizard. Keep the default settings unless you have a specific reason to change them. Once installation is complete, you can launch R from the Start menu or a desktop shortcut (if you chose to create one during setup).

2.1.2 macOS Installation

Click the “Download R for macOS” link to access the R download page and select the option relevant to your computer (see Figure 2.5). At the time of writing this, Macintosh computers have recently begun being manufactured using their own in-house built processors (i.e., dubbed “Apple silicon”); however, many older Macintosh computers (pre-2023) still contain Intel-made processors. The install file you select will need to be determined by which type of processor your computer is using. Macintosh users can determine this by selecting About This Mac via the small little apple logo in the top left corner of their desktop screen. Machines using Apple silicon, will display a row called “Chip” and state something akin to “Apple M1”. If you see this, you should opt to choose the install file that says “arm64”. Machines using Intel processors will display a row reading “Processor” followed by the make and model of the processor. In this latter case, you should choose the install file that says “x86_64.”

macOS download options.
Figure 2.5: macOS download options

Once you have downloaded the (.pkg) installation file (check your Downloads folder if you are unsure where on your computer it is located), open it and follow the setup wizard. Keep the default settings unless you have a specific reason to change them. Once installation is complete, you can delete the install file and launch the base-R environment from the “launchpad” or applications folder.

2.1.3 Linux Installation

Experianced users of Linux are not likely to need any instructions on how to install R on to their system; however, inexperianced users may need a helping hand as the install process is done entirely via the terminal. There are no install files you can download from a website and launch with a couple clicks of the mouse.

When it comes to Linux distributions (a.k.a. Linux operating systems), there are of course too many to even name. This makes it unrealisic to cover the install process for all of them. Consequently, this section will focus on Ubuntu and its derivatives such as Linux Mint, Kubuntu, Budgie, etc.

Important

Unlike Windows and macOS, installing R on Linux does not include a graphical coding environment (see Figure 2.3). Consequently, Linux users must use the terminal to run R until they install an IDE such as RStudio or Positron.

2.1.3.1 Ubuntu and its Flavours

Many Linux distros, like Ubuntu, have their own officially currated repositories that you can download R from. However, the version of R these store often lags quite a bit behind the offical files supplied by the R Project’s CRAN repository. The reason being, Ubuntu’s package maintainers need time to test software, ensure it integrates well with other software on Ubuntu, and verify it works across different Ubuntu versions. This deliberate process keeps things stable but, unfortunately, means you’re not getting the latest version of R immediately.

By contrast, if you need the latest R version or specific recent features/bug fixes, you can add the R Project’s own repository to Ubuntu’s package manager configuration apt (Advanced Package Tool).

2.1.3.1.1 Installing from Ubuntu’s Repository

If using Ubuntu’s offical repository appeals to you, inside your terminal you can just run …

sudo apt update
sudo apt install r-base
2.1.3.1.2 Installing from the R Project’s Repository (CRAN)

If, on the other hand, you would like the most recent releases of R and R packages (this is the reccommended approach), you will first need to download the GPG public key file from the R project’s official server. This is used to verify that R packages from the the CRAN’s Ubuntu repository are legitimate and haven’t been tampered with.

In your terminal run the the following to first refresh your package lists:

sudo apt update

This tells apt “go fetch the latest list of available packages and their versions” from all the configured repositories on your computer.

Next we are going to install two helper programs, software-properties-common and dirmngr. The former provides tools for managing software repositories. The latter is background service that manages keys and certificates. It’s used to verify GPG signatures on packages and keys.

sudo apt install --no-install-recommends software-properties-common dirmngr

Then, in order to install the GPG key file, we run …

 wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc

Next we add the R Project’s repository to your system’s package sources so apt knows where to find R packages. To do this, you need to determine the codename of Ubuntu you are running. Ubuntu names each new long term support (LTS) release of its operating system with a codename (focal = 20.04, jammy = 22.04, noble = 24.04, etc.) To determine the code name just run lsb_release -cs in the terminal.

In the code below you need to replace noble with the codename of your version of Ubuntu.

echo "deb https://cloud.r-project.org/bin/linux/ubuntu noble-cran40/" \
 | sudo tee /etc/apt/sources.list.d/cran-r.list

And now we can finally install R:

sudo apt update
sudo apt install r-base

And, for good measure, install build tools for installing R packages from source This is needed for many CRAN package installs.

sudo apt install r-base-dev

Check the version of R you have installed.

R --version

To run R simply type:

R

To quit running R:

q()

2.1.3.2 Linux Mint and other Ubuntu-based Distributions

The installation processs on other Ubuntu-based distributions, such as Linux Mint, Edubuntu, Kubuntu, Lubuntu, Ubuntu Budgie, Ubuntu Cinnamon, Ubuntu MATE, and others, is identical to regular Ubuntu with one exception. Some distributions, such as Linux Mint, have their own distinct codenames. This means that, in the steps above where we replace noble, we will need to make sure that we use the Ubuntu codename that your Ubuntu-based distribution is built from. This can be obtained by running the following lines in the terminal:

. /etc/os-release
echo "$UBUNTU_CODENAME"

Everything else about the install, should be the same as standard Ubuntu.

2.1.3.3 Other Linux Distributions

The offical R Project website contains detailed information for installing on other major Linux distributions. https://cloud.r-project.org/bin/linux/

2.2 Installing an IDE (RStudio and Positron)

As mentioned earlier, the default R environment that ships with a Windows and macOS install of R is quite limited (see Figure 2.3). While perfectly functional, it lacks many quality-of-life features found in modern IDEs. RStudio and Positron, by contrast, are the two most feature-rich IDEs for R, with RStudio having dominated the community for years and Positron emerging as a compelling newer alternative.

As of writing this, RStudio is the established choice. It has extensive documentation, countless tutorials, and broad support across the R community. By contrast, Positron is a much more recent development offering a more flexible and responsive interface built on Visual Studio Code’s foundation.4 Both applications are developed by Posit, a company focused on open-source tools for data science and scientific research. For a detailed feature comparison, see Posit’s comparison page.

If you can’t decide which to choose and want definitive choice made for you, go with Positron. It integrates modern development practices more seamlessly than RStudio, which was designed in 2011 when the computing landscape looked rather different. Both are excellent, but Positron won’t make you feel like you’re using legacy software.

Installing a IDE is not strictly necessary to work through this book’s content; however, the wealth of features and customization they offer does makes it a worthwhile endeavor and is recommended for anyone reading this text.

2.2.1 Installing RStudio

To install RStudio simply visit:

https://posit.co/download/rstudio-desktop/


Installing R studio is as simple as selecting the installation relevant to your operating system (“OS” on the website’s table), downloading the installation file, and launching it with a double-click of the mouse.

2.2.1.1 Note for Macintosh Users

For Macintosh users, when you download and launch the install file for R studio there will probably be no installation wizard; rather, you will likely be prompted to drag an RStudio icon into your applications folder. Once that is done, R studio is installed.

2.2.1.2 Notes for Linux Users of Ubuntu, Mint, and other Ubuntu-based Distros

Ubuntu users, Mint users, and users of other Ubuntu-based distributions (e.g., Kubuntu, Edubuntu, Ubuntu Budgie, etc.) will need to select the Ubuntu install file that corresponds to the major version of Ubuntu they are running. In most cases this is will be Ubuntu 24.

It is also worth noting that the install file is a .deb file. Once downloaded to your computer, these can usually be run without issue using a simple double click of the mouse (similar to the .exe files present on Windows systems). However, the more reliable method is to use the terminal to install this file. Should you want to take this approach you can follow the three steps below.

  1. Open a terminal by pressing Ctrl + Alt + T or searching for “Terminal” in your application menu.

  2. Assuming the file you downloaded is in your “Downloads” folder, navigate their in your terminal by typing:

cd ~/Downloads

The ~ symbol represents your home directory, so this takes you to /home/yourusername/Downloads.

  1. Install the file by typing the code below making sure to replace filename.deb with the actual name of the file you downloaded.
sudo apt install ./filename.deb

The ./ before the filename tells apt to look in the current directory.




CautionComing Soon:
  • Installation Instructions for Positron.
  • Cloud-based IDEs (no installation required)



  1. Admittedly, the process is probably a little less straightforward on Linux, but still managable.↩︎

  2. It’s worth noting that most popular mobile and desktop operating systems do their absolute best to keep terminal applications out of sight. They they offer too much fine grain control of the computer (a.k.a. freedom). That makes it hard to herd users into the unquestioned patterns of behaviour the platform wants. You’ll hear this justified as protecting the “user experience,” but it rings hollow: 10-year-olds in the early ’90s navigated MS-DOS to launch Duke Nukem, and ’80s kids taught themselves BASIC to play Ultima. The assumption that users can’t handle a command line isn’t about safety, it’s about control.↩︎

  3. I say that as someone who worships at the throne of Tux, the Penguin Emperor.↩︎

  4. Visual Studio Code is a free open-source IDE that has become the standard choice for many developers across multiple programming languages.↩︎