consecutive_mutate_linter: Require consecutive calls to mutate() to be combined when...

View source: R/consecutive_mutate_linter.R

consecutive_mutate_linterR Documentation

Require consecutive calls to mutate() to be combined when possible

Description

dplyr::mutate() accepts any number of columns, so sequences like DF %>% dplyr::mutate(..1) %>% dplyr::mutate(..2) are redundant – they can always be expressed with a single call to dplyr::mutate().

Usage

consecutive_mutate_linter(invalid_backends = "dbplyr")

Arguments

invalid_backends

Character vector of packages providing dplyr backends which may not be compatible with combining mutate() calls in all cases. Defaults to "dbplyr" since not all SQL backends can handle re-using a variable defined in the same mutate() expression.

Details

An exception is for some SQL back-ends, where the translation logic may not be as sophisticated as that in the default dplyr, for example in DF %>% mutate(a = a + 1) %>% mutate(b = a - 2).

Tags

configurable, consistency, efficiency, readability

See Also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "x %>% mutate(a = 1) %>% mutate(b = 2)",
  linters = consecutive_mutate_linter()
)

# okay
lint(
  text = "x %>% mutate(a = 1, b = 2)",
  linters = consecutive_mutate_linter()
)

code <- "library(dbplyr)\nx %>% mutate(a = 1) %>% mutate(a = a + 1)"
writeLines(code)
lint(
  text = code,
  linters = consecutive_mutate_linter()
)


r-lib/lintr documentation built on Dec. 20, 2024, 7:24 p.m.