Skip to contents

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.).

Usage

seq_linter()

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. It is safer to use base::seq_len() or base::seq_along() instead.

See also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "seq(length(x))",
  linters = seq_linter()
)
#> ::warning file=<text>,line=1,col=1::file=<text>,line=1,col=1,[seq_linter] seq(length(...)) is likely to be wrong in the empty edge case. Use seq_along(...) instead.

lint(
  text = "1:nrow(x)",
  linters = seq_linter()
)
#> ::warning file=<text>,line=1,col=1::file=<text>,line=1,col=1,[seq_linter] 1:nrow(...) is likely to be wrong in the empty edge case. Use seq_len(nrow(...)) instead.

lint(
  text = "dplyr::mutate(x, .id = 1:n())",
  linters = seq_linter()
)
#> ::warning file=<text>,line=1,col=24::file=<text>,line=1,col=24,[seq_linter] 1:n() is likely to be wrong in the empty edge case. Use seq_len(n()) instead.

# okay
lint(
  text = "seq_along(x)",
  linters = seq_linter()
)

lint(
  text = "seq_len(nrow(x))",
  linters = seq_linter()
)

lint(
  text = "dplyr::mutate(x, .id = seq_len(n()))",
  linters = seq_linter()
)