Primer 3 · Packages and the pipe
library(), and code that reads in the order the work happens
This page runs R inside your browser. It needs about 22 MB on first load (the R engine, the machinery that runs your code, and dplyr) and a minute or two on a slow connection. If nothing happens after that, your network is probably blocking it. Work through the R primer in RStudio instead.
There is no Stop button. If you write a loop that never ends, reload the page.
Two small things that change how all your later code looks.
Packages
Base R ships with a few thousand functions. Everything else lives in packages other people wrote and published. Using one is 2 steps, and people mix them up:
| Step | Command | How often |
|---|---|---|
| Install: get it onto your disk | install.packages("dplyr") |
once, ever |
| Load: make it available now | library(dplyr) |
every session |
Installing is like buying a book. Loading is like taking it off the shelf. You buy once; you take it off the shelf every time you want to read it.
This page has already loaded dplyr for you before you ran anything. Your own RStudio will not. There, every script starts with its library() calls, and a script that forgets them fails with could not find function.
So the library(dplyr) line below is a no-op here and essential there. Type it anyway. It is the habit that matters, and a missing library() is a common reason a script that worked yesterday fails today.
install.packages() goes in your console, never in a script.
A script that reinstalls its packages every run takes 4 minutes to do 2 seconds of work. On a machine with no internet it fails outright.
The pipe
Read this and try to say what it does:
You had to read it inside out: the innermost bracket happens first. That is the opposite of the order you would describe the work in.
|> is the native pipe. It takes the thing on its left and makes it the first argument of the function on its right. Same answer, read left to right:
“Take these numbers, average them, square-root that, round to two places.” That sentence and that code are now in the same order.
RStudio has a shortcut: Ctrl+Shift+M on Windows and Linux, Cmd+Shift+M on macOS. By default it inserts the other pipe, %>%. Turn on the native one in Tools → Global Options → Code → Use native pipe operator.
You will meet %>% in older code and older books. It behaves almost identically. We use |> because it is part of R itself and needs no package.
Why this matters more than it looks
An analysis is a sequence: start with data, keep some rows, add a column, group it, summarise it. Written with nested brackets, that sequence is written backwards and is very hard to edit. Written with pipes it is a list of steps you can add to, comment out, or reorder.
Comment out any one line with a # and the rest still runs. That is the real payoff, and it is why the next primer is entirely pipes.
A pipe is not magic
It only ever does one thing: put the left side into the first argument slot on the right. These two are identical:
If a function needs the piped value somewhere other than first, the pipe cannot help you, and you write the call normally. That is uncommon in the tidyverse, because tidyverse functions are designed to take the data first for exactly this reason.
What you should now be able to do
- Tell installing a package from loading one, and say where each belongs.
- Start every script with its
library()calls, even though this browser does not make you. - Read and write a
|>pipeline, and explain why it reads better than nested brackets. - Recognise
%>%in other people’s code as the older equivalent.