Checks whether an argument is a valid column index (arg_index()) of a data set or a vector thereof (arg_indices()).
Usage
arg_index(
x,
data,
.arg = rlang::caller_arg(x),
.arg_data = rlang::caller_arg(data),
.msg = NULL,
.call
)
arg_indices(
x,
data,
.arg = rlang::caller_arg(x),
.arg_data = rlang::caller_arg(data),
.msg = NULL,
.call
)Arguments
- x
the argument to be checked
- data
a data set (i.e., a matrix or data frame)
- .arg
the name of the argument supplied to
xto appear in error messages. The default is to extract the argument's name usingrlang::caller_arg(). Ignored if.msgis supplied.- .arg_data
the name of the argument supplied to
datato appear in error messages. The default is to extract the argument's name usingrlang::caller_arg(). Ignored if.msgis supplied.- .msg
an optional alternative message to display if an error is thrown instead of the default message.
- .call
the execution environment of a currently running function, e.g.
.call = rlang::current_env(). The corresponding function call is retrieved and mentioned in error messages as the source of the error. Passed toerr(). Set toNULLto omit call information. The default is to search along the call stack for the first user-facing function in another package, if any.
Details
For arg_indices(), an error will be thrown unless one of the following are true:
xis a vector of counts (seearg_counts()) less than or equal toncol(data)xis a character vector with values a subset ofcolnames(data)
For arg_index(), x additionally must have length equal to 1. Passing arg_index() ensures that data[, x] (if data is a matrix) or data[[x]] (if x is a data frame) evaluate correctly.
If data has no column names, an error will be thrown if x is a character vector.
Examples
dat <- data.frame(col1 = 1:5,
col2 = 6:10)
f <- function(z) {
arg_index(z, dat)
}
try(f(1)) # No error
try(f(3)) # Error: not a valid index
#> Error : `z` must be the name or index of a column in `dat`.
try(f("col1")) # No error
try(f("bad_col")) # Error: not a valid index
#> Error : `z` must be the name or index of a column in `dat`.
try(f(1:2)) # Error: arg_index() requires scalar
#> Error : `z` must be the name or index of a column in `dat`.
mat <- matrix(1:9, ncol = 3)
g <- function(z) {
arg_indices(z, mat)
}
try(g(1)) # No error
try(g(1:3)) # No error
try(g("col")) # Error: `mat` has no names
#> Error : `z` must be the index of a column in `mat`.