% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/arg_named.R
\name{arg_named}
\alias{arg_named}
\alias{arg_colnamed}
\title{Check Named Argument}
\usage{
arg_named(x, .arg = rlang::caller_arg(x), .msg = NULL, .call)

arg_colnamed(x, .arg = rlang::caller_arg(x), .msg = NULL, .call)
}
\arguments{
\item{x}{the argument to be checked}

\item{.arg}{the name of the argument supplied to \code{x} to appear in error messages. The default is to extract the argument's name using \code{\link[rlang:caller_arg]{rlang::caller_arg()}}. Ignored if \code{.msg} is supplied.}

\item{.msg}{an optional alternative message to display if an error is thrown instead of the default message.}

\item{.call}{the execution environment of a currently running function, e.g. \code{.call = rlang::current_env()}. The corresponding function call is retrieved and mentioned in error messages as the source of the error. Passed to \code{\link[=err]{err()}}. Set to \code{NULL} to omit call information. The default is to search along the call stack for the first user-facing function in another package, if any.}
}
\value{
Returns \code{NULL} invisibly if an error is not thrown.
}
\description{
Checks whether an argument has valid (non-\code{NULL}, non-empty, and non-\code{NA}) names.
}
\details{
\code{arg_named()} works for vectors, lists, and data frames. To check whether a matrix has column names, use \code{arg_colnamed()} (which also works for data frames, but not vectors or lists).
}
\examples{
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
}
\seealso{
\code{\link[rlang:is_named]{rlang::is_named()}}, \code{\link[=names]{names()}}, \code{\link[=colnames]{colnames()}}, \code{\link[=arg_data]{arg_data()}}
}
