Checks if object
and expected
have the same levels.
If the levels differ
vec_check_levels()
returns a list describing the problemvec_grade_levels()
returns a failing grade and informative message withgradethis::fail()
Usage
vec_check_levels(object = .result, expected = .solution, env = parent.frame())
vec_grade_levels(
object = .result,
expected = .solution,
max_diffs = 3,
env = parent.frame(),
...
)
Arguments
- object
An object to be compared to
expected
.- expected
An object containing the expected result.
- env
The environment in which to find
.result
and.solution
.- max_diffs
[numeric(1)]
The maximum number of missing and/or unexpected names to include in an informative failure message. Defaults to 3.- ...
Arguments passed on to
gradethis::fail
hint
Include 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 ofhint
can be set usinggradethis_setup()
or thegradethis.fail.hint
option.encourage
Include a random encouraging phrase with
random_encouragement()
? The default value ofencourage
can be set usinggradethis_setup()
or thegradethis.fail.encourage
option.
Value
If there are any issues, a list from vec_check_levels()
or a
gradethis::fail()
message from vec_grade_levels()
.
Otherwise, invisibly returns NULL
.
Problems
levels_n
:object
andexpected
have a different number of levels.levels
: The object has levels that are not expected, or is missing levels that are expected.levels_reversed
: Thelevels
ofobject
are in the opposite order ofexpected
.level_order
: The levels ofobject
are not in the same order asexpected
.
Examples
.result <- as.factor(rep_len(letters[1:3], 6))
.solution <- as.factor(rep_len(letters[1:2], 6))
vec_check_levels()
#> <tblcheck problem>
#> Your result should have 2 levels, but it has 3 levels.
#> $ type : chr "levels_n"
#> $ expected: int 2
#> $ actual : int 3
vec_grade_levels()
#> <gradethis_graded: [Incorrect]
#> Your result should have 2 levels, but it has 3 levels.
#> >
.result <- as.factor(letters[1:6])
.solution <- as.factor(letters[21:26])
vec_check_levels()
#> <tblcheck problem>
#> Your result should have levels named `u`, `v`, `w`, and 3 more. Your result should not have levels named `a`, `b`, `c`, or 3 more.
#> $ type : chr "levels"
#> $ missing : chr [1:6] "u" "v" "w" "x" ...
#> $ unexpected: chr [1:6] "a" "b" "c" "d" ...
vec_grade_levels()
#> <gradethis_graded: [Incorrect]
#> Your result should have levels named `u`, `v`, `w`, and 3 more.
#> Your result should not have levels named `a`, `b`, `c`, or 3
#> more.
#> >
vec_grade_levels(max_diffs = 5)
#> <gradethis_graded: [Incorrect]
#> Your result should have levels named `u`, `v`, `w`, `x`, `y`,
#> and 1 more. Your result should not have levels named `a`, `b`,
#> `c`, `d`, `e`, or 1 more.
#> >
vec_grade_levels(max_diffs = Inf)
#> <gradethis_graded: [Incorrect]
#> Your result should have levels named `u`, `v`, `w`, `x`, `y`,
#> and `z`. Your result should not have levels named `a`, `b`,
#> `c`, `d`, `e`, or `f`.
#> >