Skip to content

Checks if object and expected have the same class. If the classes differ

  • tbl_check_class() and vec_check_class() return a list describing the problem

  • tbl_grade_class() and vec_grade_class() return a failing grade and informative message with gradethis::fail()

Usage

tbl_check_class(object = .result, expected = .solution, env = parent.frame())

vec_check_class(object = .result, expected = .solution, env = parent.frame())

tbl_grade_class(
  object = .result,
  expected = .solution,
  env = parent.frame(),
  ...
)

vec_grade_class(
  object = .result,
  expected = .solution,
  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.

...

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

Incude 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_class() and vec_check_class() or a gradethis::fail() message from tbl_grade_class() and vec_grade_class(). Otherwise, invisibly returns NULL.

Problems

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

Examples

.result <- 1:10
.solution <- as.character(1:10)
vec_check_class()
#> <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
vec_grade_class()
#> <gradethis_graded: [Incorrect]
#>   Your result should be a vector of text (class `character`), but
#>   it is a vector of integers (class `integer`).
#> >

.result <- data.frame(a = 1:10)
.solution <- tibble::tibble(a = 1:10)
tbl_check_class()
#> <tblcheck problem>
#> Your result 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 1
#> $ actual_length  : int 1
tbl_grade_class()
#> <gradethis_graded: [Incorrect]
#>   Your result should be a tibble (class `tbl_df`), but it is a
#>   data frame (class `data.frame`).
#> >

.result <- tibble::tibble(a = 1:10, b = a %% 2 == 0)
.solution <- dplyr::group_by(tibble::tibble(a = 1:10, b = a %% 2 == 0), b)
tbl_check_class()
#> <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
tbl_grade_class()
#> <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()`?
#> >