lintr icon indicating copy to clipboard operation
lintr copied to clipboard

Soften `pipe_consistency_linter`

Open maciekbanas opened this issue 1 month ago • 3 comments

Please consider passing pipe_consistency_linter in cases where magrittr pipe handles passing object as a non-first argument in a more readable way:

private$hosts <- new_host |>
          private$check_for_duplicate_hosts() %>%
          append(private$hosts, .)

Using magrittr as an exception looks neater than the approach below:

private$hosts <- new_host |>
          private$check_for_duplicate_hosts() |>
          (\(x) append(private$hosts, x))()

maciekbanas avatar Dec 05 '25 08:12 maciekbanas

I agree that this is a viable case to deviate from the default recommendation. I suppose a # nolint: pipe_consistency_linter in those lines is acceptable, if a rare occurrence.

HaHeho avatar Dec 05 '25 15:12 HaHeho

You should use the native pipe placeholder (that's the 4.3.0 reference in the style guide: https://style.tidyverse.org/pipes.html#magrittr):

private$hosts <- new_host |>
  private$check_for_duplicate_hosts() |>
  append(private$hosts, values = _)

Note that the lambdas are specifically discouraged in pipelines, too: https://style.tidyverse.org/functions.html#anonymous-functions

Avoid using \() in a pipe

MichaelChirico avatar Dec 05 '25 15:12 MichaelChirico

There are some residual cases where {magrittr} %>% works that |> won't. That's what's covered by the Style guide here though:

https://style.tidyverse.org/pipes.html#magrittr

As of R 4.3.0, the base pipe provides all the features from magrittr that we recommend using.

(emph. mine)

If |> doesn't work, the intention is to reconsider how the code is written.

Possibly the linter message could be customized to look for ., though things like . in dplyr::do() (itself superseded) will interfere with its accuracy.

I'll let this sit for a while to gather feedback before investing in that.

MichaelChirico avatar Dec 05 '25 19:12 MichaelChirico