Skip to content

Checks for differences between object and expected in the following order:

  1. Check table class with tbl_check_class()

  2. Check column names with tbl_check_names()

  3. Check number of rows and columns with tbl_check_dimensions()

  4. Check groups with tbl_check_groups()

  5. Check that each column is the same with tbl_check_column()

If the tables differ

  • tbl_check() returns a list describing the problem

  • tbl_grade() returns a failing grade and informative message with gradethis::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 between object and expected. Differences in other columns will be ignored. If NULL, the default, all columns will be checked.

check_class

[logical(1)]
Whether to check that object and expected have the same classes with tbl_check_class().

ignore_class

[character()]
A vector of classes to ignore when finding differences between object and expected.

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 if object has class integer and expected has class numeric, or vice versa.

If all the classes of expected are included in ignore_class, a class problem will never be returned.

check_names

[logical(1)]
Whether to check that object and expected have the same column names with tbl_check_names().

check_column_order

[logical(1)]
Whether to check that the columns of object are in the same order as expected with tbl_check_names(). Defaults to FALSE.

check_dimensions

[logical(1)]
Whether to check that object and expected have the same number of rows and columns with tbl_check_dimensions().

check_groups

[logical(1)]
Whether to check that object and expected have the same groups with dplyr::group_vars().

check_columns

[logical(1)]
Whether to check that all columns have the same contents with tbl_check_column().

check_column_class

[logical(1)]
Whether to check that each columns has the same class in object and expected.

check_column_values

[logical(1)]
Whether to check that each column has the same values in object and expected.

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 to tbl_check_names() to determine the number of mismatched column names to display and the n_values argument of tbl_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() and fail_if_equal() and the message is added using the default options of give_code_feedback() and maybe_code_feedback(). The default value of hint can be set using gradethis_setup() or the gradethis.fail.hint option.

encourage

Include a random encouraging phrase with random_encouragement()? The default value of encourage can be set using gradethis_setup() or the gradethis.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

  1. class: The table does not have the expected classes.

  2. not_table: object does not inherit the data.frame class.

  3. names: The table has column names that are not expected, or is missing names that are expected.

  4. names_order: The table has the same column names as expected, but in a different order.

  5. ncol: The table doesn't have the expected number of columns.

  6. nrow: The table doesn't have the expected number of rows.

  7. 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)