@@ -83,7 +83,6 @@ check_length <- function(x, length = integer(), ..., min = 0, max = Inf,
8383 if (n %in% length ) {
8484 return (invisible (NULL ))
8585 }
86- fmt <- if (inherits(arg , " AsIs" )) identity else function (x ) sprintf(" `%s`" , x )
8786 if (length(length ) > 0 ) {
8887 type <- paste0(" a vector of length " , oxford_comma(length ))
8988 if (length(length ) == 1 ) {
@@ -96,7 +95,7 @@ check_length <- function(x, length = integer(), ..., min = 0, max = Inf,
9695 }
9796 msg <- sprintf(
9897 " %s must be %s, not length %d." ,
99- fmt (arg ), type , n
98+ fmt_arg (arg ), type , n
10099 )
101100 cli :: cli_abort(msg , call = call , arg = arg )
102101 }
@@ -122,11 +121,33 @@ check_length <- function(x, length = integer(), ..., min = 0, max = Inf,
122121
123122 msg <- sprintf(
124123 " `%s` must be a %s with %s, not length %d." ,
125- fmt (arg ), type , what , n
124+ fmt_arg (arg ), type , what , n
126125 )
127126 cli :: cli_abort(msg , call = call , arg = arg )
128127}
129128
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+
130151# ' Check graphics device capabilities
131152# '
132153# ' This function makes an attempt to estimate whether the graphics device is
@@ -419,3 +440,10 @@ check_device <- function(feature, action = "warn", op = NULL, maybe = FALSE,
419440.blend_ops <- c(" multiply" , " screen" , " overlay" , " darken" , " lighten" ,
420441 " color.dodge" , " color.burn" , " hard.light" , " soft.light" ,
421442 " difference" , " exclusion" )
443+
444+ fmt_arg <- function (x ) {
445+ if (inherits(x , " AsIs" )) {
446+ return (x )
447+ }
448+ sprintf(" `%s`" , x )
449+ }
0 commit comments