Checks for differences between the name
column in object
and in
expected
in the following order:
Check that the
name
column exists inobject
Check class with
vec_check_class()
Check length with
vec_check_dimensions()
If the column is a factor, check factor levels with
vec_check_levels()
Check column values with
vec_check_values()
If the columns differ
tbl_check_column()
returns a list describing the problemtbl_grade_column()
returns a failing grade and informative message withgradethis::fail()
Usage
tbl_check_column(
column,
object = .result,
expected = .solution,
check_class = TRUE,
ignore_class = NULL,
check_length = TRUE,
check_values = TRUE,
check_names = FALSE,
env = parent.frame()
)
tbl_grade_column(
column,
object = .result,
expected = .solution,
max_diffs = 3,
check_class = TRUE,
ignore_class = NULL,
check_length = TRUE,
check_values = TRUE,
check_names = FALSE,
env = parent.frame(),
...
)
Arguments
- column
[character(1)]
The name of the column to check.- object
A data frame to be compared to
expected
.- expected
A data frame containing the expected result.
- check_class
[logical(1)]
Whether to check thatcolumn
has the same class inobject
andexpected
.- ignore_class
[character()]
A vector of classes to ignore when finding differences betweenobject
andexpected
.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 ifobject
has class integer andexpected
has class numeric, or vice versa.If all the classes of
expected
are included inignore_class
, aclass
problem will never be returned.- check_length
[logical(1)]
Whether to check thatcolumn
has the same length inobject
andexpected
.- check_values
[logical(1)]
Whether to check thatcolumn
has the same values inobject
andexpected
.- check_names
[logical(1)]
Whether to check thatcolumn
has the same names inobject
andexpected
. Defaults toFALSE
.- env
The environment in which to find
.result
and.solution
.- max_diffs
[numeric(1)]
The maximum number of mismatched values to print. 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 tbl_check_column()
or a
gradethis::fail()
message from tbl_grade_column()
.
Otherwise, invisibly returns NULL
.
Problems
names
(table_problem
):object
doesn't contain a column named column.class
: Any mismatch in the classes of thecolumn
.length
: Thecolumn
doesn't have the expected length.levels_n
,levels
,levels_reversed
,levels_order
: Seevec_check_levels()
.values
: Thecolumn
doesn't have the expected values.names
(column_problem
): Thecolumn
has different names than expected.names_order
: Thecolumn
has the same names as expected, but in a different order.
Examples
.result <- tibble::tibble(a = 1:10, b = 11:20)
.solution <- tibble::tibble(a = letters[1:10], b = letters[11:20])
tbl_check_column("a")
#> <tblcheck problem>
#> Your `a` column 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 "column"
#> $ column : chr "a"
tbl_grade_column("a")
#> <gradethis_graded: [Incorrect]
#> Your `a` column should be a vector of text (class `character`),
#> but it is a vector of integers (class `integer`).
#> >
.result <- tibble::tibble(a = 1:10, b = 11:20)
.solution <- tibble::tibble(a = 1:11, b = 12:22)
tbl_check_column("a")
#> <tblcheck problem>
#> Your `a` column 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 "column"
#> $ column : chr "a"
tbl_grade_column("a")
#> <gradethis_graded: [Incorrect]
#> Your `a` column should contain 11 values, but it has 10 values.
#> I expected your result to include the value `11`.
#> >
.result <- tibble::tibble(a = 1:10, b = 11:20)
.solution <- tibble::tibble(a = 11:20, b = 1:10)
tbl_check_column("a")
#> <tblcheck problem>
#> The first 3 values of your `a` column 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 "column"
#> $ column : chr "a"
tbl_grade_column("a")
#> <gradethis_graded: [Incorrect]
#> The first 3 values of your `a` column should be `11`, `12`, and
#> `13`, not `1`, `2`, and `3`.
#> >
tbl_grade_column("a", max_diffs = 5)
#> <gradethis_graded: [Incorrect]
#> The first 5 values of your `a` column should be `11`, `12`,
#> `13`, `14`, and `15`, not `1`, `2`, `3`, `4`, and `5`.
#> >
tbl_grade_column("a", max_diffs = Inf)
#> <gradethis_graded: [Incorrect]
#> The first 10 values of your `a` column 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`.
#> >