test <- function(value, dtype) {
# Test if a value is an instance of type `dtype`.
#
# Arguments:
# value {char} -- value to test
# dtype {char} -- one of c('logical', 'date', 'numeric', 'char')
#
# Returns:
# {logical}
stopifnot(dtype %in% c('logical', 'date', 'numeric', 'char'))
tryCatch({
if (dtype == "logical") {
if (tolower(value) %in% c("true", "t", "yes", "y", "false", "no", "n")) {
return(TRUE)
} else {
return(FALSE)
}
} else if (dtype == "date") {
test = as.Date(value)
} else if (dtype == "numeric") {
test = suppressWarnings(as.numeric(value))
if (is.na(test)) { return(FALSE) } else { return(TRUE) }
} else if (dtype == "char") {
test = suppressWarnings(as.character(value))
if (is.na(test)) { return(FALSE) } else { return(TRUE) }
}
return(TRUE)
}, error = function(e) {
return(FALSE)
})
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.