This linter checks for 1:length(...)
, 1:nrow(...)
, 1:ncol(...)
,
1:NROW(...)
and 1:NCOL(...)
expressions in base-R, or their usage in
conjunction with seq()
(e.g., seq(length(...))
, seq(nrow(...))
, etc.).
Details
Additionally, it checks for 1:n()
(from {dplyr}
) and 1:.N
(from {data.table}
).
These often cause bugs when the right-hand side is zero.
Instead, it is safer to use base::seq_len()
(to create a sequence of a specified length) or
base::seq_along()
(to create a sequence along an object).
See also
linters for a complete list of linters available in lintr.
Tags
best_practices, consistency, default, efficiency, robustness
Examples
# will produce lints
lint(
text = "seq(length(x))",
linters = seq_linter()
)
#> <text>:1:1: warning: [seq_linter] Use seq_along(...) instead of seq(length(...)), which is likely to be wrong in the empty edge case.
#> seq(length(x))
#> ^~~~~~~~~~~~~~
lint(
text = "1:nrow(x)",
linters = seq_linter()
)
#> <text>:1:1: warning: [seq_linter] Use seq_len(nrow(...)) instead of 1:nrow(...), which is likely to be wrong in the empty edge case.
#> 1:nrow(x)
#> ^~~~~~~~~
lint(
text = "dplyr::mutate(x, .id = 1:n())",
linters = seq_linter()
)
#> <text>:1:24: warning: [seq_linter] Use seq_len(n()) instead of 1:n(), which is likely to be wrong in the empty edge case.
#> dplyr::mutate(x, .id = 1:n())
#> ^~~~~
# okay
lint(
text = "seq_along(x)",
linters = seq_linter()
)
#> ℹ No lints found.
lint(
text = "seq_len(nrow(x))",
linters = seq_linter()
)
#> ℹ No lints found.
lint(
text = "dplyr::mutate(x, .id = seq_len(n()))",
linters = seq_linter()
)
#> ℹ No lints found.