Primer 3 · Packages and the native pipe

Loading the tools, and chaining them together

ImportantIf the cells below never become ready

This page runs R inside your browser. It needs about 22 MB on first load (the R engine, the machinery that runs and marks your code, and the dplyr package) 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. Everything taught here is taught there too.

There is no Stop button. If you write a loop that never ends, reload the page.

tibble() in Primer 2 was not part of base R. It arrived in a package, and a package’s functions are out of reach until you ask for them. This page covers the two lines of plumbing that every tidyverse script needs: library(), which puts a package’s functions within reach, and |>, which chains those functions together in the order you think about them. For the full explanation, see the R primer.

WarningOne way this browser lies to you

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.

Packages

A package is a bundle of functions somebody else wrote and shared. You install it once, ever, and load it every session.

install.packages("dplyr")   # once, ever, in the console
library(dplyr)              # every session, at the top of every script

library() takes the package name without quotation marks. Run it, then use a function that only exists because of it:

n_distinct() counts distinct values: five enrolments there, but only 2 distinct qualifications. It comes from dplyr, not from base R. Without library(dplyr) in your own RStudio, that line stops with could not find function "n_distinct", the single most common error a beginner meets.

Warninginstall.packages() does not belong in your script

Put library() calls at the top of a script; keep install.packages() in the console. A script that reinstalls its packages on every run is slow, needs an internet connection it should not need, and is rude to whoever else runs it.

The native pipe |>

The pipe passes the thing on its left into the function on its right as that function’s first argument. These two lines do exactly the same thing:

Both print 77.8. The first has to be read from the inside out: find the innermost bracket, work outwards, and keep track of which closing bracket belongs to what. The second reads left to right, in the order the work actually happens: take these scores, take the mean, round to one decimal. Once an analysis is six steps long, the pipe is the difference between readable and unreadable code.

You will meet %>% in older code and in most tutorials online. For everything in this workshop it behaves the same way, but we use |> because it is built into R itself and needs no package.

NoteExercise 1: unpick the nesting

Time: 5 minutes

Four stations were marked out of 100: 16, 25, 36 and 49. Somebody has written their root-mean score inside out. Rewrite it using |> so that it reads left to right: take the vector, take its mean(), take the sqrt() of that, then round() to 2 decimal places. The answer is 5.61 either way.

c(16, 25, 36, 49) |> mean() |> sqrt() |> round(2)
c(16, 25, 36, 49) |> mean() |> sqrt() |> round(2)

Each step is one |> followed by a function with its brackets: mean(), then sqrt(), then round(2). The piped value fills the first argument, so round(2) sets only the digits.

NoteExercise 2: mean attendance, piped

Time: 5 minutes

Four students attended 88%, 72%, 95% and 64% of sessions. Starting from that vector, pipe it into mean() and then into round(1) to get the mean attendance to one decimal place.

c(88, 72, 95, 64) |> mean() |> round(1)
c(88, 72, 95, 64) |> mean() |> round(1)

The same shape as Exercise 1, one step shorter. round(1) means one decimal place. The piped value is already the first argument.

Check yourself

NoteFour questions on the Moodle course

Take Check yourself · Primer 3 on the Moodle course. Four questions, unlimited attempts, no marks. Every answer comes back with an explanation, so a wrong one costs you nothing and teaches you something.

Next

→ Primer 4 · The five dplyr verbs: the verbs that do almost all of the work.

Back to top