| Title: | A Lightweight Core of the 'assertive' Package |
|---|---|
| Description: | A minimal set of predicates and assertions used by the assertive package. This is mainly for use by other package developers who want to include run-time testing features in their own packages. End-users will usually want to use assertive directly. Compatibility updates by Renova. |
| Authors: | Richard Cotton [aut, cre], Sunkyu Choi [trl], Ivanka Skakun [trl], Gergely Dar<c3><b3>czi [trl], Anton Antonov [trl], Hisham Ben Hamidane [trl], Anja Billing [trl], Aditya Bhagwat [trl], Rasmus B<c3><a5><c3><a5>th [trl], Mine Cetinkaya-Rundel [trl], Aspasia Chatziefthymiou [trl] |
| Maintainer: | Richard Cotton <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.0.9.9001 |
| Built: | 2026-11-13 18:02:24 UTC |
| Source: | https://github.com/renova-r/assertive.base |
Checks if the inputs are identical.
are_identical( x, y, allow_attributes = FALSE, .xname = get_name_in_parent(x), .yname = get_name_in_parent(y) ) are_identical_legacy(..., l = list()) assert_are_identical( x, y, allow_attributes = FALSE, severity = getOption("assertive.severity", "stop") ) assert_all_are_identical_legacy(..., l = list()) assert_any_are_identical_legacy(..., l = list())are_identical( x, y, allow_attributes = FALSE, .xname = get_name_in_parent(x), .yname = get_name_in_parent(y) ) are_identical_legacy(..., l = list()) assert_are_identical( x, y, allow_attributes = FALSE, severity = getOption("assertive.severity", "stop") ) assert_all_are_identical_legacy(..., l = list()) assert_any_are_identical_legacy(..., l = list())
x |
An R object or expression. |
y |
Another R object or expression. |
allow_attributes |
If |
.xname |
Not intended to be used directly. |
.yname |
Not intended to be used directly. |
... |
Some R expressions, deprecated. |
l |
A list of R expressions, deprecated. |
severity |
How severe should the consequences of the assertion be?
Either |
are_identical returns TRUE if x and y
are identical. The assert_* function throws an error on failure.
The legacy function are_identical_legacy allows an arbitrary number
of inputs and returns a symmetric square logical matrix which is TRUE
where pairs of inputs are identical. (The new version of the function
is easier to work with, and it is recommended that you switch your code to
it.)
identical,
are_same_length in the assertive.properties package
x <- 1:5 are_identical(c(1, -1), cos(c(0, pi))) assertive.base::dont_stop(assert_are_identical(c(1, 1), cos(c(0, pi))))x <- 1:5 are_identical(c(1, -1), cos(c(0, pi))) assertive.base::dont_stop(assert_are_identical(c(1, 1), cos(c(0, pi))))
The workhorse of the package that creates an assertion from a predicate. If a condition isn't met, then an error is thrown. This function is exported for use by package developers so that they can create their own assert functions.
assert_engine( predicate, ..., msg = "The assertion failed.", what = c("all", "any"), na_ignore = FALSE, severity = c("stop", "warning", "message", "none") )assert_engine( predicate, ..., msg = "The assertion failed.", what = c("all", "any"), na_ignore = FALSE, severity = c("stop", "warning", "message", "none") )
predicate |
Function that returns a logical value (possibly a vector). |
... |
Passed to the |
msg |
The error message, in the event of failure. |
what |
Either 'all' or 'any', to reduce vectorised tests to a single value. |
na_ignore |
A logical value. If |
severity |
How severe should the consequences of the assertion be?
Either |
FALSE with the attribute message, as provided
in the input.
Missing values are considered as FALSE for the purposes of
whether or not an error is thrown.
# Basic usage is like do.call; pass a predicate and the arguments to it. dont_stop(assert_engine(is_true, c(TRUE, FALSE, NA))) # Customise the error message dont_stop( assert_engine(is_true, c(TRUE, FALSE, NA), msg = "Not everything is true") ) # Only fail when no values match the predicate's conditions dont_stop(assert_engine(is_true, logical(3), what = "any")) # You can use base predicates, but the error message isn't as informative dont_stop(assert_engine(is.matrix, 1:5)) # Reduce the severity of failure assert_engine(is_true, c(TRUE, FALSE, NA), severity = "message")# Basic usage is like do.call; pass a predicate and the arguments to it. dont_stop(assert_engine(is_true, c(TRUE, FALSE, NA))) # Customise the error message dont_stop( assert_engine(is_true, c(TRUE, FALSE, NA), msg = "Not everything is true") ) # Only fail when no values match the predicate's conditions dont_stop(assert_engine(is_true, logical(3), what = "any")) # You can use base predicates, but the error message isn't as informative dont_stop(assert_engine(is.matrix, 1:5)) # Reduce the severity of failure assert_engine(is_true, c(TRUE, FALSE, NA), severity = "message")
Error, warning, and message classes derived from their simple equivalents.
assertionError(message, call = NULL, predicate_name = NULL) assertionWarning(message, call = NULL, predicate_name = NULL) assertionMessage(message, call = NULL, predicate_name = NULL)assertionError(message, call = NULL, predicate_name = NULL) assertionWarning(message, call = NULL, predicate_name = NULL) assertionMessage(message, call = NULL, predicate_name = NULL)
message |
A string describing the problem. |
call |
A call describing the source of the condition. |
predicate_name |
A string naming the predicate that was called when the condition occured. |
An object of class assertionError, assertionWarning, or
assertionMessage.
These objects behave the same as the standard-issue simpleError,
simpleWarning, and simpleMessage objects from base-R. The
extra class allows you to provide custom handling for assertions inside
tryCatch.
tryCatch( assert_all_are_true(FALSE), error = function(e) { if(inherits(e, "assertionCondition")) { # Handle assertions message("This is an assertion condition.") # Handle assertions cause by a specific predicate if(e$predicate_name == "is_true") { } } else { # Handle other error types } } )tryCatch( assert_all_are_true(FALSE), error = function(e) { if(inherits(e, "assertionCondition")) { # Handle assertions message("This is an assertion condition.") # Handle assertions cause by a specific predicate if(e$predicate_name == "is_true") { } } else { # Handle other error types } } )
Wrapper to vapply for functions that return a boolean (logical
scalar) value.
bapply(x, predicate, ...)bapply(x, predicate, ...)
x |
A vector (atomic or list). |
predicate |
A predicate (function that returns a bool) to apply.
elementwise to |
... |
Passed to |
A logical vector.
USE.NAMES is set to TRUE
Calls a function, and names the result with the first argument.
call_and_name(fn, x, ...)call_and_name(fn, x, ...)
fn |
A function to call. See note below. |
x |
The first input to |
... |
Optional additional inputs to |
The result of fn(x, ...), with names given by the
argument x.
The function, fn, should return an object with the
same length as the input x. For speed and simplicity, this
isn't checked; it is up to the developer of the assertion to make
sure that this condition holds.
call_and_name(is.finite, c(1, Inf, NA)) # Make sure that the output is the same size as the input. # Don't do this: dont_stop(call_and_name(isTRUE, list(TRUE, FALSE, NA))) # Do this instead: call_and_name( Vectorize(isTRUE, SIMPLIFY = FALSE), list(TRUE, FALSE, NA) )call_and_name(is.finite, c(1, Inf, NA)) # Make sure that the output is the same size as the input. # Don't do this: dont_stop(call_and_name(isTRUE, list(TRUE, FALSE, NA))) # Do this instead: call_and_name( Vectorize(isTRUE, SIMPLIFY = FALSE), list(TRUE, FALSE, NA) )
"cause" attributeGets or sets the "cause" (of failure) attribute of a variable.
cause(x) cause(x) <- valuecause(x) cause(x) <- value
x |
Any variable. |
value |
Passed to |
The get method returns the "cause" attribute.
# Scalar case yn <- is_identical_to_true(FALSE) cause(yn) # Vector case yn <- is_true(c(TRUE, FALSE, NA)) cause(yn)# Scalar case yn <- is_identical_to_true(FALSE) cause(yn) # Vector case yn <- is_true(c(TRUE, FALSE, NA)) cause(yn)
Coerce the input to a different class, with a warning. More reliable then
as, and supports coercion to multiple classes.
coerce_to(x, target_class, .xname = get_name_in_parent(x))coerce_to(x, target_class, .xname = get_name_in_parent(x))
x |
Input to coerce. |
target_class |
The desired class of x. Multiple values allowed (see note). |
.xname |
Not intended to be used directly. |
The input x after attempted coercion to the target class.
If x does not already have the target class, a warning is given
before coercion.
The function will try and convert the x to each of the classes given
in target_class, in order, until it succeeds or runs out of classes
to try. It will first try and convert x using a dedicated
as.target_class function if that exists. If it does not exist, or
throws an error then coerce_to will try to use
as(x, target_class).
# Numbers can be coerced to characters but not to calls. dont_stop(coerce_to(1:5, c("call", "character")))# Numbers can be coerced to characters but not to calls. dont_stop(coerce_to(1:5, c("call", "character")))
Runs code without stopping for warnings or errors.
dont_stop(expr)dont_stop(expr)
expr |
Code to execute. |
A list containing the results of evaluating each call in expr.
This function is dangerous, since it overrides warnings and errors. Its intended use is for documenting examples of warnings and errors.
warning and stop for
generating warnings and errors respectively; try and
conditions for handling them.
dont_stop({ warning("a warning") x <- 1 stop("an error") y <- sqrt(exp(x + 1)) assert_is_identical_to_true(y) y > 0 })dont_stop({ warning("a warning") x <- 1 stop("an error") y <- sqrt(exp(x + 1)) assert_is_identical_to_true(y) y > 0 })
Always returns the value FALSE, with a cause attribute.
false(...)false(...)
... |
Passed to |
FALSE with the attribute cause, as provided
in the input.
Gets the name of the input in the parent frame.
get_name_in_parent(x, escape_percent = TRUE)get_name_in_parent(x, escape_percent = TRUE)
x |
Variable to get the name of. |
escape_percent |
Logical. If |
A string giving the name of the input in the parent frame.
outside <- 1 f <- function(inside, escape_percent) { get_name_in_parent(inside, escape_percent) } f(outside, TRUE) f('10%', TRUE) f('10%', FALSE)outside <- 1 f <- function(inside, escape_percent) { get_name_in_parent(inside, escape_percent) } f(outside, TRUE) f('10%', TRUE) f('10%', FALSE)
If a function named is.class exists, call is.class(x).
If not, call is(x, class).
is2(x, class, .xname = get_name_in_parent(x))is2(x, class, .xname = get_name_in_parent(x))
x |
Input to check. |
class |
Target class that |
.xname |
Not intended to be used directly. |
TRUE if x belongs to the class and FALSE
otherwise.
is, and
assert_is_all_of in the assertive.types package for the corresponding assert fns.
is2(1:5, "character") is2(matrix(1:5), "character") is2(1:5, c("character", "list", "numeric")) is2(mean, c("function", "data.frame"))is2(1:5, "character") is2(matrix(1:5), "character") is2(1:5, c("character", "list", "numeric")) is2(mean, c("function", "data.frame"))
Merges variable length ellipsis arguments to a function with a list argument.
merge_dots_with_list( ..., l = list(), warn_on_dupes = TRUE, allow_unnamed_elements = FALSE )merge_dots_with_list( ..., l = list(), warn_on_dupes = TRUE, allow_unnamed_elements = FALSE )
... |
Some inputs. |
l |
A list. |
warn_on_dupes |
|
allow_unnamed_elements |
|
A list containing the merged inputs.
If any arguments are present in both the ... and l
arguments, the ... version takes preference, and a warning is thrown.
merge_dots_with_list( foo = 1, bar = 2, baz = 3, l = list(foo = 4, baz = 5, quux = 6) )merge_dots_with_list( foo = 1, bar = 2, baz = 3, l = list(foo = 4, baz = 5, quux = 6) )
Merges two lists, taking duplicated elements from the first list.
## S3 method for class 'list' merge(x, y, warn_on_dupes = TRUE, allow_unnamed_elements = FALSE, ...)## S3 method for class 'list' merge(x, y, warn_on_dupes = TRUE, allow_unnamed_elements = FALSE, ...)
x |
A list. |
y |
A list. |
warn_on_dupes |
|
allow_unnamed_elements |
|
... |
Ignored. |
A list, combining elements from x and y.
In the event of elements that are duplicated between x and
y, the versions from x are used.
merge( list(foo = 1, bar = 2, baz = 3), list(foo = 4, baz = 5, quux = 6) ) # If unnamed elements are allowed, they are included at the end merge( list("a", foo = 1, "b", bar = 2, baz = 3, "c"), list(foo = 4, "a", baz = 5, "b", quux = 6, "d"), allow_unnamed_elements = TRUE )merge( list(foo = 1, bar = 2, baz = 3), list(foo = 4, baz = 5, quux = 6) ) # If unnamed elements are allowed, they are included at the end merge( list("a", foo = 1, "b", bar = 2, baz = 3, "c"), list(foo = 4, "a", baz = 5, "b", quux = 6, "d"), allow_unnamed_elements = TRUE )
Always returns the value (logical) NA, with a cause attribute.
na(...)na(...)
... |
Passed to |
NA with the attribute cause, as provided
in the input.
Parenthesise a character vector by wrapping elements in brackets, dashes or commas.
parenthesize( x, type = c("round_brackets", "square_brackets", "curly_brackets", "angle_brackets", "chevrons", "hyphens", "en_dashes", "em_dashes", "commas") ) parenthesise( x, type = c("round_brackets", "square_brackets", "curly_brackets", "angle_brackets", "chevrons", "hyphens", "en_dashes", "em_dashes", "commas") )parenthesize( x, type = c("round_brackets", "square_brackets", "curly_brackets", "angle_brackets", "chevrons", "hyphens", "en_dashes", "em_dashes", "commas") ) parenthesise( x, type = c("round_brackets", "square_brackets", "curly_brackets", "angle_brackets", "chevrons", "hyphens", "en_dashes", "em_dashes", "commas") )
x |
Character vector to wrap in parenthenses. |
type |
String naming the type of parenthesis. |
A character vector of the input wrapped in parentheses.
English grammar terminology is awfully confusing. The verb 'to parenthesise' means to wrap a phrase in brackets or dashes or commas, thus denoting it as supplementary material that could be left out. A 'parenthesis' as a noun is often used as a synonym for a round bracket.
paste("There were three", parenthesise(3), "mice in the experiment.") paste( "I love parmos", parenthesise("Teesside's finest culinary invention", "en_dashes"), "but they are sure to give me heart disease." ) parenthesise(letters[1:5], "curly") paste0( "The R language", parenthesise("an offshoot of S and Scheme", "commas"), "is quite good for data analysis." )paste("There were three", parenthesise(3), "mice in the experiment.") paste( "I love parmos", parenthesise("Teesside's finest culinary invention", "en_dashes"), "but they are sure to give me heart disease." ) parenthesise(letters[1:5], "curly") paste0( "The R language", parenthesise("an offshoot of S and Scheme", "commas"), "is quite good for data analysis." )
Prints a variable and captures the output, collapsing the value to a single string.
print_and_capture(x, ...)print_and_capture(x, ...)
x |
A variable. |
... |
Arguments passed to |
A string.
# This is useful for including data frames in warnings or errors message("This is the sleep dataset:\n", print_and_capture(sleep))# This is useful for including data frames in warnings or errors message("This is the sleep dataset:\n", print_and_capture(sleep))
Prints objects of class scalar_with_cause and
vector_with_cause.
## S3 method for class 'scalar_with_cause' print(x, ...) ## S3 method for class 'vector_with_cause' print(x, na_ignore = FALSE, n_to_show = 10, ...)## S3 method for class 'scalar_with_cause' print(x, ...) ## S3 method for class 'vector_with_cause' print(x, na_ignore = FALSE, n_to_show = 10, ...)
x |
an object of class |
... |
Currently unused. |
na_ignore |
A logical value. If |
n_to_show |
A natural number. The maximum number of failures to show. |
A version of deparse that is guaranteed to always return
a single string.
safe_deparse(expr, ...)safe_deparse(expr, ...)
expr |
Any R expression. |
... |
Passed to |
A character vector or length one.
By default the RStudio IDE truncates output in the console at 1000
characters. Consequently, if you use safe_deparse on large or complex
objects, you won't see the full value. You can change the setting using
Tools -> "Global Options..." -> Code -> Display -> Console -> "Limit length
of lines displayed in console to:".
# safe_deparse only differs from deparse when the deparse string is longer # than width.cutoff deparse(CO2, width.cutoff = 500L) # has length 6 safe_deparse(CO2) # has length 1# safe_deparse only differs from deparse when the deparse string is longer # than width.cutoff deparse(CO2, width.cutoff = 500L) # has length 6 safe_deparse(CO2) # has length 1
Sets the cause attribute of an object and returns that object.
set_cause(x, false_value, missing_value = "missing")set_cause(x, false_value, missing_value = "missing")
x |
A variable. |
false_value |
A character vector to set the cause to, where |
missing_value |
A character vector to set the cause to, where |
If x is TRUE everywhere, this returns the input
without setting a cause. Otherwise, the cause is an empty string where
x is TRUE, false_value where it is FALSE, and
missing_value where it is NA.
x, with a new cause attribute.
Strips all the attributes from a variable.
strip_attributes(x)strip_attributes(x)
x |
Input to strip. |
x, without attributes.
x <- structure(c(foo = 1, bar = 2), some_attr = 3) x2 <- strip_attributes(x) attributes(x) attributes(x2)x <- structure(c(foo = 1, bar = 2), some_attr = 3) x2 <- strip_attributes(x) attributes(x) attributes(x2)
Checks to see if the input is TRUE, FALSE or NA.
assert_is_identical_to_false( x, allow_attributes = FALSE, severity = getOption("assertive.severity", "stop") ) assert_is_identical_to_na( x, allow_attributes = FALSE, severity = getOption("assertive.severity", "stop") ) assert_is_identical_to_true( x, allow_attributes = FALSE, severity = getOption("assertive.severity", "stop") ) assert_all_are_false(x, severity = getOption("assertive.severity", "stop")) assert_any_are_false(x, severity = getOption("assertive.severity", "stop")) assert_all_are_na(x, severity = getOption("assertive.severity", "stop")) assert_any_are_na(x, severity = getOption("assertive.severity", "stop")) assert_all_are_true(x, severity = getOption("assertive.severity", "stop")) assert_any_are_true(x, severity = getOption("assertive.severity", "stop")) assert_all_are_not_false(x, severity = getOption("assertive.severity", "stop")) assert_any_are_not_false(x, severity = getOption("assertive.severity", "stop")) assert_all_are_not_na(x, severity = getOption("assertive.severity", "stop")) assert_any_are_not_na(x, severity = getOption("assertive.severity", "stop")) assert_all_are_not_true(x, severity = getOption("assertive.severity", "stop")) assert_any_are_not_true(x, severity = getOption("assertive.severity", "stop")) is_identical_to_false( x, allow_attributes = FALSE, .xname = get_name_in_parent(x) ) is_identical_to_na(x, allow_attributes = FALSE, .xname = get_name_in_parent(x)) is_identical_to_true( x, allow_attributes = FALSE, .xname = get_name_in_parent(x) ) is_false(x, .xname = get_name_in_parent(x)) is_na(x, coerce_to_logical = FALSE, .xname = get_name_in_parent(x)) is_not_na(x, coerce_to_logical = FALSE, .xname = get_name_in_parent(x)) is_not_false(x, .xname = get_name_in_parent(x)) is_not_true(x, .xname = get_name_in_parent(x)) is_true(x, .xname = get_name_in_parent(x))assert_is_identical_to_false( x, allow_attributes = FALSE, severity = getOption("assertive.severity", "stop") ) assert_is_identical_to_na( x, allow_attributes = FALSE, severity = getOption("assertive.severity", "stop") ) assert_is_identical_to_true( x, allow_attributes = FALSE, severity = getOption("assertive.severity", "stop") ) assert_all_are_false(x, severity = getOption("assertive.severity", "stop")) assert_any_are_false(x, severity = getOption("assertive.severity", "stop")) assert_all_are_na(x, severity = getOption("assertive.severity", "stop")) assert_any_are_na(x, severity = getOption("assertive.severity", "stop")) assert_all_are_true(x, severity = getOption("assertive.severity", "stop")) assert_any_are_true(x, severity = getOption("assertive.severity", "stop")) assert_all_are_not_false(x, severity = getOption("assertive.severity", "stop")) assert_any_are_not_false(x, severity = getOption("assertive.severity", "stop")) assert_all_are_not_na(x, severity = getOption("assertive.severity", "stop")) assert_any_are_not_na(x, severity = getOption("assertive.severity", "stop")) assert_all_are_not_true(x, severity = getOption("assertive.severity", "stop")) assert_any_are_not_true(x, severity = getOption("assertive.severity", "stop")) is_identical_to_false( x, allow_attributes = FALSE, .xname = get_name_in_parent(x) ) is_identical_to_na(x, allow_attributes = FALSE, .xname = get_name_in_parent(x)) is_identical_to_true( x, allow_attributes = FALSE, .xname = get_name_in_parent(x) ) is_false(x, .xname = get_name_in_parent(x)) is_na(x, coerce_to_logical = FALSE, .xname = get_name_in_parent(x)) is_not_na(x, coerce_to_logical = FALSE, .xname = get_name_in_parent(x)) is_not_false(x, .xname = get_name_in_parent(x)) is_not_true(x, .xname = get_name_in_parent(x)) is_true(x, .xname = get_name_in_parent(x))
x |
Input to check. See note. |
allow_attributes |
If |
severity |
How severe should the consequences of the assertion be?
Either |
.xname |
Not intended to be used directly. |
coerce_to_logical |
Logical: should the input be coerced to logical before checking? See note. |
The is* functions return TRUE if the input is
TRUE/FALSE. The assert_* functions return nothing but
throw an error if the corresponding is_* function returns
FALSE.
is_identical_to_true wraps the base function isTRUE,
providing more information on failure. Likewise,
is_identical_to_false checks that the input is identical to FALSE. If
allow_attributes is TRUE, a scalar value of TRUE with
attributes is allowed. is_true and is_false are vectorized,
returning TRUE when the inputs are TRUE and FALSE
respectively.
The for is_true, is_false, is_not_true and
is_not_false, x argument will be coerced to be a logical vector
if it isn't already.
Coercion to logical is optional for is_na and is_not_na. If
you do coerce, it means that is_na differs in behaviour from
base::is.na for character vector, list and data frame inputs. To
replicate the behaviour of is.na, ensure the argument
coerce_to_logical is FALSE (this is the default).
Note that in assertive version 0.1-4 and prior,
is_identical_to_true/false were named is_true/false and the
vectorized versions were not present.
# Checks against logical values using base::identical assert_is_identical_to_true(TRUE) assert_is_identical_to_false(FALSE) assert_is_identical_to_na(NA) # Other NA types match assert_is_identical_to_na(NA_complex_) # NaN is not NA dont_stop(assert_is_identical_to_na(NaN)) # For a slightly less strict test, you can ignore attributes assert_is_identical_to_true(c(truth = TRUE), allow_attributes = TRUE) assert_is_identical_to_false(matrix(FALSE), allow_attributes = TRUE) assert_is_identical_to_na(structure(NA, class = "nanana"), allow_attributes = TRUE) # Vectorized predicates (package name explicitly given to prevent # problems with testthat name clash) x <- c(TRUE, FALSE, NA) assertive.base::is_true(x) assertive.base::is_false(x) is_na(x) # ...and their opposites is_not_true(x) is_not_false(x) is_not_na(x) # Check that at least one element fits the condition assert_any_are_true(x) assert_any_are_false(x) assert_any_are_na(x) # These checks should fail: dont_stop({ assert_is_identical_to_true(c(truth = TRUE)) assert_is_identical_to_true(1) assert_is_identical_to_true(c(TRUE, TRUE)) assert_is_identical_to_false(matrix(FALSE)) assert_is_identical_to_na(structure(NA, class = "nanana")) assert_all_are_true(x) assert_all_are_false(x) assert_all_are_na(x) }) # base::is.na has non-standard behaviour for data.frames and lists. # is_na and is_not_na coerce to logical vectors (except character input). # unlist the input or use an apply function. d <- data.frame( x = c(TRUE, FALSE, NA), y = c(0, NA, 2), z = c("a", "NA", NA) ) is.na(d) is_na(unlist(d))# Checks against logical values using base::identical assert_is_identical_to_true(TRUE) assert_is_identical_to_false(FALSE) assert_is_identical_to_na(NA) # Other NA types match assert_is_identical_to_na(NA_complex_) # NaN is not NA dont_stop(assert_is_identical_to_na(NaN)) # For a slightly less strict test, you can ignore attributes assert_is_identical_to_true(c(truth = TRUE), allow_attributes = TRUE) assert_is_identical_to_false(matrix(FALSE), allow_attributes = TRUE) assert_is_identical_to_na(structure(NA, class = "nanana"), allow_attributes = TRUE) # Vectorized predicates (package name explicitly given to prevent # problems with testthat name clash) x <- c(TRUE, FALSE, NA) assertive.base::is_true(x) assertive.base::is_false(x) is_na(x) # ...and their opposites is_not_true(x) is_not_false(x) is_not_na(x) # Check that at least one element fits the condition assert_any_are_true(x) assert_any_are_false(x) assert_any_are_na(x) # These checks should fail: dont_stop({ assert_is_identical_to_true(c(truth = TRUE)) assert_is_identical_to_true(1) assert_is_identical_to_true(c(TRUE, TRUE)) assert_is_identical_to_false(matrix(FALSE)) assert_is_identical_to_na(structure(NA, class = "nanana")) assert_all_are_true(x) assert_all_are_false(x) assert_all_are_na(x) }) # base::is.na has non-standard behaviour for data.frames and lists. # is_na and is_not_na coerce to logical vectors (except character input). # unlist the input or use an apply function. d <- data.frame( x = c(TRUE, FALSE, NA), y = c(0, NA, 2), z = c("a", "NA", NA) ) is.na(d) is_na(unlist(d))
If the input is not scalar, then only the first element is returned, with a warning.
use_first(x, indexer = c("[[", "["), .xname = get_name_in_parent(x))use_first(x, indexer = c("[[", "["), .xname = get_name_in_parent(x))
x |
Input that should be scalar. |
indexer |
Either double indexing, |
.xname |
Not intended to be used directly. |
If x is scalar, it is returned unchanged, otherwise
only the first element is returned, with a warning.
dont_stop(use_first(1:5))dont_stop(use_first(1:5))