tbl_check_class: Checks that two objects have the same classes

View source: R/tbl_check_class.R

tbl_check_classR Documentation

Checks that two objects have the same classes

Description

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

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

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_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()
vec_grade_class()

.result <- data.frame(a = 1:10)
.solution <- tibble::tibble(a = 1:10)
tbl_check_class()
tbl_grade_class()

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

# 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"))


rstudio/tblcheck documentation built on March 11, 2023, 5:42 p.m.