Description Usage Arguments Details Value Functions Examples
A check function test something about its first (required) parameter and returns either an empty string if the check succeeds or a non-empty string if the check fails. Even when a test fails due to an error, the error is caught and a message will be returned. The check functions described here test various properties of values.
1 2 3 4 5 6 7 8 9 | checkIsNull(value)
checkIsNotNull(value)
checkLength(value, minimumLength = 0, maximumLength = Inf)
checkIsTrue(value)
checkIsFalse(value)
|
value |
A value to check. |
minimumLength |
The minimum length |
maximumLength |
The maximum length |
Many of the these functions are simple wrappers around existing R functions. Note, calling any of these functions with an unused parameters will cause an actual error as that is thrown before the called function begins executing and hence can not be trapped and converted to a message by the called function.
All functions return either an empty string if the check succeeds or a non-empty string if the check fails (including if checking causes an error.)
checkIsNull
: Use to test if a value is NULL
. Returns one
of:
"" (empty string)
Check succeeded as value
was NULL
.
"Is not null."
Check failed as value
was something other than NULL
.
"Checking for null failed with the following error: ..."
Check failed unexpectedly with an error.
checkIsNotNull
: Use to test that a value is not NULL
. Returns
one of:
"" (empty string)
Check succeeded as value
was something other than NULL
.
"Is null."
Check failed as value
was NULL
.
"Checking for null failed with the following error: ..."
Check failed unexpectedly with an error.
checkLength
: Use to test that a value has a length between
minimumLength
and maximumLength
, inclusive. By default checks
that length is between 0
and Inf
, so you need to supply at
least one of these two extra parameters to do anything useful. Returns one
of:
"" (empty string)
Check succeeded as value
had a length >= min and had a length <=
max.
"Length is not between min
and max
."
Check failed as value
had a length > max, or had a length < min
(or both min and max were chosen poorly!).
"Length is not length
."
Check failed as value
did not have the specified length. This
message is only given when max == min.
"Bad parameter 'value'. ..."
Check failed due to an invalid value
parameter. This probably
means that the provided value
was NULL
.
checkLength(NULL)
results in a failure string, contrary to R's
very tolerant evaluation of length(NULL)
as 0
.
"Checking for length failed with the following error: ..."
Check failed unexpectedly with an error.
checkIsTrue
: Use to check if an expression (value
)
evaluates to TRUE
. Returns one of:
"" (empty string)
Check succeeded as value
evaluated to logical TRUE
.
"Is false"
Check failed as value
evaluated to logical FALSE
.
"Is NA."
Check failed as value
evaluated to logical NA
.
"Result is not a vector of mode logical."
Check failed as value
evaluated to something other than a
logical vector.
"Resulting logical vector is not of length 1."
Check failed as value
evaluated to logical vector, but either
it was empty (length 0) or it had more than one element.
"Checking expression for truth failed with the following error: ..."
Check failed unexpectedly with an error. This is probably a problem with
evaluating the provided expression.
checkIsFalse
: Use to check if an expression (value
)
evaluates to FALSE
. Returns one of:
"" (empty string)
Check succeeded as value
evaluated to logical FALSE
.
"Is true."
Check failed as value
evaluated to logical TRUE
.
"Is NA."
Check failed as value
evaluated to logical NA
.
"Result is not a vector of mode logical."
Check failed as value
evaluated to something other than a
logical vector.
"Resulting logical vector is not of length 1."
Check failed as value
evaluated to logical vector, but either
it was empty (length 0) or it had more than one element.
"Checking expression for falsehood failed with the following error: ..."
Check failed unexpectedly with an error. This is probably a problem with
evaluating the provided expression.
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | # Ensuring a value is NULL
checkIsNull( NULL )
#=> [1] ""
checkIsNull( NA )
#=> [1] "Is not null."
checkIsNull()
#=> [1] "Checking for null failed with the following error:
#=> argument \"value\" is missing, with no default"
# Testing a value for existence
checkIsNotNull( NA )
#=> [1] ""
checkIsNotNull( NULL )
#=> [1] "Is null."
checkIsNotNull()
#=> [1] "Checking for null failed with the following error:
#=> argument \"value\" is missing, with no default"
# Testing the length of a value.
checkLength( character(0) )
#=> [1] ""
checkLength( c(1, 2), minimumLength= 2 )
#=> [1] ""
checkLength( c(1, 2), minimumLength= 3 )
#=> [1] "Length is not between 3 and Inf."
checkLength( c(1, 2), max= 1 )
#=> [1] "Length is not between 0 and 1."
checkLength( c(1, 2), min= 2, max= 2 )
#=> [1] ""
checkLength( c(1, 2, 3), min= 2, max= 2 )
#=> [1] "Length is not 2."
checkLength( NULL )
#=> [1] "Bad parameter 'value'. Is null."
# Checking an expression for truth
checkIsTrue( TRUE )
#=> [1] ""
checkIsTrue( 1 + 1 == 2 )
#=> [1] ""
checkIsTrue({
x <- 1
y <- 1
x == y
})
checkIsTrue( FALSE )
#=> [1] "Is false."
checkIsTrue( FALSE == NA )
#=> [1] "Is NA."
checkIsTrue({ x <- c(T,T,F); all(x) })
#=> [1] "Is false."
checkIsTrue( c(T,T,T) )
#=> [1] "Resulting logical vector is not of length 1."
checkIsTrue( "true" )
#=> [1] "Result is not a vector of mode logical."
checkIsTrue( NULL )
#=> [1] "Result is not a vector of mode logical."
checkIsTrue( stop('Oops.') )
#=> [1] ""Checking expression for truth failed with the following error: Oops."
# Checking an expression for falsehood
checkIsFalse( TRUE )
#=> [1] ""
checkIsFalse( 1 + 1 == 3 )
#=> [1] ""
checkIsFalse({
x <- 1
y <- 2
x == y
})
checkIsFalse( TRUE )
#=> [1] "Is true"
checkIsFalse( TRUE == NA )
#=> [1] "Is NA."
checkIsFalse({ x <- c(T,T,F); any(x) })
#=> [1] "Is false."
checkIsFalse( c(F,F,F) )
#=> [1] "Resulting logical vector is not of length 1."
checkIsFalse( "false" )
#=> [1] "Result is not a vector of mode logical."
checkIsFalse( NULL )
#=> [1] "Result is not a vector of mode logical."
checkIsFalse( stop('Oops.') )
#=> [1] "Checking expression for falsehood failed with the following error: Oops."
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.