geocompx / geocompr

Geocomputation with R: an open source book
https://r.geocompx.org/
Other
1.52k stars 580 forks source link

Use of = instead of <- confusing #319

Closed EconGeo closed 5 years ago

EconGeo commented 5 years ago

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 commented 5 years ago

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!): https://github.com/csgillespie/efficientR/issues/34

tim-salabim commented 5 years ago

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

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 commented 5 years ago

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 commented 5 years ago

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.

yihui commented 5 years ago

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