Checks for differences between object
and expected
in the following order:
Check table class with
tbl_check_class()
Check column names with
tbl_check_names()
Check number of rows and columns with
tbl_check_dimensions()
Check groups with
tbl_check_groups()
Check that each column is the same with
tbl_check_column()
If the tables differ
tbl_check()
returns a list describing the problemtbl_grade()
returns a failing grade and informative message withgradethis::fail()
Usage
tbl_check(
object = .result,
expected = .solution,
cols = NULL,
check_class = TRUE,
ignore_class = NULL,
check_names = TRUE,
check_column_order = FALSE,
check_dimensions = TRUE,
check_groups = TRUE,
check_columns = TRUE,
check_column_class = check_columns,
check_column_values = check_columns,
env = parent.frame()
)
tbl_grade(
object = .result,
expected = .solution,
cols = NULL,
max_diffs = 3,
check_class = TRUE,
ignore_class = NULL,
check_names = TRUE,
check_column_order = FALSE,
check_dimensions = TRUE,
check_groups = TRUE,
check_columns = TRUE,
check_column_class = check_columns,
check_column_values = check_columns,
env = parent.frame(),
...
)
Arguments
- object
A data frame to be compared to
expected
.- expected
A data frame containing the expected result.
- cols
[
tidy-select
]
A selection of columns to compare betweenobject
andexpected
. Differences in other columns will be ignored. IfNULL
, the default, all columns will be checked.- check_class
[logical(1)]
Whether to check thatobject
andexpected
have the same classes withtbl_check_class()
.- 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_names
[logical(1)]
Whether to check thatobject
andexpected
have the same column names withtbl_check_names()
.- check_column_order
[logical(1)]
Whether to check that the columns ofobject
are in the same order asexpected
withtbl_check_names()
. Defaults toFALSE
.- check_dimensions
[logical(1)]
Whether to check thatobject
andexpected
have the same number of rows and columns withtbl_check_dimensions()
.- check_groups
[logical(1)]
Whether to check thatobject
andexpected
have the same groups withdplyr::group_vars()
.- check_columns
[logical(1)]
Whether to check that all columns have the same contents withtbl_check_column()
.- check_column_class
[logical(1)]
Whether to check that each columns has the same class inobject
andexpected
.- check_column_values
[logical(1)]
Whether to check that each column has the same values inobject
andexpected
.- env
The environment in which to find
.result
and.solution
.- max_diffs
[numeric(1)]
The maximum number of mismatched values to display in an informative failure message. Passed totbl_check_names()
to determine the number of mismatched column names to display and then_values
argument oftbl_check_column()
to determine the number of mismatched column values to display. 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()
or a
gradethis::fail()
message from tbl_grade()
.
Otherwise, invisibly returns NULL
.
Problems
class
: The table does not have the expected classes.not_table
:object
does not inherit the data.frame class.names
: The table has column names that are not expected, or is missing names that are expected.names_order
: The table has the same column names as expected, but in a different order.ncol
: The table doesn't have the expected number of columns.nrow
: The table doesn't have the expected number of rows.groups
: The table has groups that are not expected, or is missing groups that are expected.
Additional problems may be produced by tbl_check_column()
.
Examples
.result <- data.frame(a = 1:10, b = 11:20)
.solution <- tibble::tibble(a = 1:10, b = 11:20)
tbl_check()
#> <tblcheck problem>
#> Your table should be a tibble (class `tbl_df`), but it is a data frame (class `data.frame`).
#> $ type : chr "class"
#> $ expected : chr [1:3] "tbl_df" "tbl" "data.frame"
#> $ actual : chr "data.frame"
#> $ expected_length: int 2
#> $ actual_length : int 2
#> $ location : chr "table"
tbl_grade()
#> <gradethis_graded: [Incorrect]
#> Your table should be a tibble (class `tbl_df`), but it is a
#> data frame (class `data.frame`).
#> >
.result <- tibble::tibble(a = 1:10, b = a, c = a, d = a, e = a, f = a)
.solution <- tibble::tibble(z = 1:10, y = z, x = z, w = z, v = z, u = z)
tbl_check()
#> <tblcheck problem>
#> Your table should have columns named `z`, `y`, `x`, and 3 more. Your table should not have columns named `a`, `b`, `c`, or 3 more.
#> $ type : chr "names"
#> $ missing : chr [1:6] "z" "y" "x" "w" ...
#> $ unexpected: chr [1:6] "a" "b" "c" "d" ...
#> $ location : chr "table"
tbl_grade()
#> <gradethis_graded: [Incorrect]
#> Your table should have columns named `z`, `y`, `x`, and 3 more.
#> Your table should not have columns named `a`, `b`, `c`, or 3
#> more.
#> >
tbl_grade(max_diffs = 5)
#> <gradethis_graded: [Incorrect]
#> Your table should have columns named `z`, `y`, `x`, `w`, `v`,
#> and 1 more. Your table should not have columns named `a`, `b`,
#> `c`, `d`, `e`, or 1 more.
#> >
tbl_grade(max_diffs = Inf)
#> <gradethis_graded: [Incorrect]
#> Your table should have columns named `z`, `y`, `x`, `w`, `v`,
#> and `u`. Your table should not have columns named `a`, `b`,
#> `c`, `d`, `e`, or `f`.
#> >
.result <- tibble::tibble(a = 1:10, b = 11:20)
.solution <- tibble::tibble(a = 1:11, b = 12:22)
tbl_check()
#> <tblcheck problem>
#> Your table should have 11 rows, but it has 10 rows.
#> $ type : chr "nrow"
#> $ expected: int 11
#> $ actual : int 10
#> $ location: chr "table"
tbl_grade()
#> <gradethis_graded: [Incorrect]
#> Your table should have 11 rows, but it has 10 rows.
#> >
.result <- tibble::tibble(a = 1:10, b = 11:20)
.solution <- tibble::tibble(a = letters[1:10], b = letters[11:20])
tbl_check()
#> <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()
#> <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, intermediate = 6:15, b = 11:20)
.solution <- tibble::tibble(a = 1:10, b = 11:20)
tbl_check(cols = any_of(names(.solution)))
tbl_grade(cols = any_of(names(.solution)))
.result <- tibble::tibble(a = 1:10, b = 11:20)
.solution <- tibble::tibble(a = 11:20, b = 1:10)
tbl_check()
#> <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()
#> <gradethis_graded: [Incorrect]
#> The first 3 values of your `a` column should be `11`, `12`, and
#> `13`, not `1`, `2`, and `3`.
#> >
tbl_grade(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(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`.
#> >
.result <- tibble::tibble(a = 1:10, b = rep(1:2, 5))
.solution <- dplyr::group_by(tibble::tibble(a = 1:10, b = rep(1:2, 5)), b)
tbl_check()
#> <tblcheck problem>
#> Your table isn't a grouped data frame, but I was expecting it to be grouped. Maybe you need to use `group_by()`?
#> $ type : chr "class"
#> $ expected : chr [1:4] "grouped_df" "tbl_df" "tbl" "data.frame"
#> $ actual : chr [1:3] "tbl_df" "tbl" "data.frame"
#> $ expected_length: int 2
#> $ actual_length : int 2
#> $ location : chr "table"
tbl_grade()
#> <gradethis_graded: [Incorrect]
#> Your table isn't a grouped data frame, but I was expecting it
#> to be grouped. Maybe you need to use `group_by()`?
#> >
tbl_grade(check_groups = FALSE)