tl;dr
I made it so you can launch RStudio in the browser with R v4.2 installed—thanks to the Binder service—so you can try out the new pipe |>
and anonymous-function \()
syntax.
Just browsering
Want to try R v4.2 from the safety of your browser without installing any software?
Maybe your organisation hasn’t yet moved to version 4.1 or higher, but you want a chance to noodle around with its cool new syntax that all the hip young trendsetters are yakking about.
Click the ‘launch binder’ badge below to launch R v4.2 and RStudio in your browser, thanks to the Binder project and {holepunch} package.1 You may need to wait a few moments for it to build.
Once loaded, click on the get-started.R
file in the ‘files’ pane for a very simple introductory script with some basic introductions to the new syntax.
Two packages are also installed with the Binder instance:
- The {dplyr} package, authored by Hadley Wickham, Romain François, Lionel Henry and Kirill Müller, so you can compare the base pipe against the {magrittr} pipe (
%>%
), which was created by Stefan Milton-Bache and made popular by the tidyverse. - The {pipebind} package by Brenton Wiernik, so you can explore some methods for extending the functionality of the base pipe
Untaxing syntax
There are two major new features to try: the base pipe |>
and anonymous-function syntax \()
(sometimes referred to as ‘lambdas’), which were both introduced in R v4.1 (May 2021).
From R news:
R now provides a simple native forward pipe syntax |>. The simple form of the forward pipe inserts the left-hand side as the first argument in the right-hand side call. The pipe implementation as a syntax transformation was motivated by suggestions from Jim Hester and Lionel Henry.
R now provides a shorthand notation for creating functions, e.g. (x) x + 1 is parsed as function(x) x + 1.
An underscore placeholder _
for the right-hand side of a base pipe was introduced in R v4.2 (April 2022). From R news:
In a forward pipe |> expression it is now possible to use a named argument with the placeholder _ in the rhs [right-hand side] call to specify where the lhs [left-hand side] is to be inserted. The placeholder can only appear once on the rhs.
Who’s been piping up?
This post isn’t about how to use the new syntax or the motivation behind it.
This post exists, at best, to help you play with R v4.2 and the latest features without installing anything. At worst, it might make you aware that the base pipe exists, or that Binder is magic.
I suggest you take a look at the following materials for more information:
- Hadley Wickham’s ‘Pipes’ chapter in the work-in-progress second edition of R for Data Science (R4DS), which talks about why to use it and how it compares to
%>%
- Michael Barrowman’s post about the speed of the new base pipe and what it’s doing under the hood
- Sharon Machlis’s post that provides an introduction and also points to materials for running different R versions in a Docker container
- Isabella Velásquez’s post, with a story about solving a plotting problem with the base pipe
- Elio Campitelli’s post that covers a number of things, including implications for {data.table}
- Miles McBain’s post on the awkward ‘dog balls’ syntax for constructing anonymous functions on the right-hand side of a base pipe, which is partially fixed by the introduction of the underscore placeholder in R v4.22
- Brenton Wiernik’s Twitter thread about the {pipebind} package to help address some of the base pipe’s shortcomings, including use of the placeholder multiple times on the right-hand side
- Adolfo Álvarez’s charming post on the history of pipes in R, including the inception of the base pipe
And there’s probably loads more I’m missing. Let me know about them.
Regardless, this all very exciting for me because I have strong feelings about symbols in R. Do read my theory about how $
notation is an INTERNATIONAL CONSPIRACY and YOU ARE COMPLICIT. Or my method for avoiding scripts that use the equals symbol for assignment, yuck!
Session info
## ─ Session info ───────────────────────────────────────────────────────────────
## setting value
## version R version 4.2.0 (2022-04-22)
## os macOS Big Sur/Monterey 10.16
## system x86_64, darwin17.0
## ui X11
## language (EN)
## collate en_GB.UTF-8
## ctype en_GB.UTF-8
## tz Europe/London
## date 2022-06-13
## pandoc 2.17.1.1 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/ (via rmarkdown)
##
## ─ Packages ───────────────────────────────────────────────────────────────────
## package * version date (UTC) lib source
## blogdown 1.9 2022-03-28 [1] CRAN (R 4.2.0)
## bookdown 0.26 2022-04-15 [1] CRAN (R 4.2.0)
## bslib 0.3.1 2021-10-06 [1] CRAN (R 4.2.0)
## cli 3.3.0 2022-04-25 [1] CRAN (R 4.2.0)
## digest 0.6.29 2021-12-01 [1] CRAN (R 4.2.0)
## evaluate 0.15 2022-02-18 [1] CRAN (R 4.2.0)
## fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0)
## htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.2.0)
## jquerylib 0.1.4 2021-04-26 [1] CRAN (R 4.2.0)
## jsonlite 1.8.0 2022-02-22 [1] CRAN (R 4.2.0)
## knitr 1.39 2022-04-26 [1] CRAN (R 4.2.0)
## magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0)
## R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0)
## rlang 1.0.2 2022-03-04 [1] CRAN (R 4.2.0)
## rmarkdown 2.14 2022-04-25 [1] CRAN (R 4.2.0)
## rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.2.0)
## sass 0.4.1 2022-03-23 [1] CRAN (R 4.2.0)
## sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0)
## stringi 1.7.6 2021-11-29 [1] CRAN (R 4.2.0)
## stringr 1.4.0 2019-02-10 [1] CRAN (R 4.2.0)
## xfun 0.30 2022-03-02 [1] CRAN (R 4.2.0)
## yaml 2.3.5 2022-02-21 [1] CRAN (R 4.2.0)
##
## [1] /Library/Frameworks/R.framework/Versions/4.2/Resources/library
##
## ──────────────────────────────────────────────────────────────────────────────
If you’re interested in the source, it’s on GitHub.↩︎
Basically, you couldn’t pipe into an anonymous function like this:
mtcars |> \(x) lm(hp ~ cyl, data = x)
. You had to use ‘dog balls’ or ‘eyes’,()()
, like this:mtcars |> (\(x) lm(hp ~ cyl, data = x))()
. Not pleasing. As of R v4.2, you can use the placeholder to do this:mtcars |> lm(hp ~ cyl, data = _)
. However, you will still need the balls if you want to use the placeholder more than once on the right-hand side (or you could use Brenton’s {pipebind} package).↩︎