x <- 5
y <- 10
x + y[1] 15
This course is designed to help you become familiar with R and RStudio, you’re not expected to become a professional R programmer at the end of it. There is a learning curve and there will be moments where you might want to give up (or throw your computer), but I encourage you to stick with it. One day, after practicing and potentially crying about it, it will click and you will be so happy you stuck with it. I believe in you.
I’ve designed this course using free online resources - since R, itself, is a free program. You’re going to want to bookmark the following resources as you will definitely be using these for the duration of this course (and possible beyond):
By the end of this week, you should be able to:
Data science is an interdisciplinary field that focuses on:
It combines skills from:
In public health, data science is used to turn complex health data into actionable evidence.
Public health decisions affect entire populations. Data science allows public health professionals to:
Without data science, public health decisions would rely more heavily on anecdotal evidence rather than systematic analysis.
Public health data come from many sources, including:
Data science methods help integrate these data sources into a coherent analytical framework.
Data science helps:
Timely analysis can inform rapid public health responses.
Data science allows researchers to:
These analyses support equity-focused public health interventions.
Public health programs and policies must be evaluated to determine whether they work.
Data science supports:
Using historical data, data science can:
These models complement traditional epidemiologic approaches.
R is widely used in public health because it:
Learning R allows public health professionals to implement data science workflows transparently and efficiently.
Reproducibility is a core principle of both data science and public health.
Best practices include:
Reproducible workflows increase trust in public health findings.
R is a programming language used for:
R itself runs in the background. It does the actual computation, stores objects in memory, and executes your code.
RStudio is an integrated development environment (IDE) for R.
Think of R as the engine of a car, and RStudio as the dashboard. RStudio makes it easier to:
You can use R without RStudio, but almost everyone uses R through RStudio because it is more efficient and user friendly.
If R is not installed, RStudio will not work.
When you open RStudio, you typically see four panes.
This is where you:
.R),.qmd),Nothing in this pane runs until you tell R to run it.
The Console is where:
You can type code directly here, but for reproducibility, most code should live in scripts or Quarto files.
The Environment tab shows:
If you restart R, this pane clears because the session memory resets.
This pane lets you:
An R session is the current working instance of R.
During a session:
When you restart R:
This is why reproducible workflows matter.
Write code in the Source pane, for example:
x <- 5
y <- 10
x + y[1] 15
Using keyboard shortcuts will make you faster, reduce errors, and improve reproducibility. These shortcuts work in RStudio unless otherwise noted.
| Action | Windows / Linux | macOS |
|---|---|---|
| Run current line / selection | Ctrl + Enter |
Cmd + Enter |
| Run entire script | Ctrl + Alt + R |
Cmd + Option + R |
| Re-run previous command | ↑ |
↑ |
| Action | Windows / Linux | macOS |
|---|---|---|
| Comment / uncomment lines | Ctrl + Shift + C |
Cmd + Shift + C |
| Re-indent code | Ctrl + I |
Cmd + I |
| Auto-complete | Tab |
Tab |
| Show function arguments | Ctrl + Shift + A |
Cmd + Shift + A |
| Rename object (refactor) | Ctrl + Shift + Alt + M |
Cmd + Shift + Option + M |
| Action | Windows / Linux | macOS |
|---|---|---|
| New script | Ctrl + Shift + N |
Cmd + Shift + N |
| Save file | Ctrl + S |
Cmd + S |
| Switch between tabs | Ctrl + Tab |
Cmd + Option + → |
| Zoom active pane | Ctrl + Shift + 1 |
Cmd + Shift + 1 |
| Restore panes | Ctrl + Shift + 0 |
Cmd + Shift + 0 |
| Action | Windows / Linux | macOS |
|---|---|---|
| Clear console | Ctrl + L |
Cmd + L |
| Interrupt running code | Esc |
Esc |
| Restart R session | Ctrl + Shift + F10 |
Cmd + Shift + F10 |
| Clear workspace | Ctrl + Shift + F9 |
Cmd + Shift + F9 |
| Action | Windows / Linux | macOS |
|---|---|---|
Insert pipe %>% or |> |
Ctrl + Shift + M |
Cmd + Shift + M |
Insert assignment <- |
Alt + - |
Option + - |
| Action | Windows / Linux | macOS |
|---|---|---|
| Knit / Render document | Ctrl + Shift + K |
Cmd + Shift + K |
| Insert code chunk | Ctrl + Alt + I |
Cmd + Option + I |
| Preview Quarto document | Ctrl + Shift + K |
Cmd + Shift + K |
If you memorize only a few, start with these:
Ctrl/Cmd + EnterCtrl/Cmd + Shift + MCtrl/Cmd + Shift + CCtrl/Cmd + Shift + F10Ctrl/Cmd + Shift + KYou can view or customize shortcuts in RStudio: Tools → Modify Keyboard Shortcuts
In R, the words package and library are often used interchangeably.
A package is a bundle of:
A library is the collection of installed packages on your computer, and library() is the function we use to load a package for use in the current session.
Base R can do a lot, but packages make R powerful and efficient. We use packages to:
In this course, you’ll use packages so that everyone can run the same code and get the same results.
Install a package once (or whenever you update R / need a newer version).
Load a package every time you start a new R session and want to use it.
Think of it like an app:
Use install.packages() with the package name in quotes.
The first package we will install is tidyverse. We will also install quarto.