scalar_in_linter: Block usage like x %in% "a"

View source: R/scalar_in_linter.R

scalar_in_linterR Documentation

Block usage like x %in% "a"

Description

vector %in% set is appropriate for matching a vector to a set, but if that set has size 1, == is more appropriate.

Usage

scalar_in_linter(in_operators = NULL)

Arguments

in_operators

Character vector of additional infix operators that behave like the %in% operator, e.g. {data.table}'s ⁠%chin%⁠ operator.

Details

scalar %in% vector is OK, because the alternative (any(vector == scalar)) is more circuitous & potentially less clear.

Tags

best_practices, configurable, consistency, efficiency, readability

See Also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "x %in% 1L",
  linters = scalar_in_linter()
)

lint(
  text = "x %chin% 'a'",
  linters = scalar_in_linter(in_operators = "%chin%")
)

# okay
lint(
  text = "x %in% 1:10",
  linters = scalar_in_linter()
)


r-lib/lintr documentation built on Sept. 15, 2024, 4:41 a.m.