Checks for differences between object and expected in the following order:
Check class with
vec_check_class()Check length with
vec_check_dimensions()If the vector is a factor, check factor levels are the same with
vec_check_levels()Check vector values are the same with
vec_check_values()Check names with
vec_check_names()
If the vectors differ
vec_check()returns a list describing the problemvec_grade()returns a failing grade and informative message withgradethis::fail()
Usage
vec_check(
object = .result,
expected = .solution,
check_class = TRUE,
ignore_class = NULL,
check_length = TRUE,
check_levels = TRUE,
check_values = TRUE,
check_names = TRUE,
env = parent.frame()
)
vec_grade(
object = .result,
expected = .solution,
max_diffs = 3,
check_class = TRUE,
ignore_class = NULL,
check_length = TRUE,
check_values = TRUE,
check_names = TRUE,
env = parent.frame(),
...
)Arguments
- object
A vector to be compared to
expected.- expected
A vector containing the expected result.
- check_class
[logical(1)]
Whether to check thatobjectandexpectedhave the same classes.- ignore_class
[character()]
A vector of classes to ignore when finding differences betweenobjectandexpected.If an element is named, differences will only be ignored between the pair of the element and its name. For example,
ignore_class = c("integer" = "numeric")will ignore class differences only ifobjecthas class integer andexpectedhas class numeric, or vice versa.If all the classes of
expectedare included inignore_class, aclassproblem will never be returned.- check_length
[logical(1)]
Whether to check thatobjectandexpectedhave the same length.- check_levels
[logical(1)]
Whether to check thatobjectandexpectedhave the same factor levels.- check_values
[logical(1)]
Whether to check thatobjectandexpectedcontain the same values.- check_names
[logical(1)]
Whether to check thatobjectandexpectedhave the same names.- env
The environment in which to find
.resultand.solution.- max_diffs
[numeric(1)]
The maximum number of mismatched values to print. Defaults to 3.- ...
Arguments passed on to
gradethis::failhintInclude a code feedback hint with the failing message? This argument only applies to
fail()andfail_if_equal()and the message is added using the default options ofgive_code_feedback()andmaybe_code_feedback(). The default value ofhintcan be set usinggradethis_setup()or thegradethis.fail.hintoption.encourageInclude a random encouraging phrase with
random_encouragement()? The default value ofencouragecan be set usinggradethis_setup()or thegradethis.fail.encourageoption.
Value
If there are any issues, a list from vec_check() or a
gradethis::fail() message from vec_grade().
Otherwise, invisibly returns NULL.
Problems
class:objectdoesn't have the same classes asexpected.length:objectdoesn't have the same length asexpected.levels_n,levels,levels_reversed,levels_order: Seevec_check_levels().values:objectdoesn't contain the same values asexpected.names:objecthas different names thanexpected.names_order:objecthas the same names asexpected, but in a different order.
Examples
.result <- 1:10
.solution <- letters[1:10]
vec_check()
#> <tblcheck problem>
#> Your result should be a vector of text (class `character`), but it is a vector of integers (class `integer`).
#> $ type : chr "class"
#> $ expected : chr "character"
#> $ actual : chr "integer"
#> $ expected_length: int 10
#> $ actual_length : int 10
#> $ location : chr "vector"
vec_grade()
#> <gradethis_graded: [Incorrect]
#> Your result should be a vector of text (class `character`), but
#> it is a vector of integers (class `integer`).
#> >
.result <- 1:10
.solution <- 1:11
vec_check()
#> <tblcheck problem>
#> Your result should contain 11 values, but it has 10 values. I expected your result to include the value `11`.
#> $ type : chr "length"
#> $ expected : int [1:11] 1 2 3 4 5 6 7 8 9 10 ...
#> $ actual : int [1:10] 1 2 3 4 5 6 7 8 9 10
#> $ expected_length: int 11
#> $ actual_length : int 10
#> $ location : chr "vector"
vec_grade()
#> <gradethis_graded: [Incorrect]
#> Your result should contain 11 values, but it has 10 values. I
#> expected your result to include the value `11`.
#> >
.result <- 1:10
.solution <- rlang::set_names(1:10, letters[1:10])
vec_check()
#> <tblcheck problem>
#> Your result should have the names `a`, `b`, `c`, and 7 more.
#> $ type : chr "names"
#> $ missing : chr [1:10] "a" "b" "c" "d" ...
#> $ location: chr "vector"
vec_grade()
#> <gradethis_graded: [Incorrect]
#> Your result should have the names `a`, `b`, `c`, and 7 more.
#> >
vec_grade(max_diffs = 5)
#> <gradethis_graded: [Incorrect]
#> Your result should have the names `a`, `b`, `c`, `d`, `e`, and
#> 5 more.
#> >
vec_grade(max_diffs = Inf)
#> <gradethis_graded: [Incorrect]
#> Your result should have the names `a`, `b`, `c`, `d`, `e`, `f`,
#> `g`, `h`, `i`, and `j`.
#> >
.result <- 1:10
.solution <- 11:20
vec_check()
#> <tblcheck problem>
#> The first 3 values of your result should be `11`, `12`, and `13`, not `1`, `2`, and `3`.
#> $ type : chr "values"
#> $ expected: int [1:10] 11 12 13 14 15 16 17 18 19 20
#> $ actual : int [1:10] 1 2 3 4 5 6 7 8 9 10
#> $ location: chr "vector"
vec_grade()
#> <gradethis_graded: [Incorrect]
#> The first 3 values of your result should be `11`, `12`, and
#> `13`, not `1`, `2`, and `3`.
#> >
vec_grade(max_diffs = 5)
#> <gradethis_graded: [Incorrect]
#> The first 5 values of your result should be `11`, `12`, `13`,
#> `14`, and `15`, not `1`, `2`, `3`, `4`, and `5`.
#> >
vec_grade(max_diffs = Inf)
#> <gradethis_graded: [Incorrect]
#> The first 10 values of your result should be `11`, `12`, `13`,
#> `14`, `15`, `16`, `17`, `18`, `19`, and `20`, not `1`, `2`,
#> `3`, `4`, `5`, `6`, `7`, `8`, `9`, and `10`.
#> >
