https://github.com/moodymudskipper/dotdot
Enhanced assignments. Use `..` on the right hand side as a shorthand for the left hand side.
https://github.com/moodymudskipper/dotdot
Last synced: 3 days ago
JSON representation
Enhanced assignments. Use `..` on the right hand side as a shorthand for the left hand side.
- Host: GitHub
- URL: https://github.com/moodymudskipper/dotdot
- Owner: moodymudskipper
- License: gpl-3.0
- Created: 2018-11-08T00:12:15.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-03-23T23:00:34.000Z (about 6 years ago)
- Last Synced: 2025-04-12T20:52:15.121Z (3 days ago)
- Language: R
- Size: 32.2 KB
- Stars: 17
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE.md
Awesome Lists containing this project
- jimsghstars - moodymudskipper/dotdot - Enhanced assignments. Use `..` on the right hand side as a shorthand for the left hand side. (R)
README
---
output:
md_document:
variant: markdown_github
---```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```# dotdot
Installation :
```{r, eval = FALSE}
devtools::install_github("moodymudskipper/dotdot")
```This package proposes an enhanced assignment using the shorthand `..`.
It's meant to :
* Be explicit about growing an object or overwriting it using its previous value
* Avoid repeating a variable name
* Save keystrokes/time/visual space/cognitive load```{r}
library(dotdot)
x <- y <- iris
x$Sepal.Length[5] <- x$Sepal.Length[5] + 3
y$Sepal.Length[5] := .. + 3
identical(x,y)z <- factor(letters[1:3])
levels(z) := c(.., "level4")
z
```It can help to think about the `..` as the `:` of the `:=` symbol laid horizontally.
## Integration with *`data.table`*, *`tidyverse`* and other packages using `:=`
The operator `:=` is used by prominent packages *`data.table`* and *`rlang`*
(mostly through *`tidyverse`* functions), but they only use it to parse expressions,
due to its convenient operator precedence. It's not actually called.
Thus *`dotdot`* is completely *`tidyverse`* and *`data.table`* compatible, and
some adhoc adjustments were made so it works when the latter are attached
after *`dotdot`*.In the rare case in which the user would attach another package containing `:=`,
`dotdot_first()` will move back *`dotdot`* at the first position of the search
path.## Comparison with magrittr's `%<>%`
The package *`magrittr`* contains the operator `%<>%` which share important
similarities with `:=`.The calls `iris$Sepal.Length %<>% log()` and `iris$Sepal.Length := log(..)`
indeed have the same effect.The operator precedence is different however, which makes *`dotdot`* arguably
better suited outside of pipe chains, for instance
`iris$Sepal.Length[5] := 2*.. + 3` would have to be written
`iris$Sepal.Length[5] %<>% multiply_by(2) %>% add(3)` or
`iris$Sepal.Length[5] %<>% {2*. + 3}` using *`magrittr`*.Furthermore *`magrittr`* is more complex in the way it deals with environments
while *`dotdot`* makes straightforward substitutions, so `x := substitute(..)`
works and doesn't have a *`magrittr`* equivalent.