Repository navigation
Proposal: encourage right‑hand side assignment (->) at the end of pipe chains for better readability #2142
chuxinyuan
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Problem or observation
Currently most R examples and style guides (including the tidyverse style guide) recommend assigning the result of a pipe chain using the left‑hand side assignment
<-placed at the beginning:However, the natural reading order of a pipe is from left to right, top to bottom. Using
->at the end of the chain makes the data flow visually continuous:In the second form, the eye follows the data step by step, and the final destination is reached exactly at the end of the line. This feels more intuitive, especially for longer pipelines.
Suggestion
Consider officially recognising and documenting
->as a valid and even encouraged style for pipe‑chain assignments. At minimum, the style guide could mention both patterns and explain the trade‑offs, without dogmatically preferring<-in all cases.Additional benefits
Works seamlessly with the native R pipe (
|>) and%>%.Avoids “looking back” to the beginning of the pipeline to see where the result goes.
Particularly useful when the pipeline spans several lines – the assignment becomes the final statement.
Potential counter‑arguments and responses
Priority issues:
->has low precedence, but at the top level of a pipeline this rarely causes problems. Wrapping the whole pipeline in { if needed is a simple safeguard.R tradition: Style evolves. The pipe itself was once unconventional; now it’s central. R‑ish ergonomics can adapt.
Would the community be open to discussing this more formally? Thank you.
All reactions