01 / 22
R

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.

R Programming

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

Source Editor Write & edit scripts my_data <- read.csv("data.csv") summary(my_data) # Analysis script Environment Inspect objects & data my_data 150 obs. of 5 vars x num [1:100] result 42 Console Run code interactively > 2 + 2 [1] 4 > _ Files / Plots / Help Navigate & visualize data.csv analysis.R README.md

Why RStudio Matters

Syntax Highlighting
Code is color-coded automatically, making it easier to read and spot errors at a glance.
Auto-Complete
Press Tab to see suggestions for function names, arguments, and file paths as you type.
Integrated Help
Type ?function_name in the console and documentation appears instantly in the Help pane.

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.

> 3 * 7 + 1
[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.

1
Create a script: File > New File > R Script (or press Ctrl+Shift+N).
2
Write your code in the editor pane, line by line.
3
Run a single line: Place cursor on it and press Ctrl+Enter (Cmd+Enter on Mac).
4
Run the entire script: Press Ctrl+Shift+Enter or click the "Source" button.

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.

Environment Name Type Value x num 42 name chr "Alice" scores num [1:5] 90 85 92 88 95 my_data data.frame 150 obs. of 5 variables Broom: Clear All

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.

# Store a number
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.

my_project/ my_project.Rproj data/ scripts/ output/ survey.csv analysis.R results.png Working directory = my_project/

Creating a New Project

1
Open RStudio and go to File > New Project.
2
Choose "New Directory" and then "New Project".
3
Name your project (e.g., my_analysis) and pick a location on your computer.
4
Click "Create Project". RStudio creates a folder with a .Rproj file inside.
5
Add subfolders like data/, scripts/, and output/ to stay organized.

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.

# Step 1: Create some data
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.

# Create a data frame
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.

# Read a CSV file from the data/ subfolder
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

Not Using Projects
Working without a .Rproj file means you have to manually set the working directory every time. This breaks reproducibility.
Typing Only in Console
Console commands vanish when you close RStudio. Always save important code in a .R script file.
Using Absolute Paths
Paths like "C:/Users/me/Desktop/data.csv" break on other computers. Use relative paths inside a project.
Ignoring Error Messages
R's error messages tell you exactly what went wrong. Read them carefully: they mention the line number and the issue.

RStudio Workflow Summary

1. Create New Project 2. Write R Script (.R) 3. Run Ctrl+Enter 4. Inspect Environment Pane 5. Debug Fix errors in Console 6. Save Ctrl+S, share project Iterate: write, run, inspect, debug, repeat

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?