Пустые строки

Простые типы / Строки

# Литерал пустой строки
some_empty_string <- ""
# Создана из ничего
another_empty_string <- character(1)

# nzchar() даёт TRUE для непустой строки
is_empty <- !nzchar(some_empty_string)
# is_empty = TRUE
is_another_empty <- !nzchar(another_empty_string)
# is_another_empty = TRUE

# Пробелы не считаются пустотой
spaces <- "   "
spaces_empty <- !nzchar(spaces)
# spaces_empty = FALSE

# trimws() превращает её в пустую
spaces_blank <- !nzchar(trimws(spaces))
# spaces_blank = TRUE

# character(0) не содержит строк вообще
no_strings <- length(character(0))
# no_strings = 0

cat("is_empty =", is_empty, "\n")
cat("is_another_empty =", is_another_empty, "\n")
cat("spaces_empty =", spaces_empty, "\n")
cat("spaces_blank =", spaces_blank, "\n")
cat("no_strings =", no_strings, "\n")

# if() требует ровно одно значение
if_error <- tryCatch(if (nzchar(character(0))) 1,
                     error = conditionMessage)
cat("if_error =", if_error, "\n")
# if_error = argument is of length zero