Install and Setup R

Author
Affiliation

Wei Miao

UCL School of Management

Published

September 17, 2024

Uninstall Old R and RStudio

If you have used R or RStudio before, please uninstall both R and RStudio and follow the guide below to install the latest versions. Otherwise, Quarto may not work properly on older versions.

If you switch to a new laptop later on, please come back to this tutorial and reinstall R and RStudio following the same procedures.

Video Guide

I have also prepared this video to guide you through the below installation process.

1 Installation of R

Important

Note that the latest version of R is 4.4.1 as of September 2024, which is newer than the version in the guide below, thus you may notice slight differences in the version numbers in the screenshots. However, the installation steps are the same.

1.1 For Windows computers

  1. Go to R’s official website in this link

  2. Click CRAN under Download section

  1. These are different mirrors for R download. Basically they store the same installation files but on different servers in different places. Simply click into any mirror.

  1. Click into download R for Windows for installation files for Windows computers

  1. Download and install (1) base and (2) Rtools. It’s recommended to use the default options during the installations.
    • Notes: The former is the R program, and the latter is the tool to compile R packages.
    • It’s highly recommended to change your system language to English before proceeding, or there could be weird bugs later on.

  1. Click into this link. Download and instsall Quarto CLI plugin.

1.2 For Mac computers

  1. Go to R’s official website in this link

  2. Click CRAN under download section

  1. These are different mirrors for R download. Basically they store the same installation files but on different servers in different places. Simply click into any mirror.

  1. Click into download R for macOS for the download file

  1. Download the correct pkg file to install R
    • if you use Intel based CPU, download the R-4.X.X-x86_64.pkg under “For older Intel Macs”, where X.X.X is the version number.
    • if you use Apple’s silicon chip such as M1, M1 pro, or M2, download the R-4.X.X-arm64.pkg under “For Apple Silicon Macs”, where X.X.X is the version number.
    • refer to this link if you don’t know how to check Intel or Apple CPU

  1. Install Command Line Tools following the steps below. This is essential for R to be able to compile packages so do not skip this step.

    1. Open terminal app on your mac (the icon is in the screenshot)

    2. Type the following code xcode-select --install into terminal and hit enter to run the code. Admin passwords may be required to proceed. Note that when you type the password, you won’t see any characters on the screen, but the passwords have been entered.

    The code may run for a few minutes. This step is to install MacOS tools that can help compile R packages. - If your terminal says “xcode-select: error: command line tools are already installed, use”Software Update” to install updates”. It means the needed tool is already installed on your computer. Then there is nothing needed in this step.

  1. Click into this link. Download and install Quarto CLI plugin for Mac OS.

2 Install RStudio

RStudio is where we will write our R codes. Please install RStudio following the steps below.

  1. Go to RStudio’s website here
  2. Scroll down to the download list, and select the right version that suits your computer (Mac/Windows)
  3. After downloading the installation file, follow the instructions to finish the installation. Recommended to use the default settings during installation.
  4. After installation, you can find RStudio in the Application folder on Mac or Windows menu on Windows computers.

3 Install LaTex

To render pdf documents using Quarto, you need to have Latex installed on your computer.

There is a package in R called tinytex which provides a light installation of latex. You can install tinytex as follows:

  1. Launch RStudio
  2. type, install.packages('tinytex') , into your R console, and hit enter

  1. type, tinytex::install_tinytex() , into R console, and hit enter.

  1. Finish the installation based on the prompts on the screen.

4 Check success of installation

4.1 Check R and RStudio are properly installed

Please follow the following steps to make sure you have successfully installed R and RStudio.

  • Step 1: Launch RStudio from your computer. Check if you see the following screen without any error messages

  • Step 2: As shown in the picture, type 1+1 behind the > and hit enter, check if you see a 2 output

If there are no error messages and you see exactly the same output, then congrats, you have successfully installed R and RStudio!

4.2 Check Quarto is properly installed

  • Step 1: Click the green plus sign circled below, and select Quarto Document

  • Step 2: Select word and click Create.

  • Step 3: Click the Render button. You may be asked to save the qmd file to a location. Save it to your download folder. R will then render the qmd document and generate a docx file, named “untitled.docx” in the same location.

  • Step 4: The docx file should look like below. If you can generate the docx file without issues, Quarto has successfully run on your computer!

Note that if you see a banner prompting that rmarkdown is not installed, you need click the Install button to install it. In the future, if you ever see a similar banner that prompts “Package XXX required but is not installed”, you should click the Install button to install the missing package, because this package is needed for the current Quarto file to work properly.

6 Installation Q&As

6.1 Why do we need to install Rtools and Commandline tools?

Many R packages are written in R. Since R is an interpreted language, source code written in R doesn’t have to be translated into system-specific machine language before running. However, some R packages have significant portions written in compiled languages, such as C/C++ or Fortran. These languages need accessory software tools to translate (“compile”) their source code into machine language that can run on a particular system.

Package developers have two choices when distributing code for compiled languages:

  1. They can prepare compiled, realdy-to-use “binaries” matched against common systems, so that people can simply download the binaries and directly use their packages code without having to know how to compile it.

  2. They can distribute source code (i.e., the raw C++ codes) only, and expect the user to have the right compiler software to build system-specific runnable code themselves. Rtools and Commandline tools are the compliers that do the job, therefore needed as an additional installation step.

On UNIX/Linux, only source code is distributed and all packages are compiled from source during installation (for packages written entirely in R, this is trivial!). For Windows and Mac, CRAN makes pre-compiled binaries available. On Windows, install.packages() will only install precompiled binaries, unless explicitly forced to install from source (you can read a lot more about this in the R Installation and Administration guide).