Let’s not put too fine a point on it: the R documentation is (subtly) wrong [1]. This is easy to show: we just need to find a counter-example of the = operator that isn’t (a) at the top level, nor (b) a subexpression in a braced list of expressions (i.e. {…; …}). — Without further ado:
It’s because in R’s syntax the symbol = has two distinct meanings that get routinely conflated (even by experts, including in the documentation cited above):
- The first meaning is as an assignment operatorassignment operator. This is all we’ve talked about so far.
- The second meaning isn’t an operator but rather a syntax tokensyntax token that signals named argument passing in a function call. Unlike the
= operatoroperator it performs no action at runtime, it merely changes the way an expression is parsed.
So how does R decide whether a given usage of = refers to the operator or to named argument passing? Let’s see.
A confession: I lied earlier. There is one additional difference betweenIn sum, by default the operators =<- and <-= operators: they call distinct functions. By default these functions do the same thing but you can override. But either of them can be overridden separately to change theits behaviour. By contrast, <- and -> (left-to-right assignment), though syntactically distinct, always call the same function. Overriding one also overrides the other. Knowing this is rarely practical but it can be used for some fun shenanigans.