This package proposes an enhanced assignment using the shorthand ..
.
It’s meant to :
library(dotdot)
x <- y <- iris
x$Sepal.Length[5] <- x$Sepal.Length[5] + 3
y$Sepal.Length[5] := .. + 3
identical(x,y)
#> [1] TRUE
z <- factor(letters[1:3])
levels(z) := c(.., "level4")
z
#> [1] a b c
#> Levels: a b c level4
It can help to think about the ..
as the :
of the :=
symbol laid horizontally.
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.
%<>%
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.