R allows for assignment via <-
and =
.
Whereas there a subtle differences between both assignment operators, there seems to be a broad consensus that <-
is the better choice than =
, as =
is also used as operator mapping values to arguments and thus its use may lead to ambiguous statements. The following exemplifies this:
> system.time(x <- rnorm(10))
user system elapsed
0 0 0
> system.time(x = rnorm(10))
Error in system.time(x = rnorm(10)) : unused argument(s) (x = rnorm(10))
In fact, the Google style code disallows using =
for assignment (see comments to this answer for a converse view).
I also almost exclusively use <-
as assignment operator. However, the almost in the previous sentence is the reason for this question. When =
acts as assignment operator in my code it is always accidental and if it leads to problems these are usually hard to spot.
I would like to know if there is a way to turn off assignment via =
and let R throw an error any time =
is used for assignment.
Optimally this behavior would only occur for code in the Global Environment, as there may well be code in attached namespaces that uses =
for assignment and should not break.
(This question was inspired by a discussion with Jonathan Nelson)