First Steps in RStudio

Before real code: find your way around RStudio, save your work properly, get help when you are stuck, and read R's error messages without panic.

🟢 Beginner ⏱️ ~45 min 📊 R · RStudio

Before you start

  • Ideally, R and RStudio are installed (Installing R and RStudio). Not yet? Read on anyway: the error section runs in your browser.
  • No coding experience needed.
  • Any term new? The Glossary has it.

Learning objectives

By the end of this lesson you will be able to: name RStudio's four panes and what each is for; write, run and save code in a script; set up an RStudio Project so R always finds your files; look up help for any function; tell installing a package apart from loading it; and read and fix the six error messages beginners meet most.

Why this lesson exists

Most beginners do not quit because R is hard. They quit because a red error appears and nobody told them what it means, or because they lose their work in the Console. This short lesson saves hours of frustration later.

1. The four panes of RStudio

When you open RStudio you see one window split into four areas, called panes. (If you only see three, go to File, then New File, then R Script, and the fourth one appears.)

RStudio 1. Source (your script) Write code you want to keep. Saved as a .R file. 2. Console (R answers here) Quick one-off lines and all output. > mean(c(2, 4, 6)) [1] 4 > 3. Environment Everything R is holding in memory right now. gc 41.5 genes 3 obs. of 3 4. Files, Plots, Help Tabs: your folder, your graphs, help pages and installed packages.
RStudio's default layout. You can move panes around later (Tools, Global Options, Pane Layout), but most tutorials assume this one.
PaneWhat it is forWhen you use it
Source (top left)Your script: a text file of R code.Almost always. Anything you might need again goes here.
Console (bottom left)Where R runs code and prints answers.Quick checks, like nrow(genes). Nothing typed here is saved.
Environment (top right)A list of every object R is currently holding.To check that a step worked, for example that your data loaded.
Files / Plots / Packages / Help (bottom right)Tabs for your folder, graphs, installed packages and help pages.Plots appear here automatically. Help pages too.

2. Console or script? Use a script

The Console is like talking: quick, but gone once said. A script is like writing it down: you can rerun it, fix it, and share it. Professionals write almost everything in scripts, because the script is the record of the analysis.

  1. Create one: File, then New File, then R Script.
  2. Type a line, for example mean(c(2, 4, 6)).
  3. Run it: put your cursor on the line and press Ctrl+Enter (Windows/Linux) or Cmd+Return (Mac). The line is sent to the Console and the answer appears there.
  4. Save it: Ctrl+S or Cmd+S. Give it a short name without spaces, like first_analysis.R.
Shortcut (Windows/Linux)MacWhat it does
Ctrl+EnterCmd+ReturnRun the current line (or the lines you selected)
Ctrl+Shift+EnterCmd+Shift+ReturnRun the whole script
Alt+-Option+-Type the assignment arrow <-
Ctrl+SCmd+SSave the script
Esc (in the Console)EscCancel a stuck line (see the + prompt below)

The two prompts: > and +

In the Console, > means "R is ready". A + means "R is waiting for you to finish": you opened a bracket or a quote and never closed it. Either type the missing piece and press Enter, or press Esc to cancel and start that line again.

3. RStudio Projects: so R always finds your files

Every R session has a working directory: the folder R looks in when you give just a file name. If your file is somewhere else, R says it cannot find it. An RStudio Project fixes this for good: it ties a folder to your work, and whenever you open the project, that folder becomes the working directory automatically.

penguin_project/ the project folder = working directory penguin_project.Rproj data/ scripts/ results/ double-click this to open the project your input files (CSV, counts...) your .R scripts plots and tables you produce
One folder per analysis. Inside the project, read.csv("data/my_file.csv") works on any computer, because the path starts from the project folder.

Make your first project (two minutes)

  1. File, then New Project, then New Directory, then New Project.
  2. Give it a name (for example penguin_project), choose where it lives, and click Create Project.
  3. RStudio restarts inside the project. The Files tab now shows the folder and a .Rproj file.
  4. Next time, open the project by double-clicking that .Rproj file (or File, then Open Project).
getwd()                              # check: it should print your project folder
my_data <- read.csv("data/my_file.csv")   # a path that starts from the project folder

One setting worth changing now

Go to Tools, then Global Options, then General. Untick "Restore .RData into workspace at startup" and set "Save workspace to .RData on exit" to Never. Each session then starts clean, so your script, not leftover memory, is the true record of what you did. If things ever get weird, use Session, then Restart R, and rerun your script from the top.

4. Packages: install once, load every time

A package is a bundle of extra functions someone else wrote, like the tidyverse or DESeq2. Using one takes two different steps, and mixing them up is a classic beginner trap.

install.packages("ggplot2") Buy the book, put it on the shelf Once per computer (needs internet) library(ggplot2) Take it off the shelf to read Every new session, top of script
Install is a one-time download. Loading has to happen again each time R starts.
install.packages("palmerpenguins")   # ONCE, in the Console (note the quotes)
library(palmerpenguins)              # EVERY session, at the top of your script

Keep install.packages() out of your script

Put library() lines at the top of the script, but run install.packages() in the Console. Otherwise the package re-downloads every time you run the script.

5. Getting help

Nobody remembers every function. Professionals look things up all day. R has a help page for every function, and RStudio shows it in the Help tab.

?mean                       # the help page for mean()
help(mean)                  # the same thing, written out
example(mean)               # run the examples at the bottom of the help page
??"standard deviation"     # search all help pages for a phrase

Help pages look dense, but you only need to read three parts at first:

SectionWhat it tells you
UsageHow to call the function, and the default value of each option. mean(x, trim = 0, na.rm = FALSE, ...) tells you na.rm is FALSE unless you change it.
ArgumentsWhat each input means, for example that na.rm = TRUE drops missing values first.
ExamplesWorking code at the very bottom. Usually the fastest way to understand a function: copy it, run it, change it.
Still stuck? Copy the exact error message into a search engine: someone has almost certainly hit it before. When you ask a person (a forum, a classmate, our Help page), include the code you ran, the full error, and a small piece of data that reproduces it.

6. Your first errors, and what they mean

Errors are not a sign you are bad at this. Experienced programmers see them all day; they have just learned to read them. Each box below contains a deliberate mistake. (In RStudio the same errors look slightly longer, for example Error in mena(c(2, 4, 6)) : could not find function "mena"; the part after the colon is what matters.) Press Run, read the error and the yellow hint, then fix the code and Run again. (These boxes run real R in your browser; the first Run downloads it, so allow up to a minute.)

Error: object 'Gene_count' not found

Means: R does not know that name. Usual causes: a typo, wrong capital letters (R treats gene_count and Gene_count as different names), or you never ran the line that creates it.

Error: could not find function "mena"

Means: R does not know that function. Usual causes: a typo, or the function lives in a package you have not loaded with library() yet (for example calling ggplot() before library(ggplot2)).

Error: unexpected numeric constant (or symbol, or string constant)

Means: R could not read your code as a sentence. Usual cause: a missing comma between items, or a missing operator. R points with ^ at roughly where it got confused.

Error: unexpected end of input (or a + that will not go away)

Means: something was opened and never closed: a bracket (, a brace {, or a quote ". In RStudio's Console this shows up as the + prompt; press Esc and fix the line.

Error: non-numeric argument to binary operator

Means: you tried to do maths on text. Usual cause: a number written in quotes ("5" is text, 5 is a number), or a column that R read as text because one cell contained a word.

Error: there is no package called 'palmerpenguins'

Means: you ran library() for a package that is not installed on this computer. Fix: run install.packages("palmerpenguins") once in the Console, then library(palmerpenguins) again. (This one only happens in RStudio, so there is no box for it here.)

A warning is not an error

Red text that starts with Warning means R did the job but wants you to notice something, for example as.numeric("abc") gives NA with "NAs introduced by coercion". Read it, decide whether it matters, and carry on. Only Error means R stopped.

A three-step routine for any error

  1. Read the last line of the red text. It is usually the useful part.
  2. Check the usual suspects on that line and the line above: spelling and capitals, commas, matching brackets and quotes, and whether a needed library() ran.
  3. Still stuck? Restart R (Session, then Restart R), rerun your script from the top, and if it still fails, search the exact message.

Practice: fix the broken script

This code has three mistakes. Fix them one at a time, pressing Run after each fix, and let the error messages guide you. One tip: R sometimes points at the line after the real mistake. If the line it names looks fine, check that the line above closed all its brackets. Press Reset to start over.

Show the fixed version
my_genes <- c("TP53", "BRCA1", "EGFR")     # 1. missing comma after "BRCA1"
gene_lengths <- c(19.1, 81.2, 188.3)       # 2. missing closing bracket
mean(gene_lengths)                         # 3. lowercase g, to match the name
# [1] 96.2

Every key word, in plain English

pane
one of the four areas of the RStudio window.
Console
where R runs code and prints answers; nothing typed there is saved.
script
a saved text file of R code (ending in .R) that you can rerun.
Environment
the list of objects R is holding in memory right now.
working directory
the folder R looks in when you give just a file name.
RStudio Project
a folder with a .Rproj file; opening it makes that folder the working directory.
package
a bundle of extra functions. Install once with install.packages(), load each session with library().
error
R stopped and did not finish the line.
warning
R finished but flags something you should check.

Check your understanding

You want to keep the code for an analysis so you can rerun it next week. Where should you write it?
Right. Nothing typed in the Console is saved. A script is a file you can rerun, fix and share, so it is the record of your analysis.
The Console shows a + instead of > and nothing happens when you press Enter. What is going on?
Right. + means "keep going, I am waiting for the rest". Type the missing ) or ", or press Esc to cancel the line.
Yesterday you ran install.packages("ggplot2"). Today, in a fresh session, ggplot() gives "could not find function". What do you need to do?
Right. The package is still on your computer. It just is not loaded into today's session until you call library(ggplot2).
You run Mean_length and get "object 'Mean_length' not found", but you are sure you created mean_length earlier. What is the most likely problem?
Right. Capital letters matter in R. Check the Environment pane to see the exact names R is holding.
Why use an RStudio Project instead of starting your script with setwd("C:/Users/me/Desktop/analysis")?
Right. A hard-coded setwd() path only exists on one machine. With a project, paths like "data/my_file.csv" start from the project folder wherever it lives.

Sources & further reading

  1. Wickham H, Cetinkaya-Rundel M, Grolemund G. R for Data Science, 2nd ed. (2023), chapter 6 "Workflow: scripts and projects". r4ds.hadley.nz
  2. Posit. "Using RStudio Projects" (support article). support.posit.co
  3. Posit. RStudio IDE User Guide: "Pane Layout". docs.posit.co
  4. Bryan J. "Project-oriented workflow". Tidyverse blog (2017). tidyverse.org

Last reviewed: October 2026

Next in the R series (3 of 5)

R Level 1 (Basics): values, vectors, data frames and your first plot →