Require consecutive calls to mutate() to be combined when possible
Source:R/consecutive_mutate_linter.R
consecutive_mutate_linter.Rd
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()
.
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)
.
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()
)
#> <text>:1:25: warning: [consecutive_mutate_linter] Unify consecutive calls to mutate().
#> x %>% mutate(a = 1) %>% mutate(b = 2)
#> ^~~~~~~~~~~~~
# okay
lint(
text = "x %>% mutate(a = 1, b = 2)",
linters = consecutive_mutate_linter()
)
#> ℹ No lints found.
code <- "library(dbplyr)\nx %>% mutate(a = 1) %>% mutate(a = a + 1)"
writeLines(code)
#> library(dbplyr)
#> x %>% mutate(a = 1) %>% mutate(a = a + 1)
lint(
text = code,
linters = consecutive_mutate_linter()
)
#> ℹ No lints found.