assert_scalar <- function(x, name = deparse(substitute(x))) {
if (length(x) != 1) {
stop(sprintf("'%s' must be a scalar", name), call. = FALSE)
}
invisible(x)
}
assert_character <- function(x, name = deparse(substitute(x))) {
if (!is.character(x)) {
stop(sprintf("'%s' must be character", name), call. = FALSE)
}
invisible(x)
}
assert_numeric <- function(x, name = deparse(substitute(x))) {
if (!is.numeric(x)) {
stop(sprintf("'%s' must be numeric", name), call. = FALSE)
}
invisible(x)
}
assert_logical <- function(x, name = deparse(substitute(x))) {
if (!is.logical(x)) {
stop(sprintf("'%s' must be logical", name), call. = FALSE)
}
invisible(x)
}
assert_scalar_logical <- function(x, name = deparse(substitute(x))) {
assert_scalar(x, name)
assert_logical(x, name)
assert_nonmissing(x, name)
}
assert_scalar_numeric <- function(x, name = deparse(substitute(x))) {
assert_scalar(x, name)
assert_numeric(x, name)
assert_nonmissing(x, name)
}
assert_scalar_character <- function(x, name = deparse(substitute(x))) {
assert_scalar(x, name)
assert_character(x, name)
assert_nonmissing(x, name)
}
assert_nonmissing <- function(x, name = deparse(substitute(x))) {
if (any(is.na(x))) {
stop(sprintf("'%s' must not be NA", name), call. = FALSE)
}
invisible(x)
}
assert_named <- function(x, unique = FALSE, name = deparse(substitute(x))) {
names <- names(x)
if (is.null(names) || any(names == "")) {
stop(sprintf("'%s' must be named", name), call. = FALSE)
}
if (unique && any(duplicated(names))) {
stop(sprintf("'%s' must have unique names", name), call. = FALSE)
}
invisible(x)
}
assert_is <- function(x, what, name = deparse(substitute(x))) {
if (!inherits(x, what)) {
stop(sprintf("'%s' must be a %s", name,
paste(what, collapse = " / ")), call. = FALSE)
}
invisible(x)
}
assert_file_exists <- function(x, name = "File") {
if (!file.exists(x)) {
stop(sprintf("%s does not exist: %s", name, x), call. = FALSE)
}
invisible(x)
}
assert_is_directory <- function(x, name = "File") {
assert_file_exists(x, name)
if (!is_directory(x)) {
stop(sprintf("%s exists but is not a directory: %s", name, x),
call. = FALSE)
}
invisible(x)
}
assert_raw <- function(x, name = deparse(substitute(x))) {
if (!is.raw(x)) {
stop(sprintf("'%s' must be a raw vector", name), call. = FALSE)
}
invisible(x)
}
match_value <- function(arg, choices, name = deparse(substitute(arg))) {
assert_scalar_character(arg)
if (!(arg %in% choices)) {
stop(sprintf("%s must be one of %s",
name, paste(squote(choices), collapse = ", ")))
}
arg
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.