Checks if object and expected have the same class.
If the classes differ
tbl_check_class()andvec_check_class()return a list describing the problemtbl_grade_class()andvec_grade_class()return a failing grade and informative message withgradethis::fail()
Usage
tbl_check_class(
object = .result,
expected = .solution,
ignore_class = NULL,
env = parent.frame()
)
vec_check_class(
object = .result,
expected = .solution,
ignore_class = NULL,
env = parent.frame()
)
tbl_grade_class(
object = .result,
expected = .solution,
ignore_class = NULL,
env = parent.frame(),
...
)
vec_grade_class(
object = .result,
expected = .solution,
ignore_class = NULL,
env = parent.frame(),
...
)Arguments
- object
An object to be compared to
expected.- expected
An object containing the expected result.
- ignore_class
[character()]
A vector of classes to ignore when finding differences betweenobjectandexpected.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 ifobjecthas class integer andexpectedhas class numeric, or vice versa.If all the classes of
expectedare included inignore_class, aclassproblem will never be returned.- env
The environment in which to find
.resultand.solution.- ...
Arguments passed on to
gradethis::failhintInclude 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 ofhintcan be set usinggradethis_setup()or thegradethis.fail.hintoption.encourageInclude a random encouraging phrase with
random_encouragement()? The default value ofencouragecan be set usinggradethis_setup()or thegradethis.fail.encourageoption.
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.
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()`?
#> >
# Ignore the difference between tibble and data frame
.result <- data.frame(a = 1:10)
.solution <- tibble::tibble(a = 1:10)
tbl_check_class(ignore_class = c("tbl_df", "tbl"))
tbl_grade_class(ignore_class = c("tbl_df", "tbl"))
# Ignore the difference between integer and double
.result <- 1L
.solution <- 1
vec_check_class(ignore_class = c("integer" = "numeric"))
vec_grade_class(ignore_class = c("integer" = "numeric"))
