Try R v4.2 in your browser

binder
holepunch
pipe
pipebind
r
Author
Published

June 1, 2022

Screenshot of RStudio in a browser window. The script pane shows some use of the base pipe syntax. The console shows the R startup message, including that it is version 4.2.

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.

A button that has the Binder logo on it and says 'launch binder'.

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:

  1. 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.
  2. 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:

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!

Environment

Session info
Last rendered: 2023-07-21 18:39:34 BST
R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.2.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/London
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] htmlwidgets_1.6.2 compiler_4.3.1    fastmap_1.1.1     cli_3.6.1        
 [5] tools_4.3.1       htmltools_0.5.5   rstudioapi_0.15.0 yaml_2.3.7       
 [9] rmarkdown_2.23    knitr_1.43.1      jsonlite_1.8.7    xfun_0.39        
[13] digest_0.6.33     rlang_1.1.1       evaluate_0.21    

Footnotes

  1. If you’re interested in the source, it’s on GitHub.↩︎

  2. 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).↩︎

Reuse

CC BY-NC-SA 4.0