vet_token: Vetting Tokens With Custom Error Messages

Description Usage Arguments Format Details Value Note See Also Examples

View source: R/validators.R

Description

Utility function to generate vetting tokens with attached error messages. You should only need to use this if the error message produced naturally by vetr is unclear. Several predefined tokens created by this function are also documented here.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

Arguments

exp

an expression which will be captured but not evaluated

err.msg

character(1L) a message that tells the user what the expected value should be, should contain a “%s” for sprintf to use (e.g. “%sshould be greater than 2”)

Format

An object of class call of length 2.

Details

Allows you to supply error messages for vetting to use for each error token. Your token should not contain top level && or ||. If it does your error message will not be reported because vetr looks for error messages attached to atomic tokens. If your token must involve top level && or ||, use I(x && y) to ensure that your error message is used by vet, but beware than in doing so you do not use templates within the I call as everything therein will be interpreted as a vetting expression rather than a template.

Error messages are typically of the form “%sshould be XXX”.

This package ships with many predefined tokens for common use cases. They are listed in the “Usage” section of this documentation. The tokens are named in format TYPE[.LENGTH][.OTHER]. For example INT will vet an integer vector, INT.1 will vet a scalar integer vector, and INT.1.POS.STR will vet a strictly positive integer vector. At this time tokens are predefined for the basic types as scalars or any-length vectors. Some additional checks are available (e.g. positive only values).

Every one of the predefined vetting tokens documented here implicitly disallows NAs. Numeric tokens also disallow infinite values. If you wish to allow NAs or infinite values just use a template object (e.g. integer(1L)).

Value

a quoted expressions with err.msg attribute set

Note

This will only work with standard tokens containing .. Anything else will be interpreted as a template token.

See Also

vet()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
## Predefined tokens:
vet(INT.1, 1:2)
vet(INT.1 || LGL, 1:2)
vet(INT.1 || LGL, c(TRUE, FALSE))

## Check squareness
mx <- matrix(1:3)
SQR <- vet_token(nrow(.) == ncol(.), "%sshould be square")
vet(SQR, mx)

## Let `vetr` make up error message; note `quote` vs `vet_token`
## Often, `vetr` does fine without explictly specified err msg:
SQR.V2 <- quote(nrow(.) == ncol(.))
vet(SQR.V2, mx)

## Combine some tokens, notice how we use `quote` at the combining
## step:
NUM.MX <- vet_token(matrix(numeric(), 0, 0), "%sshould be numeric matrix")
SQR.NUM.MX <- quote(NUM.MX && SQR)
vet(SQR.NUM.MX, mx)

## If instead we used `vet_token` the overall error message
## is not used; instead it falls back to the error message of
## the specific sub-token that fails:
NUM.MX <- vet_token(matrix(numeric(), 0, 0), "%sshould be numeric matrix")
SQR.NUM.MX.V2 <-
  vet_token(NUM.MX && SQR, "%sshould be a square numeric matrix")
vet(SQR.NUM.MX.V2, mx)

brodieG/vetr documentation built on Aug. 19, 2018, 12:35 a.m.