RStudio & Running Code
RStudio is the standard workspace for R — a single window that combines a script editor, a live console, and panes for your variables, plots, and help, so you can write and run R comfortably.
Learn RStudio & Running Code in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll know how R and RStudio relate, the difference between the console and a script, how to run code line by line or all at once, and how the working directory affects file paths.
What You'll Learn in This Lesson
1️⃣ Scripts: Code You Keep
A script is a plain text file (ending in .R ) full of R commands. Unlike typing into the console, a script is a permanent record you can re-run, edit, and share. Inside RStudio you write in the top-left editor pane and send lines to the console to run them.
Notice cat() prints values without quotes and lets you mix text and numbers, while "\\n" adds a line break. message() is handy for status updates that aren't part of your results.
2️⃣ The Console: Instant Feedback
The console runs code the moment you press Enter — perfect for quick checks. Two functions are invaluable for exploring: ls() lists the objects you've created, and str() ("structure") shows the shape of any object at a glance.
str() told us we have a numeric vector of 3 values: num [1:3] 1 2 3 . You'll reach for str() constantly once you start loading real data.
3️⃣ The Working Directory
When you read or write files, R uses the working directory as its starting point. getwd() shows it; setwd("path") changes it. Getting this right prevents the most common beginner frustration: "R can't find my file."
Your turn. Fill in the # TODO blank and run it, comparing with the expected output.
Write this one from scratch using the outline, then run it and check it against the example output.
📋 Quick Reference — Running Code
Practice quiz
What is the relationship between R and RStudio?
- RStudio is the language; R is the editor
- R is the language/engine; RStudio is an IDE around it
- They are the same program
- R replaced RStudio
Answer: R is the language/engine; RStudio is an IDE around it. R is the language and engine; RStudio is an IDE that sits on top of it.
What file extension does an R script use?
- .R
- .py
- .txt
- .rmd
Answer: .R. R scripts are plain text files ending in .R.
Which function lists the objects you have created this session?
- str()
- getwd()
- ls()
- list()
Answer: ls(). ls() lists the names of objects in your environment.
What does getwd() return?
- The R version
- A list of files
- Your variables
- The current working directory path
Answer: The current working directory path. getwd() shows the folder R uses as its working directory.
Which function gives a compact summary of an object's structure?
- str()
- ls()
- cat()
- message()
Answer: str(). str() reports the structure and type of any object.
What does cat() do compared to printing?
- Saves to a file
- Prints values without quotes, mixing text and numbers
- Only prints numbers
- Returns a value silently
Answer: Prints values without quotes, mixing text and numbers. cat() concatenates and prints without quotation marks.
What does " " produce inside cat()?
- A tab
- A backslash
- A newline (line break)
- A space
Answer: A newline (line break). is the newline escape, so output starts on a new line.
How do you run an entire saved script from the console?
- run("a.R")
- import("a.R")
- exec("a.R")
- source("a.R")
Answer: source("a.R"). source("a.R") executes every line of the saved file in order.
What is the keyboard shortcut to run the current line in RStudio?
- Ctrl/Cmd + Enter
- Ctrl + S
- Alt + R
- Shift + Space
Answer: Ctrl/Cmd + Enter. Ctrl/Cmd + Enter sends the current line to the console and advances.
Why does an RStudio Project (.Rproj) help with file paths?
- It compiles your code
- It hides the console
- It sets the working directory automatically
- It installs packages
Answer: It sets the working directory automatically. A Project sets the working directory for you, avoiding path headaches.