allTrue | R Documentation |
This is typically used to combine multiple all.equal
tests
into a single test, in a test file called by do.test
.
allTrue(...)
... |
Each argument is typically a call to |
This is intended for use in test run by do.test
.
A typical test may contain lines that create one or more
objects, followed by commands to check that those objects have
the expected structure and/or that calculations were correct.
By using allTrue
, the tests can all be combined into the
same expression that created the objects, so that if an error
occurs it is easier to see where it occured.
if all inputs are TRUE
the value is TRUE
.
Otherwise a list indicating which arguments did not return
TRUE
, containing the actual values.
Tim Hesterberg
all.equal
,
do.test
,
expectStop
,
expectWarnings
,
identical
.
# This is the type of expression that may be found in a test file
# to be run by do.test -- inside {} are lines that create one or
# more objects, followed by multiple tests (inside allTrue) that
# check those objects.
{
y <- rnorm(30)
x <- matrix(rnorm(60), ncol=2)
fit <- lm(y~x)
allTrue(# are important components included?
all(is.element(c("coefficients", "residuals", "effects", "rank",
"fitted.values", "assign", "df.residual", "call"),
names(fit))),
{
# do coefficients match the algebraic form?
# The algebraic form is inaccurate, so allow greater tolerance
X <- cbind(1, x)
all.equal(unname(fit$coefficients),
drop(solve( t(X) %*% X, t(X) %*% y)),
tol = 1e-5)
},
# are residuals computed correctly?
all.equal(fit$residuals, y - X %*% fit$coefficients))
}
# The second test uses 'unname' to remove names and 'drop' to change a
# matrix to a vector, so the test should pass.
# The third test fails because fit$residuals is a vector with names
# while the %*% calculation returns a matrix.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.