This function validates if inputs match all/any of your options
and return error/message with possible options to use. Similar
to match.arg()
but more flexible.
This function checks if an object has a specific attribute and stops if not.
Usage
check_opts(
inputs,
opts,
input_name = "input",
type = "all",
not = "stop",
quiet = TRUE
)
check_attr(object, attr = "type", check = NULL, stop = TRUE)
Arguments
- inputs
Vector character. Check options.
- opts
Vector character. Valid options.
- input_name
Character. Custom your message and change "input" for any other string. For example: "column names".
- type
Character. Options: "all", "any."
- not
Character. Options: "stop", "message", "print", "return".
- quiet
Boolean. Keep quiet? If not, returns logical value.
- object
Object of any kind
- attr
Character. Attribute to check
- check
Character. Attribute value
- stop
Boolean. Stop if doesn't check?
Value
Boolean. Result of inputs
in opts
(options).
Depending on type
and/or stop
arguments,
errors or messages will be shown.
No return value, called for side effects.
Examples
opts <- c("A", "B", "C")
# Let's check the "all" logic
check_opts(inputs = c("A", "B"), opts, quiet = FALSE)
#> [1] TRUE
check_opts(inputs = c("X"), opts, not = "message", quiet = FALSE)
#> Your input 'X' is NOT valid; ALL inputs should match these options: 'A', 'B', 'C'
#> [1] FALSE
check_opts(inputs = c("A", "X"), opts, input_name = "value", not = "warning")
#> Warning: Your value 'X' is NOT valid; ALL inputs should match these options: 'A', 'B', 'C'
# Now let's check the "any" logic
check_opts(inputs = c("A", "X"), opts, type = "any")
check_opts(inputs = c("X"), opts, type = "any", not = "message")
#> Your input 'X' is NOT valid; ANY inputs should match these options: 'A', 'B', 'C'
check_opts(inputs = c("A", NA), opts, type = "any")
# Final trick: just ignore results
check_opts(inputs = "X", opts, not = "invisible")
test <- data.frame()
attributes(test)
#> $names
#> character(0)
#>
#> $row.names
#> integer(0)
#>
#> $class
#> [1] "data.frame"
#>
check_attr(test, "class", "data.frame")
# check_attr(test, "class", "not.data.frame")