Using a consistent coding style helps make your code more legible and therefore makes it easier for yourself and others to maintain the source code. You can use the {lintr} package [[1]] to help you adhere to a particular coding style.
I was surprised to discover that Google's current style guide for R says that they do not support using the backwards assignment -> (or ->> for global assignments), or right-hand assignment as they call it [[2]]. In other words, the people at Google do not recommend using backwards assignments. I firmly disagree. The Tidyverse style guide (which is based on Google's old style guide for R) mentions that backward assignments can be quite intuitive when using pipes [[3]]. The example given is
iris |>
gather(measure, value, -Species) |>
arrange(-value) ->
iris_long
I don't know about you, but I find the notation above more intuitive than
iris_long <- iris |>
gather(measure, value, -Species) |>
arrange(-value)
Personally, I also use backward assignment after a big statement like
do.call(rbind, lapply(foobar, function(x) {
# lot's of code here ...
})) -> result
Why does R even have the reverse versions of <- and <<-? John Chambers, the creator of the S language, says the following [[4]]:
The parser accepts mirror-image versions -> and ->>, but just translates them to point left. These are historic leftovers from the days before inline editing.
So there's no deep philosophical reasoning behind the backward assignment operator. If you use the {lintr} package and you want to allow the use of the backward assignment operators, use assignment_linter(allow_right_assign = TRUE) to allow right assignment.
— M
Footnotes
[1] | https://lintr.r-lib.org/ |
[2] | https://google.github.io/styleguide/Rguide.html#right-hand-assignment |
[3] | https://style.tidyverse.org/pipes.html#assignment |
[4] | Software for Data Analysis by John M. Chambers, Springer, 2008 (ISBN: 978-0-387-75935-7). |