pipe_consistency

NoteAdded in 0.6.0

What it does

Reports cases where both pipes (%>% or |>) are used. By default, the base pipe |> is preferred but this can be changed in the configuration file, see details of the configuration options below.

Why is this bad?

This simply ensures that pipe usage is consistent. There are a few cases where both pipes are not equivalent, and therefore where this rule doesn’t report diagnostics:

  • if the RHS of a %>% uses . as an unnamed argument, then it is not reported because there is no equivalent in base R (the _ placeholder only works for named arguments).

  • if the RHS of a %>% uses . several times, then it is not reported because there is no equivalent in base R (the _ placeholder can only be used once in the RHS).

This rule is available only for R >= 4.2 (the _ placeholder was introduced in 4.2, even though |> itself was introduced in 4.1), and it has an unsafe fix due to some specificities of the native pipe (e.g. it doesn’t work when +() is on the RHS).

Configuration options

The operation of the pipe_consistency rule can be customised in the configuration file.

The pipe option takes a single value ("|>" or "%>%") indicating the preferred pipe operator in the files to check. If pipe = "|>" and if the "pipe_consistency" rule is enabled, then any use of %>% will be reported, and vice-versa.

Default values

pipe = "|>"

TOML settings

[lint]
...

[lint.pipe_consistency]
# Use the {magrittr} pipe instead
pipe = "%>%"

Example

data %>%
  transform(a = x / 2) |>
  plot()

Use instead:

data |>
  transform(a = x / 2) |>
  plot()

References

See ?pipeOp