Skip to contents

nzchar() efficiently determines which of a vector of strings are empty (i.e., are ""). It should in most cases be used instead of constructions like string == "" or nchar(string) == 0.

Usage

nzchar_linter()

Details

One crucial difference is in the default handling of NA_character_, i.e., missing strings. nzchar(NA_character_) is TRUE, while NA_character_ == "" and nchar(NA_character_) == 0 are both NA. Therefore, for strict compatibility, use nzchar(x, keepNA = TRUE). If the input is known to be complete (no missing entries), this argument can be dropped for conciseness.

See also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "x[x == '']",
  linters = nzchar_linter()
)
#> ::warning file=<text>,line=1,col=3::file=<text>,line=1,col=3,[nzchar_linter] Use !nzchar(x) instead of x == "". Note that unlike nzchar(), EQ coerces to character, so you'll have to use as.character() if x is a factor. Whenever missing data is possible, please take care to use nzchar(., keepNA = TRUE); nzchar(NA) is TRUE by default.

lint(
  text = "x[nchar(x) > 0]",
  linters = nzchar_linter()
)
#> ::warning file=<text>,line=1,col=3::file=<text>,line=1,col=3,[nzchar_linter] Use nzchar(x) instead of nchar(x) > 0. Whenever missing data is possible, please take care to use nzchar(., keepNA = TRUE); nzchar(NA) is TRUE by default.

# okay
lint(
  text = "x[!nzchar(x, keepNA = TRUE)]",
  linters = nzchar_linter()
)

lint(
  text = "x[nzchar(x, keepNA = TRUE)]",
  linters = nzchar_linter()
)