← Back to Module 0: Languages & Platforms
RStudio Interface Guide
RStudio is the most popular IDE (Integrated Development Environment) for R. It's free, powerful, and designed specifically for statistical analysis and research. If you'll be using R for this course, this is where you'll spend most of your time.
What is RStudio?
RStudio is not the R language itself—it's a program that makes writing R code much easier. Think of it like this: R is the engine, and RStudio is the car with the steering wheel, dashboard, and comfortable seats. You could drive the engine directly (by running R in a terminal), but RStudio makes the experience much more pleasant.
Installing RStudio
Before installing RStudio, you need to install R itself. Here's the order:
- Install R first: Download from cran.r-project.org
- Then install RStudio: Download from posit.co/download/rstudio-desktop
RStudio needs R to function—it's just a nice interface around R. If you try to run RStudio without R installed, it won't work. Always install R first, then RStudio.
The RStudio Interface
When you first open RStudio, you'll see a window divided into four panels. Hover over each numbered region below to learn what it does:
library(tidyverse)
library(fixest)
# Load data
df <- read_csv("data.csv")
Min. 1st Qu. Median Mean 3rd Qu. Max.
15000 35000 52000 58400 75000 250000
> |
Hover over a numbered region to learn about that part of RStudio.
Essential Keyboard Shortcuts
Learning these shortcuts will dramatically speed up your workflow:
| Shortcut (Windows/Linux) | Shortcut (Mac) | Action |
|---|---|---|
Ctrl + Enter |
Cmd + Enter |
Run current line or selection |
Ctrl + Shift + Enter |
Cmd + Shift + Enter |
Run entire script |
Ctrl + L |
Cmd + L |
Clear the Console |
Tab |
Tab |
Autocomplete function/variable names |
Ctrl + Shift + C |
Cmd + Shift + C |
Comment/uncomment selected lines |
Ctrl + S |
Cmd + S |
Save current file |
Your First R Script
Let's write a simple script to make sure everything is working. In RStudio:
- Go to File > New File > R Script (or press
Ctrl+Shift+N) - Type the following code in the Source Editor:
# My first R script
# This is a comment - R ignores lines starting with #
# Print a message
print("Hello, RStudio!")
# Create some data
x <- c(1, 2, 3, 4, 5)
y <- x * 2
# Calculate the mean
mean(y)
# Create a simple plot
plot(x, y, main = "My First Plot")
- Place your cursor on the first line and press
Ctrl+Enterrepeatedly to run each line - Watch the output appear in the Console, and the plot appear in the Plots panel
Setting Up Your Project Folder
Before diving into data analysis, it's essential to organize your work properly. A well-structured project folder makes your research reproducible and helps you (and others) find files easily.
Recommended Folder Structure
For any research project, I recommend creating a folder structure like this:
├── data/ ← Raw data files (never modify these)
│ ├── raw/ ← Original downloaded data
│ └── processed/ ← Cleaned/transformed data
├── code/ ← Your R scripts
│ ├── 01_import.R
│ ├── 02_clean.R
│ └── 03_analysis.R
├── output/ ← Figures, tables, results
│ ├── figures/
│ └── tables/
└── README.md ← Description of your project
You can create this structure using RStudio's Files panel (bottom-right) or by running these commands in the Console:
# Create project folder structure
dir.create("data/raw", recursive = TRUE)
dir.create("data/processed", recursive = TRUE)
dir.create("code")
dir.create("output/figures", recursive = TRUE)
dir.create("output/tables", recursive = TRUE)
Setting Your Working Directory
The working directory is the folder R uses as its starting point for reading and writing files. When you run read.csv("data.csv"), R looks for data.csv in the working directory.
To check your current working directory:
# Where is R looking for files?
getwd()
To change your working directory to your project folder:
# Set working directory (use YOUR path)
setwd("~/Documents/my_project")
# Windows example:
setwd("C:/Users/yourname/Documents/my_project")
# Mac example:
setwd("/Users/yourname/Documents/my_project")
# Method 1: Use the RStudio menu
# Session → Set Working Directory → Choose Directory...
# Method 2: Use the Files panel
# Navigate to your project folder in the Files panel (bottom-right)
# Click "More" (gear icon) → "Set As Working Directory"
# Method 3: Use RStudio Projects (Recommended!)
# File → New Project → creates a .Rproj file
# Double-click the .Rproj file to open RStudio with the correct directory
The best practice is to create an RStudio Project for each research project. Go to File → New Project and choose "Existing Directory" or "New Directory". This creates a .Rproj file in your folder. When you double-click this file, RStudio opens with the working directory already set correctly!
Practice: Download Real Data
Let's practice by downloading real economic data. Our World in Data provides excellent, well-documented datasets perfect for economics research. We'll use this data source throughout the course.
- Visit Our World in Data Datasets on GitHub
- Or download directly: GDP per capita (Maddison Project Database)
- Save the file to your
data/raw/folder
Alternatively, download it directly with R:
# Download GDP per capita data from Our World in Data
url <- "https://raw.githubusercontent.com/owid/owid-datasets/master/datasets/GDP%20per%20capita%20(Maddison%20Project%20Database%202020)/GDP%20per%20capita%20(Maddison%20Project%20Database%202020).csv"
# Download and save to data/raw folder
download.file(url, destfile = "data/raw/gdp_per_capita.csv")
Exploring Data in RStudio
Once you have data in your project, RStudio makes it easy to explore:
- Files Panel: Navigate to your
data/raw/folder and click on the CSV file to preview it - Load and View: Use R code to load and inspect the data:
# Load the data
gdp_data <- read.csv("data/raw/gdp_per_capita.csv")
# Quick look at the data
head(gdp_data)
# Structure of the data
str(gdp_data)
# Open in spreadsheet viewer (click the data frame in Environment panel too!)
View(gdp_data)
After loading data, look at the Environment panel (top-right). You'll see your data frame listed with its dimensions (e.g., "1000 obs. of 5 variables"). Click on the name to open it in RStudio's built-in data viewer—a spreadsheet where you can sort columns and search for values.
Cloud Alternative: Posit Cloud
If you don't want to install anything, you can use Posit Cloud (formerly RStudio Cloud) in your browser. It's the same RStudio interface, running in the cloud.
Video Tutorial
For a visual walkthrough of the RStudio interface, I recommend this official video from Posit:
- RStudio IDE Tutorial (YouTube, ~8 min) — A quick tour of the interface
- RStudio User Guide — Official documentation