Why are there two assignment operators, `<-` and `->` in R?

  • A+
Category:Languages

I know how to use <- and ->, and there are good writeups on the difference between equals assignment & arrow assignment, but I don't know when to prefer -> over <-.

It seems the community has coalesced around using <- for assignment.

Neither the google R style-guide, nor Hadley Wickam's tidyverse R style-guide even mention -> in the assignment section.

I'm curious about the design considerations that led the S/S-PLUS developers to put in the right arrow assign operator ->. In what setting(s) would using -> be considered more readable (or easier to type) versus <- or =?

I'm not familiar with any other language that allows the right-assignment semantics. What languages inspired R in this regard?

 


From the answer to an exercise in The New S Language (Becker, Chamber and Wilks 1988), via Google Books:

When you type a long expression only to remember at the end that it would be a good idea to save the result, a right-hand arrow allows you to perform an assignment without retyping the line.

This suggests that S users were working directly in the console, without line-editing capabilities that are available in most modern REPL/interactive environments ...


Some archaeology: I poked around in foundational sources on Google Books. There are three relevant books:

  • the Brown Book: S: An Interactive Environment for Data Analysis and Graphics R. A. Becker, J. M. Chambers (CRC Press, 1984)
  • Extending the S System, Becker and Chambers (Taylor & Francis, 1985)
  • the Blue Book: The New S Language Becker, Chamber and Wilks (Wadsworth and Brooks/Cole 1988, but reissued in 2018!! by CRC Press)

    1. The Brown Book doesn't mention -> in the main text:

Why are there two assignment operators, `<-` and `->` in R?

but it does in the appendix:

Why are there two assignment operators, `<-` and `->` in R?

  1. Extending the S System doesn't mention -> in the main text:

Why are there two assignment operators, `<-` and `->` in R?

but I can't search very much of the book, so -> might be mentioned in the appendix somewhere.

  1. The Blue Book refers to the right-arrow, but actually seems to have a typo:

Why are there two assignment operators, `<-` and `->` in R?

I interpret the underlined red passages as supporting that there there's a typo in the first underlined line, which should be -> rather than ← ...

Here's the screenshot of the exercise answer referred to above:

Why are there two assignment operators, `<-` and `->` in R?

If you want a copy of the 1985 book you can get it for $34.41 - or $1070.99 (but with free shipping!) ...

Why are there two assignment operators, `<-` and `->` in R?

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: