Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of = instead of <- confusing #319

Closed
EconGeo opened this issue Oct 5, 2018 · 5 comments
Closed

Use of = instead of <- confusing #319

EconGeo opened this issue Oct 5, 2018 · 5 comments

Comments

@EconGeo
Copy link

EconGeo commented Oct 5, 2018

Love the book. However, throughout the book you do not use the common assignment convention of <- and instead use an = as in:
world1 = dplyr::select(world, name_long, pop)
instead of
world1 <- dplyr::select(world, name_long, pop)
It throws me off as an advanced R user, and I think beginners reading the book will definitely be thrown off by this considering nearly every tutorial available on learning R follows the convention.

@Robinlovelace
Copy link
Collaborator

Robinlovelace commented Oct 5, 2018

Hi @EconGeo thanks for the question. Use of assignment symbol is a question of style and has no impact on results unless you do something like this (solution also shown in 3rd command):

system.time(x = 1e9)
#> Error in system.time(x = 1e+09): unused argument (x = 1e+09)
system.time(x <- 1e9)
#>    user  system elapsed 
#>       0       0       0
system.time({x = 1e9})
#>    user  system elapsed 
#>       0       0       0

Created on 2018-10-05 by the reprex package (v0.2.1)

For the reasoning behind our decision to use = instead of <- please see the note just preceding this section in the book: https://geocompr.robinlovelace.net/spatial-class.html#why-simple-features

We know it's not everyone's cup of tea and would be up for changing it using the styler package. However it's how we (@Nowosad, @jannes-m and I) have ended-up writing and teaching R code after a few years and many teaching sessions of collective experience.

There are some 'advanced' R materials that use = instead of <- such as the book Efficient R Programming and most packages/tutorials by @yihui, e.g. the blogdown book: https://bookdown.org/yihui/blogdown/r-markdown.html

We discussed changing it, and even considered creating a poll but, like the Brexit referendum, we cannot be sure we'll get the result we prefer ;) Furthermore we favour focusing on style over substance substance over style so don't want to inflame the debate. But very open to ideas and if there are sufficient reasons, we can switch! Interested to hear about the + and - impacts of this so if you or any others have further comments, here's a good place to put them. You never know, we may switch (back) to using arrow assignment.

P.s. here's an old discussion of a similar issue (there's lots more out there!): csgillespie/efficientR#34

@tim-salabim
Copy link
Contributor

As someone who prefers = out of shear lazyness and habit, I've

  • never come accross any issues that would not happen when using <-
  • never understood the argument that beginners might be thrown off as for both types of beginners (coming from other languages or new to coding) I cannot see any negative impacts. For beginners new to coding it emphasises that there are many accepted ways of doing things in R, which is also true for many other aspects of the language e.g. piping or classical style. For new to R beginners coming from other languages adaption will be easier this way.

Furthermore, I consider it very bad parctice to do something along the lines of

mean(myobject <- 1:10)

which is sometimes used as an argument for <- assigment. Those are clearly two statements that should be treated as such, especially when arguing for <- in the sense of readability. On the contrary, I find it more than helpful that this throws an error when using =.

In summary, I firmly believe that this choice really boils down to personal preference just the same as using piping (via e.g. dplyr), base style coding such as [ or data.table notation.

@yihui
Copy link
Contributor

yihui commented Oct 5, 2018

Yes, I'm a firm believer in =, and use it all the time in all my own projects. When I collaborate with other people who prefer <-, I just use <- to make them comfortable.

I don't think it is very wise to be "thrown off" just by something related to personal preference. Besides, <- is a too powerful assignment operator in my opinion since it works anywhere---so powerful that it can be surprising and even dangerous: fun(foo <- value) is one example, and I have also seen R code like df$x<1; df$x<0; df$x<-1 several times (when they meant df$x < -1), which means "white spaces in code don't matter until they do", and this is very hard to debug. I agree with @tim-salabim that other language users will actually find = more natural, because none of other languages (to my knowledge) use <- for assignment and they all use =.

Usually I'm fine with multiple ways to do one thing, but in R, the two assignment operators (there are more) have been bothering me. The critical issue is that the two are not equivalent, so it is not purely a matter of personal taste. To sum it up, reasons why I don't use <-:

  1. It requires more typing effort, despite of keyboard shortcuts in editors;

  2. It requires special attention when I switch from another language to R (Hey, you are in R now, use <-! Hey, you are in JavaScript, don't use <-!);

  3. It is too powerful and works anywhere, including where I don't expect assignment to happen.

I never push <- users to switch to = when I work with them, and I wish they don't push back, either 😉 Let's just appreciate the diversity of assignment operators in R (after understanding their pros and cons), even though this particular diversity is not a good thing in the first place.

@EconGeo
Copy link
Author

EconGeo commented Oct 5, 2018

Fair enough. I understand your point of view, and didn't realize this was such a well established view in the community. Great book and thanks for publishing, wish it and the sf class was around when I started down my spatial econometrics path of research.

@EconGeo EconGeo closed this as completed Oct 5, 2018
@yihui
Copy link
Contributor

yihui commented Oct 6, 2018

Well, not really a well established view. People who use = are definitely the minority (not all assignment operators are created equal...). Thanks for understanding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants