Installing R and RStudio

Step 1: Install R

R is the underlying statistical computing environment and must be installed before RStudio.

  1. Navigate to the official CRAN repository: https://cran.r-project.org
  2. Select the installer appropriate for your operating system:
    • Windows: Click Download R for Windows → base → download and execute the installer
    • macOS: Click Download R for macOS → select the build matching your architecture (Apple Silicon users should select the arm64 build)
    • Linux: Follow the distribution-specific installation instructions provided on the page
  3. Run the installer using the default configuration settings.

Step 2: Install RStudio Desktop

RStudio is an integrated development environment (IDE) for R. It requires a prior R installation to function.

  1. Navigate to: https://posit.co/download/rstudio-desktop/
  2. Download the free RStudio Desktop release for your operating system
  3. Execute the installer and follow the on-screen instructions
  4. Upon launching RStudio, the application should automatically detect the installed R environment

Important: R and RStudio are distinct pieces of software. RStudio does not bundle R; it serves solely as an interface to an existing R installation.


Authoring Documents with R Markdown and Generating PDF Output

R Markdown is a document format that integrates prose, code, and computational output within a single reproducible file. The following steps outline the process of creating an R Markdown document and rendering it as a PDF.

Step 1: Install Required Packages

Execute the following commands in the RStudio Console:

install.packages("rmarkdown")
install.packages("knitr")
install.packages(c("tidyverse", "patchwork", "uuid")) # course packages (tidyverse includes ggplot2)

Step 2: Install a LaTeX Distribution

PDF rendering requires a LaTeX engine. The tinytex package provides a lightweight, self-contained LaTeX distribution suitable for most use cases:

install.packages("tinytex")
tinytex::install_tinytex()

Restart RStudio after installation to ensure the LaTeX binaries are correctly registered.

Step 3: Create a New R Markdown File

For graded work, do not start from RStudio’s own sample file. Instead:

  1. From the RStudio menu, select: File → New File → R Markdown…, set the output format to PDF, and click OK
  2. Select everything in the new file and delete it — RStudio’s sample text (## R Markdown, summary(cars), plot(pressure)) must not be left in a submission
  3. Paste in the official template from the Problem sets page and save the file as exN-YourStudentID.Rmd

Step 4: Configure the YAML Header

Every R Markdown document begins with a YAML front matter block delimited by ---. This block controls document metadata and output settings:

---
title: "Ex. X"
author: "Student ID"
date: "`r Sys.Date()`"
output: pdf_document
---

Important: For graded submissions, the author field must contain your student ID only (e.g., author: "D10627008") — do not include your name. Always start from the official template on the Problem sets page, which also includes the required UUID code chunk. Submissions that deviate from the template receive a grade of zero.

Step 5: Write Document Content

R Markdown uses standard Markdown syntax for formatting prose, supplemented by fenced code chunks for embedding R code:

Syntax Rendered Output
# Heading 1 First-level heading
## Heading 2 Second-level heading
**bold** bold
*italic* italic
Fenced code block with {r} Executed R code with output

Example code chunk:

summary(mtcars)
      mpg             cyl             disp             hp       
 Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
 1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
 Median :19.20   Median :6.000   Median :196.3   Median :123.0  
 Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
 3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
 Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
      drat             wt             qsec             vs        
 Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
 1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
 Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
 Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
 3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
 Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
       am              gear            carb      
 Min.   :0.0000   Min.   :3.000   Min.   :1.000  
 1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
 Median :0.0000   Median :4.000   Median :2.000  
 Mean   :0.4062   Mean   :3.688   Mean   :2.812  
 3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
 Max.   :1.0000   Max.   :5.000   Max.   :8.000  

Step 6: Render to PDF

Click the Knit button in the RStudio toolbar, or use the keyboard shortcut Ctrl+Shift+K (Windows/Linux) or Cmd+Shift+K (macOS), and select Knit to PDF.

Note on CJK character rendering: You can avoid this entirely by answering in English, which is what the course expects. If you do need Chinese, Japanese, or Korean characters and they appear as missing glyphs, switch the LaTeX engine and name a font your own machine actually has:

output:
  pdf_document:
    latex_engine: xelatex
header-includes:
  - \usepackage{fontspec}
  - \setmainfont{PingFang TC}

Use PingFang TC on macOS and Microsoft JhengHei on Windows. Do not use Noto Sans CJK TC unless you have installed it yourself — it ships with neither operating system, and naming a font you do not have stops the PDF from building at all.

Changing output: and adding header-includes: in this way is permitted and will not count as deviating from the template.