Install and Setup R
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.
I have also prepared this video to guide you through the below installation process.
1 Installation of R
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
Go to R’s official website in this link
Click
CRAN
under Download section
- 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.
- Click into
download R for Windows
for installation files for Windows computers
- 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.
- Click into this link. Download and instsall Quarto CLI plugin.
1.2 For Mac computers
Go to R’s official website in this link
Click CRAN under download section
- 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.
- Click into
download R for macOS
for the download file
- 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”, whereX.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”, whereX.X.X
is the version number. - refer to this link if you don’t know how to check Intel or Apple CPU
- if you use Intel based CPU, download the
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.- Open
terminal
app on your mac (the icon is in the screenshot)
- Type the following code
xcode-select --install
into terminal and hitenter
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.
- Open
- 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.
- Go to RStudio’s website here
- Scroll down to the download list, and select the right version that suits your computer (Mac/Windows)
- After downloading the installation file, follow the instructions to finish the installation. Recommended to use the default settings during installation.
- 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:
- Launch RStudio
- type,
install.packages('tinytex')
, into your R console, and hit enter
- type,
tinytex::install_tinytex()
, into R console, and hit enter.
- 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 hitenter
, check if you see a2
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 clickCreate
.
- Step 3: Click the
Render
button. You may be asked to save theqmd
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.
5 Recommended Setting for RStudio
After you have everything installed, it’s highly recommended to set up RStudio based on my next tutorial here.
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:
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.
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
andCommandline 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).