base::anyNA() exists as a replacement for any(is.na(x)) which is more efficient
for simple objects, and is at worst equally efficient.
Therefore, it should be used in all situations instead of the latter.
Details
This also lints NA %in% x, which is also more readably expressed as anyNA(x). In
this case, note a subtle difference of behavior, namely that is.na(NaN) is TRUE
while NA %in% NaN is FALSE.
See also
linters for a complete list of linters available in lintr.
Examples
# will produce lints
lint(
text = "any(is.na(x), na.rm = TRUE)",
linters = any_is_na_linter()
)
#> <text>:1:1: warning: [any_is_na_linter] anyNA(x) is better than any(is.na(x)).
#> any(is.na(x), na.rm = TRUE)
#> ^~~~~~~~~~~~~~~~~~~~~~~~~~~
lint(
text = "any(is.na(foo(x)))",
linters = any_is_na_linter()
)
#> <text>:1:1: warning: [any_is_na_linter] anyNA(x) is better than any(is.na(x)).
#> any(is.na(foo(x)))
#> ^~~~~~~~~~~~~~~~~~
# okay
lint(
text = "anyNA(x)",
linters = any_is_na_linter()
)
#> ℹ No lints found.
lint(
text = "anyNA(foo(x))",
linters = any_is_na_linter()
)
#> ℹ No lints found.
lint(
text = "any(!is.na(x), na.rm = TRUE)",
linters = any_is_na_linter()
)
#> ℹ No lints found.
