I am writing a function where I want to make sure that the input is a vector.
As tibbles do not automatically drop to vectors when a singe column is returned, I want to make sure within the function that I am working with a vector. I tried the following both with as.name
and as.symbol
and failed.
vectorSupposedly <- tibble(ids=c("v1231","v32141","v64364"))
MyFunction <- function(charVector) {
if (!is.character(charVector)) {
# Extracting the name of the supposedly 1 column and turning it to a name instead of character
colName <- as.name(names(charVector)[1])
charVector<- `$`(charVector, colName)
}
class(charVector)
}
# And here is what I get
> MyFunction(vectorSupposedly)
[1] "NULL"
Warning message:
Unknown or uninitialised column: 'colName'.
What am I doing wrong?