Skip to content

Commit d6ebd4c

Browse files
committed
add check function
1 parent f60e953 commit d6ebd4c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

R/utilities-checks.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,28 @@ check_length <- function(x, length = integer(), ..., min = 0, max = Inf,
126126
cli::cli_abort(msg, call = call, arg = arg)
127127
}
128128

129+
check_named <- function(x, arg = caller_arg(x), call = caller_env()) {
130+
if (missing(x)) {
131+
stop_input_type(x, "a vector", arg = arg, call = call)
132+
}
133+
if (length(x) < 1) {
134+
return(invisible())
135+
}
136+
msg <- character()
137+
if (!is_named2(x)) {
138+
msg <- sprintf("%s must have names.", fmt_arg(arg))
139+
} else if (anyDuplicated(names2(x))) {
140+
dups <- names2(x)
141+
dups <- sprintf('"%s"', unique(dups[duplicated(dups)]))
142+
dups <- oxford_comma(dups, final = "and")
143+
msg <- sprintf("%s cannot have duplicate names (%s).", fmt_arg(arg), dups)
144+
}
145+
if (length(msg) < 1) {
146+
return(invisible())
147+
}
148+
cli::cli_abort(msg, call = call, arg = arg)
149+
}
150+
129151
#' Check graphics device capabilities
130152
#'
131153
#' This function makes an attempt to estimate whether the graphics device is

0 commit comments

Comments
 (0)