R - Determine if a variable is a string

jamesatha picture jamesatha · Jul 15, 2016 · Viewed 12.9k times · Source

Is there a way to determine if an R variable is a single string? is.character looked promising, but there was one issue: is.character(c("a", "b")) also returned TRUE which is not what I want.

Answer

jamesatha picture jamesatha · Jul 15, 2016

Based on the comments, this is my current solution:

isSingleString <- function(input) {
    is.character(input) & length(input) == 1
}