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.)
| Pane | What it is for | When 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.
- Create one: File, then New File, then R Script.
- Type a line, for example
mean(c(2, 4, 6)). - 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.
- Save it: Ctrl+S or Cmd+S. Give it a short name without spaces, like
first_analysis.R.
| Shortcut (Windows/Linux) | Mac | What it does |
|---|---|---|
Ctrl+Enter | Cmd+Return | Run the current line (or the lines you selected) |
Ctrl+Shift+Enter | Cmd+Shift+Return | Run the whole script |
Alt+- | Option+- | Type the assignment arrow <- |
Ctrl+S | Cmd+S | Save the script |
Esc (in the Console) | Esc | Cancel 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.
read.csv("data/my_file.csv") works on any computer, because the path starts from the project folder.Make your first project (two minutes)
- File, then New Project, then New Directory, then New Project.
- Give it a name (for example
penguin_project), choose where it lives, and click Create Project. - RStudio restarts inside the project. The Files tab now shows the folder and a
.Rprojfile. - Next time, open the project by double-clicking that
.Rprojfile (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("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:
| Section | What it tells you |
|---|---|
| Usage | How 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. |
| Arguments | What each input means, for example that na.rm = TRUE drops missing values first. |
| Examples | Working code at the very bottom. Usually the fastest way to understand a function: copy it, run it, change 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
- Read the last line of the red text. It is usually the useful part.
- 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. - 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.2Every 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
+ instead of > and nothing happens when you press Enter. What is going on?+ means "keep going, I am waiting for the rest". Type the missing ) or ", or press Esc to cancel the line.install.packages("ggplot2"). Today, in a fresh session, ggplot() gives "could not find function". What do you need to do?library(ggplot2).Mean_length and get "object 'Mean_length' not found", but you are sure you created mean_length earlier. What is the most likely problem?setwd("C:/Users/me/Desktop/analysis")?"data/my_file.csv" start from the project folder wherever it lives.Sources & further reading
- Wickham H, Cetinkaya-Rundel M, Grolemund G. R for Data Science, 2nd ed. (2023), chapter 6 "Workflow: scripts and projects". r4ds.hadley.nz
- Posit. "Using RStudio Projects" (support article). support.posit.co
- Posit. RStudio IDE User Guide: "Pane Layout". docs.posit.co
- Bryan J. "Project-oriented workflow". Tidyverse blog (2017). tidyverse.org
Last reviewed: October 2026
