Introduction to R: Lecture 1

Topics: Course Logistics, R, Markdown

Sabrina Nardin, Summer 2025

Agenda

  1. Welcome to the Course!
  2. Intro to R, RStudio, and Workbench
  3. R Markdown

1. Welcome to the Course!

Teaching Team

Role Name Email Office Hours
Instructor Sabrina Nardin nardin@uchicago.edu Tue & Thu, 2:30–3:30 PM (after class)
Teaching Assistant Zach Meyer zmeyer@uchicago.edu Tue 2:30–3:30 PM (after class)


How to Reach Out

  • Ask questions during or after class
  • Attend OHs (Office Hours, no appointment needed)
  • Email us or post on Ed Discussion for course-related concerns
  • Use the course website for slides, class materials, etc.


Course Objectives

The goal of this course is to acquire basic computational skills. Specifically:

  1. Learning R, especially the “tidyverse”
  2. Learning best practices for reproducible research

You won’t become an R expert in a few weeks, BUT you will:

  • learn the basics and gain confidence to explore new techniques beyond this course

  • use R as a tool for analyzing social science data

Course Topics

Main topics of this course:

  • Git/GitHub via RStudio
  • R Markdown
  • Data Visualization
  • Data Cleaning and Transformation
  • Exploratory Data Analysis
  • R Programming: Control Structures, Data Structures, etc.

How We’ll Do This

We start simple and build toward more complex code

# create a variable and print it
x <- "Hello!"
print(x)
[1] "Hello!"


We use a mix of lectures, in-class activities, assignments (take-home and in-class), and we rely on several platforms (e.g., course website, Canvas, Ed Discussion, Workbench, Git/GitHub, and Markdown).

All course materials are free and accessible from our website.


Homework and Evaluation

See “Assessment” from our Syllabus and the Course Schedule:

  • Assessment and Evaluation: There will be in-class quizzes and take-home assignments. Check the syllabus for the grading rubric for details.
  • Arrive on Time: Each class starts right at 1:00 PM with a quick quiz. These are short and meant to help you stay on track. Be here and ready to go by 1:00 PM!
  • Submission Policies: Info on how we handle late or missed assignments is in the syllabus.

Software Setup

In this course we use RStudio Workbench, which is cloud-based version of RStudio that runs in your browser.

Next lecture we will learn the basics of Git and GitHub and complete the Software Setup for the course.

Check our website under “Software Setup” for details.

Activity: Meet & Share Goals

In groups of 3-5 people, please share:

  • Your name, plus something about yourself that you’re comfortable sharing (e.g., a hobby, your favorite food, your high school, your experience with programming, etc.)
  • What you hope to learn or achieve in this course
  • Any questions you have, and strategies you plan to use to stay on track (we’ll go around the room and answer questions)

Done early?

If your group finishes early, please complete this short survey if you haven’t already. It only takes a minute and helps us better understand your background and what you’re hoping to learn in this course.

Course Expectations

  • Attend classes and bring a laptop (no cell phones)

  • Review in-class materials and complete assigned work

  • Ask questions and collaborate

    • 15-minute rule
    • Resources: AI (but see next slide), Google, StackOverflow, your peers (study group!)
    • Office Hours and Ed Discussion

Yes to Collaboration, No to Copying

Researchers collaborate on projects. Developers work in teams to write programs. AI is becoming part of our daily and academic lives.

→ Collaboration is good and encouraged, but in this course you are also expected to:

  • Complete your own work individually
  • Write your code and fully understand it
  • Be ready to explain your code line-by-line to someone else

Collaboration vs. Plagiarism

✅ DO:

  • Discuss and debug with peers
  • Rewrite code found online in your own style
    and cite the source
  • Use AI to help debug or clarify concepts
  • Submit code you wrote
  • Submit code you fully understand

🚫 DON’T:

  • Copy code from your classmaters or others
  • Simply copy and paste chunks of code from
    the internet
  • Ask AI to write code for you
  • Submit code you didn’t write
  • Submit code you can’t explain

Tips from former students…

  • “One of the hardest things for me was remembering where I saw a concept or piece of code.”
    Advice: Stay organized (e.g., customize slides and notes, and create your own system to retrieve info)!
  • “Homework took more time and effort than I expected.”
    Advice: Start early! You don’t need to know everything before you begin: just get started and review the materials as needed. Complete the in-class exercises before tackling a homework.
  • “I’m in week 4, and I feel lost.”
    Advice: Reach out to us! Concepts build over time. The big picture will click IF you stay consistent with your efforts every week.
  • “I found code that worked but wasn’t covered in class, so I wasn’t sure whether to use it.”
    Advice: Stick to class syntax and functions — they’re chosen for a reason. But you can show both approaches by comparing the function(s) covered in class with your own.

2. Intro to R, RStudio, and Workbench

What Are These Tools?

Tool What It Is
R A programming language for data analysis, statistics, and visualization.
RStudio A user-friendly interface (IDE) for using R.
Workbench A cloud-based version of R/RStudio with everything pre-installed. We will use it in this course to minimize setup issues.

Let’s start by exploring our Software

Workbench: https://macss-r.uchicago.edu/

  • Log in
  • Look at panels
  • How to export/import files between Workbench and your machine
  • Create an empty R Script

Getting Started with R

Today we learn how to:

  • Create and use variables
  • Understand variable types (e.g., number, text, logical, etc.)
  • Write comments and organize your code
  • Use scripts (vs. console) for reproducibility
  • Clear your environment
  • Group values using vectors

💻 Live Coding Activity to get Started with R

We learn all of this via a live coding activity:

3. R Markdown

Deep Dive on R Markown

R Markdown is a file format (with extension .Rmd) that combines R code, using code chunks, and text in a single document. It’s very popular because it makes it easy to create reproducible reports, analyses, and presentations.

You might hear about Quarto (with extension .qmd), which is a newer alternative to R Markdown. We won’t cover Quarto in this course, but if you learn R Markdown, you can quickly adjust to Quarto.

Comparing R Scripts, R Markdown, and Markdown

File Type Extension Key Differences
R Script .R Supports code
R Markdown .Rmd Supports code and formatted text
Markdown .md Supports formatted text

We use all three, but mainly .Rmd for assignments and .md for Git/GitHub documentation.

R Markdown: Three Main Components

  1. YAML header for document title, author, output format, etc. Insert it at the top between ---

  2. Text regular Markdown for headers, lists, links, etc.

  3. Code chunks for code. Insert it with triple backticks ```

Code Chunk Tips

Three ways to insert a code chunk in your R Markdown:

  • Use the RStudio toolbar
  • Type manually ```{r} and ```
  • Use a shortcut
    • Mac: ⌘ + ⌥ + I
    • Windows: Ctrl + Alt + I

R Markdown: Example

This is what we type (YAML, text, code):

---
title: "Homework 1"
author: "Sabrina Nardin"
output: html_document
---

I write my homework description here. 
I can format the text in many ways, for example, by bolding **key concepts**.

```{r}
# create a variable
x <- 5
```

YAML: Yet Another Markup Language

The YAML header sits at the top of your .Rmd and tells R what the document is about and how to display it (e.g., as a html, PDF, Word, etc.). Spacing, indentation, and --- all matters!

The example below shows three fields but many more can be added:

---
title: "Homework 1"
author: "Sabrina Nardin"
output: html_document
---

R Markdown: Formatting Text

Syntax

*Italics*  
**Bold**  
~~Strikethrough~~  
`inline code`

Output

Italics
Bold
Strikethrough
inline code

R Markdown: Unordered Lists

Syntax

+ item 1  
  + sub  
  + sub  
- item 2  
  - sub  
  - sub  
+ item 3  
  - sub  
  * sub  

Output

  • item 1
    • sub
    • sub
  • item 2
    • sub
    • sub
  • item 3
    • sub
    • sub

R Markdown: Ordered Lists

Syntax

1. item 1  
    + sub  
      + sub  
    + sub  

1. item 2  
    * sub  
      * sub  
    * sub  

1. item 3  
    - sub  

Output

  1. item 1
    • sub
      • sub
    • sub
  2. item 2
    • sub
      • sub
    • sub
  3. item 3
    • sub

R Markdown: Formatting Headers

# Heading 1  
## Heading 2  
### Heading 3  

Heading 1

Heading 2

Heading 3

Using # in Code vs. Markdown Text

In Code

# Assign number to variable
x <- 5 
# Assign text to variable
name <- "Sabrina"
  • # starts a COMMENT in R Scripts and R Markdown Code Chunks
  • Comments are ignored when the code runs
  • Use one single # for code explanations or debugging, space doesn’t matter

In R Markdown Text

# Title (h1)
## Section (h2)
### Subsection (h3)
#### More Subsections (h4)
  • # creates a HEADER in Markdown or R Markdown Text
  • The number of # signs = heading level
  • Use as many # as needed for organizing document sections, space matters

R Markdown: Handy Code Chunk Options

Option What it does Default
eval = FALSE Code is not run, only shown; results do not appear. Useful for code examples or showing code with errors. eval = TRUE
include = FALSE Code is run, but both code and results are hidden. Useful for setup code you don’t want to display. include = TRUE
echo = FALSE Code is run, results are shown, but code is hidden. Good when readers only need the output. echo = TRUE
error = TRUE Code is run and shown, even if it throws an error. Useful when demonstrating broken code. error = FALSE
message = FALSE / warning = FALSE Code is run, but messages and warnings are hidden from the output. message = TRUE / warning = TRUE

Check this table and options for more.

Rendering R Markdown Documents

R Markdown lets you create documents that combine text, code, and output, and “render” or “knit” them in formats like html, pdf, word, and more.

Rendering Tips

Three ways to render your R Markdown document:

  • Click “Knit” in RStudio and choose an output

  • Set the format in the YAML header such as html_document,word_document, pdf_document, github_document, etc.

  • Use render() in your console by typing rmarkdown::render("my-document.Rmd", output_format: html_document). More info here

R Markdown: Knitting process

When you “knit” your document, the following sequential things happen:

  1. R Markdown sends the .Rmd file to knitr http://yihui.name/knitr/

  2. Knitr executes all of the code chunks and creates a new plain Markdown .md file which includes the code and its output

  3. This plain Markdown file is then converted by pandoc into any number of output types including html, PDF, Word document, etc.: http://pandoc.org/

💻 Practice R Markdown Syntax

Recap: What We Learned Today

  • Familiarized with Workbench
  • Wrote simple R code
  • Key components of R Markdown
  • How to run and render R Markdown Documents

Reminders

  • Register for a GitHub account and share your GitHub username with us (see Lecture 1 on our website for details)
  • Review today’s materials to prep for the short quiz at the beginning of next class
  • Check Lecture 2 homepage

To print these slides as pdf

Click on the icon bottom-right corner > Tools > PDF Export Mode > Print as a Pdf