| arg_named | R Documentation |
Checks whether an argument has valid (non-NULL, non-empty, and non-NA) names.
arg_named(x, .arg = rlang::caller_arg(x), .msg = NULL, .call)
arg_colnamed(x, .arg = rlang::caller_arg(x), .msg = NULL, .call)
x |
the argument to be checked |
.arg |
the name of the argument supplied to |
.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. |
arg_named() works for vectors, lists, and data frames. To check whether a matrix has column names, use arg_colnamed() (which also works for data frames, but not vectors or lists).
Returns NULL invisibly if an error is not thrown.
rlang::is_named(), names(), colnames(), arg_data()
obj <- c(1,
B = 2,
C = 3)
try(arg_named(obj)) # Error: one name is blank
names(obj)[1L] <- "A"
try(arg_named(obj)) # No error
obj2 <- unname(obj)
try(arg_named(obj2)) # Error: no names
# Matrix and data frame
mat <- matrix(1:6, ncol = 2L)
colnames(mat) <- c("A", "B")
try(arg_named(mat)) # Error: matrices are not named
try(arg_colnamed(mat)) # No error
dat <- as.data.frame(mat)
try(arg_named(dat)) # No error: data frames are named
try(arg_colnamed(dat)) # No error
colnames(mat) <- NULL
try(arg_colnamed(mat)) # Error: no colnames
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.