The R Project SVN R

Rev

Rev 81729 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 81729 Rev 89245
Line 1... Line 1...
1
% File src/library/base/man/match.arg.Rd
1
% File src/library/base/man/match.arg.Rd
2
% Part of the R package, https://www.R-project.org
2
% Part of the R package, https://www.R-project.org
3
% Copyright 1995-2022 R Core Team
3
% Copyright 1995-2025 R Core Team
4
% Distributed under GPL 2 or later
4
% Distributed under GPL 2 or later
5
 
5
 
6
\name{match.arg}
6
\name{match.arg}
7
\title{Argument Verification Using Partial Matching}
7
\title{Argument Verification Using Partial Matching}
8
\description{
8
\description{
Line 16... Line 16...
16
\arguments{
16
\arguments{
17
  \item{arg}{a character vector (of length one unless \code{several.ok}
17
  \item{arg}{a character vector (of length one unless \code{several.ok}
18
    is \code{TRUE}) or \code{NULL} which means to take \code{choices[1]}.}
18
    is \code{TRUE}) or \code{NULL} which means to take \code{choices[1]}.}
19
  \item{choices}{a character vector of candidate values, often missing, see
19
  \item{choices}{a character vector of candidate values, often missing, see
20
    \sQuote{Details}.}
20
    \sQuote{Details}.}
21
  \item{several.ok}{logical specifying if \code{arg} should be allowed
21
  \item{several.ok}{either a \code{\link{logical}} specifying if \code{arg}
22
    to have more than one element.}
22
    should be allowed to have more than one element or a string, starting
-
 
23
    with \code{"all"} meaning that \code{arg} may have more than one
-
 
24
    element \emph{all} of which must match \code{choices}.}
23
}
25
}
24
\details{
26
\details{
25
  In the one-argument form \code{match.arg(arg)}, the choices are
27
  In the one-argument form \code{match.arg(arg)}, the choices are
26
  obtained from a default setting for the formal argument \code{arg} of
28
  obtained from a default setting for the formal argument \code{arg} of
27
  the function from which \code{match.arg} was called.  (Since default
29
  the function from which \code{match.arg} was called.  (Since default
Line 71... Line 73...
71
## Allowing more than one 'arg' and hence more than one match:
73
## Allowing more than one 'arg' and hence more than one match:
72
match.arg(c("gauss", "rect", "ep"),
74
match.arg(c("gauss", "rect", "ep"),
73
          c("gaussian", "epanechnikov", "rectangular", "triangular"),
75
          c("gaussian", "epanechnikov", "rectangular", "triangular"),
74
          several.ok = TRUE)
76
          several.ok = TRUE)
75
match.arg(c("a", ""),  c("", NA, "bb", "abc"), several.ok=TRUE) # |-->  "abc"
77
match.arg(c("a", ""),  c("", NA, "bb", "abc"), several.ok=TRUE) # |-->  "abc"
-
 
78
 
-
 
79
## New (2025-'26) option several.ok = "all" (or "all.match", "all_must_match", "all*" ..):
-
 
80
## --> Error if *some* of the several do not match:
-
 
81
abcd <- c("a","b","c","d")
-
 
82
try(match.arg(abcd, c("b", "c"), several.ok = FALSE)) # Error: ... 'arg' must be of length 1
-
 
83
    match.arg(abcd, c("b", "c"), several.ok = TRUE) # "b" "c"
-
 
84
try(match.arg(abcd, c("b", "c"), several.ok = "all")) # Error: ... should be one of "b", "c"
-
 
85
## i.e. _all_ of 'arg' must match the choices:
-
 
86
(m1 <- match.arg(abcd, letters, several.ok = TRUE       )) # "a" "b" "c" "d"
-
 
87
(m2 <- match.arg(abcd, letters, several.ok = "all.match")) #    ditto
-
 
88
stopifnot(identical(m1, abcd), identical(m1, m2),
-
 
89
          identical(abcd, match.arg(abcd, abcd, several.ok = TRUE)),
-
 
90
          identical(abcd, match.arg(abcd, abcd, several.ok = "all_must_match")))
76
}
91
}
77
\keyword{programming}
92
\keyword{programming}