extraction_operator_linter {lintr}R Documentation

Extraction operator linter

Description

Check that the [[ operator is used when extracting a single element from an object, not [ (subsetting) nor $ (interactive use).

Usage

extraction_operator_linter()

Details

There are three subsetting operators in R ([[, [, and $) and they interact differently with different data structures (atomic vector, list, data frame, etc.).

Here are a few reasons to prefer the [[ operator over [ or $ when you want to extract an element from a data frame or a list:

For data frames (and tibbles), irrespective of the size, the [[ operator is slower than $. For lists, however, the reverse is true.

Tags

best_practices, style

References

See Also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = 'iris["Species"]',
  linters = extraction_operator_linter()
)

lint(
  text = "iris$Species",
  linters = extraction_operator_linter()
)

# okay
lint(
  text = 'iris[["Species"]]',
  linters = extraction_operator_linter()
)


[Package lintr version 3.1.2 Index]