View source: R/unnecessary_lambda_linter.R
unnecessary_lambda_linter | R Documentation |
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.
unnecessary_lambda_linter(allow_comparison = FALSE)
allow_comparison |
Logical, default |
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.
best_practices, configurable, efficiency, readability
linters for a complete list of linters available in lintr.
# will produce lints
lint(
text = "lapply(list(1:3, 2:4), function(xi) sum(xi))",
linters = unnecessary_lambda_linter()
)
lint(
text = "sapply(x, function(xi) xi == 2)",
linters = unnecessary_lambda_linter()
)
lint(
text = "sapply(x, function(xi) sum(xi) > 0)",
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()
)
lint(
text = "sapply(x, function(xi) xi == 2)",
linters = unnecessary_lambda_linter(allow_comparison = TRUE)
)
lint(
text = "sapply(x, function(xi) sum(xi) > 0)",
linters = unnecessary_lambda_linter(allow_comparison = TRUE)
)
lint(
text = "sapply(x, function(xi) sum(abs(xi)) > 10)",
linters = unnecessary_lambda_linter()
)
lint(
text = "sapply(x, sum) > 0",
linters = unnecessary_lambda_linter()
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.