Learn R Programming

lintr (version 3.1.2)

unnecessary_lambda_linter: Block usage of anonymous functions in iteration functions when unnecessary

Description

Using an anonymous function in, e.g., lapply() is not always necessary, e.g. lapply(DF, sum) is the same as lapply(DF, function(x) sum(x)) and the former is more readable.

Usage

unnecessary_lambda_linter()

Arguments

Tags

best_practices, efficiency, readability

Details

Cases like lapply(x, \(xi) grep("ptn", xi)) are excluded because, though the anonymous function can be avoided, doing so is not always more readable.

See Also

linters for a complete list of linters available in lintr.

Examples

Run this code
# will produce lints
lint(
  text = "lapply(list(1:3, 2:4), function(xi) sum(xi))",
  linters = unnecessary_lambda_linter()
)

# okay
lint(
  text = "lapply(list(1:3, 2:4), sum)",
  linters = unnecessary_lambda_linter()
)

lint(
  text = 'lapply(x, function(xi) grep("ptn", xi))',
  linters = unnecessary_lambda_linter()
)

lint(
  text = "lapply(x, function(xi) data.frame(col = xi))",
  linters = unnecessary_lambda_linter()
)

Run the code above in your browser using DataLab