RStudio
Basics
Learn to write and run R code, inspect objects in your environment, and manage projects like a pro using RStudio's integrated development environment.
What is RStudio?
RStudio is an Integrated Development Environment (IDE) designed specifically for the R programming language. It wraps around R itself, giving you a powerful visual interface with panels for writing code, viewing results, managing files, and exploring data: all in one window.
The Four-Pane Layout
Why RStudio Matters
The Console: Your Direct Line to R
The Console is where R listens and responds. You type a command after the > prompt, press Enter, and R immediately evaluates it and prints the result.
[1] 22
> sqrt(144)
[1] 12
> greeting <- "Hello, R!"
> greeting
[1] "Hello, R!"
The Source Editor: Write Reusable Code
The Source Editor is where you write R scripts (files ending in .R). Unlike the Console, everything you write here is saved and can be re-run any time.
Console vs Source Editor
| Console | Source Editor | |
|---|---|---|
| Purpose | Quick experiments | Reusable scripts |
| Saved? | No: lost on close | Yes: .R files persist |
| Execution | One command at a time | Line, selection, or entire file |
| Best for | Testing, exploring | Analysis, reproducibility |
| Keyboard shortcut | Type + Enter | Ctrl+Enter per line |
The Environment Pane
The Environment pane (top-right) shows every object currently in memory: variables, data frames, vectors, functions, and more. It updates live as you run code.
Creating Objects in R
In R, you store values using the assignment operator <-. The value on the right is saved into the name on the left, and it appears in the Environment pane.
age <- 17
# Store a text string
school <- "Lincoln High"
# Store a vector of numbers
grades <- c(92, 85, 88, 95, 90)
# Compute with objects
avg <- mean(grades)
avg
[1] 90
RStudio Projects
An RStudio Project is a folder on your computer that RStudio treats as the "home base" for your work. It automatically sets the working directory to that folder, so file paths stay simple and portable.
Creating a New Project
Essential Keyboard Shortcuts
| Action | Windows / Linux | Mac |
|---|---|---|
| Run current line | Ctrl+Enter | Cmd+Enter |
| Run entire script | Ctrl+Shift+Enter | Cmd+Shift+Enter |
| Assignment operator <- | Alt+ - | Option+ - |
| New R script | Ctrl+Shift+N | Cmd+Shift+N |
| Save file | Ctrl+S | Cmd+S |
| Find and replace | Ctrl+F | Cmd+F |
| Comment/uncomment | Ctrl+Shift+C | Cmd+Shift+C |
Example: Running Your First Script
Let's walk through a basic workflow: create a variable, compute with it, and check the result.
temperatures <- c(72, 68, 75, 80, 69, 74, 77)
# Step 2: Calculate the mean
avg_temp <- mean(temperatures)
# Step 3: Print the result
avg_temp
[1] 73.57143
Example: Inspecting Objects
R provides several functions to inspect what an object contains and how it is structured.
students <- data.frame(
name = c("Ana", "Ben", "Cara"),
score = c(95, 82, 91)
)
str(students) # Structure
'data.frame': 3 obs. of 2 variables:
$ name : chr "Ana" "Ben" "Cara"
$ score: num 95 82 91
nrow(students) # Number of rows
[1] 3
head(students) # First few rows
Example: Reading Data from a File
With an RStudio Project, reading files is straightforward because the working directory is already set to your project folder.
survey <- read.csv("data/survey.csv")
# Check what we loaded
dim(survey)
[1] 200 8
# View it as a spreadsheet
View(survey)
# Quick summary statistics
summary(survey$age)
Min. 1st Qu. Median Mean 3rd Qu. Max.
14.00 15.00 16.00 16.23 17.00 19.00
Common Mistakes to Avoid
RStudio Workflow Summary
Question 1 of 5
Which RStudio pane is used to write and save reusable R scripts?
Question 2 of 5
What does the Environment pane display?
Question 3 of 5
What is the keyboard shortcut to run the current line of code from the Source Editor on Windows?
Question 4 of 5
Why should you use an RStudio Project instead of just opening R files directly?
Question 5 of 5
You type str(my_data) in the Console. What does this function do?