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

RHS assignment persistent line breaks introduce extra indentation in pipes #220

Closed
gadenbuie opened this issue Feb 12, 2025 · 3 comments · Fixed by #227
Closed

RHS assignment persistent line breaks introduce extra indentation in pipes #220

gadenbuie opened this issue Feb 12, 2025 · 3 comments · Fixed by #227

Comments

@gadenbuie
Copy link
Contributor

❯ cat test.R
data <-
  starwars |>
  filter(height > 172) |>
  select(1:3)

❯ air format test.R
❯ cat test.R       
data <-
  starwars |>
    filter(height > 172) |>
    select(1:3)

❯ air --version    
air 0.3.0

As a fan of the persistent line break in this case, I really want all of the expressions in the pipeline to be indented to the same level:

# 😊
data <-
  starwars |>
  filter(height > 172) |>
  select(1:3)

# 😥
data <-
  starwars |>
    filter(height > 172) |>
    select(1:3)
@DavisVaughan
Copy link
Collaborator

DavisVaughan commented Feb 12, 2025

I feel like that kind of goes against the spirit of the pipe chain though, right? i.e. pipe chains work like:

  • the starwars line starts the pipe chain
  • the next line of the chain, and all lines following it, are indented out by one level

I am imagining that in your head data itself starts the "chain", with <- being the first operator in the chain, so starwars |> is really the "next" line of the chain, so it gets indented and then every line after it is indented at the same level as that. But we definitely don't work that way right now, and it might be hard to do, especially since some people (me, at least) prefer the current indentation.

@juliasilge
Copy link
Contributor

I know this is definitely an opinion, but I am a big fan of the kind of indentation @gadenbuie shows here. You can see how I use this in my blog posts such as:

This would probably be enough to have me not use air honestly. 😬

@DavisVaughan DavisVaughan changed the title Persistent line breaks introduce extra indentation in pipes RHS assignment persistent line breaks introduce extra indentation in pipes Feb 12, 2025
@DavisVaughan
Copy link
Collaborator

I have looked at some additional examples
https://github.com/topepo/recipe_selection_sim/blob/main/template.R
https://github.com/tidymodels/workshops/blob/70e789dc1b7ad708dfd768f3a19974fae8721f90/slides/advanced-04-racing.qmd#L71

and feel that you are both probably right here. This is a particularly unique example ("assignment with rhs starting on its own line leading into a pipe chain"), so it will have to be tightly scoped and special cased, because we still want normal pipe indentation pretty much everywhere else. But it is worth trying to fix up.

It is also in the style guide and has been this way for many years
https://style.tidyverse.org/pipes.html#assignment

If we document this behavior I think we should justify it in the way I mentioned earlier. In these examples, in your head you aren't starting the pipe chain at starwars or recipe(), you're starting it at data <- and hotel_rec <-, so you indent the line after those and then all future lines in the chain keep the same level of indentation.

data <-
  starwars |>
  filter(height > 172) |>
  select(1:3)

hotel_rec <-
  recipe(avg_price_per_room ~ ., data = hotel_train) %>%
  step_YeoJohnson(lead_time) %>%
  step_dummy_hash(agent,   num_terms = tune("agent hash")) %>%
  step_dummy_hash(company, num_terms = tune("company hash")) %>%
  step_zv(all_predictors())

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

Successfully merging a pull request may close this issue.

3 participants