tl;dr
All <-
vs =
flamewars are nullified forever with the introduction of my new ‘down assign’ operator for the R language:
|
v
Get down
I no longer set my calendar by the movement of the Earth around the hottest point in the solar system. I now set it by the recurrent emergence of the hottest take in the solar system: that R’s assignment operator <-
is garbage and R users should be ashamed of themselves.1
Last time I spoke about this I made clear that our Strong Pointy Lad was the One True Operator for assignment. In that post, like some kind of modern Prometheus, I gave mortals the power to detect and destroy R scripts containing the weak and mundane equals assignment operator.
But with deference, I have come to realise something profound: we shouldn’t fight about this. It’s not worth anyone’s time to debate the relative merits of using <-
or =
. We should all relax. We can live in harmony.
…Because I’ve invented a new assignment concept. Folks, say hello to the down assign operator.
|
v
Yes, it’s still an ‘arrow’, but I think everyone will agree that it makes sense this time. Lateral assignment is unnatural and inefficient and is out of the natural order of things.
Now the the value falls effortlessly down your script, under the weight of gravity, into the name of the object. If it’s good enough Sir Isaac Newton, it’s good enough for me. Or don’t you believe in gravity? Exactly.
So x <- 1
(or, shudder, x = 1
) translates to:
1
|
v
x
Elegant, isn’t it?
While R Core perform the trivial task of cementing this feature into base R, I’ve prepared a small function that will take care of rudimentary usage for now.
Note that this function won’t work in an interactive session; it takes the filepath to a script as its input. But that’s okay: I think <-
haters are often computer-scientist types and 1337 h4x0rz who never sully their code by playing around in filthy IDEs and notebooks anyway. Obviously we should emulate them.
Update
Folks, nerdsniping works:
- Matthew Kay has now written the {explodeAssign} package, which lets you use special (weaponised) down-assign arrows in an interactive session
- Antoine Fabri has followed up with a coded implementation for making this possible with a just a
v
operator (‘vassign’)
As Matthew says, this is a ‘terrible idea’ and you should use at your own risk, lol.
First I’ll write a demo script to a temporary file. It assigns the values of 1
and 2
to x
and y
, respectively, then adds them together.
demo_script <- "
1
|
v
x
2
|
v
y
x + y
"
demo_file <- tempfile(fileext = ".R")
writeLines(demo_script, demo_file)
Now to define the function. It reads our script file, finds the down arrows, substitutes them, returns them back to the expression from whence they came, then executes the script.
point_down <- function(file) {
content <- readLines(file)
for (i in seq(content)) {
if (content[i] == "|" & content[i + 1] == "v") {
combos <- paste0(
content[i - 1], content[i], content[i + 1], content[i + 2]
)
rm_index <- c(i - 1, i, i + 1, i + 2)
content[rm_index[1]] <- combos
content[rm_index[2:4]] <- ""
content <- gsub("\\|v", "->", content)
}
}
path <- tempfile(fileext = ".R")
writeLines(content, path)
eval(parse(path))
}
And now we execute.
point_down(demo_file)
## [1] 3
Simply: wow.
Down and away
My next step for unifying the community around R operators is the ‘down pipe’:
_
v
Some use the term ‘down pipe’ to mean the exterior drainage tube that takes wastewater away from their homes. I hope we can use the down pipe operator as a way of siphoning away all the bilge around the %>%
vs |>
arguments once and for all. You’re welcome.
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)
## fontawesome 0.2.2 2021-07-02 [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
##
## ──────────────────────────────────────────────────────────────────────────────
The latest, at time of writing.↩︎