check_class: Check variable class

Description Usage Arguments Value Examples

View source: R/check_class.R

Description

Match variable class with the one specified and abort by 'abort' otherwise.

Usage

1
check_class(var, type, fun_name = NULL, n = NULL, allowNULL = FALSE)

Arguments

var

variable to check

type

class to check var for

fun_name

required function name for error message, as character

n

numeric to also check length var

allowNULL

allow to pass check if var NULL? FALSE by default

Value

silent if check is passed, rlang error otherwise

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
  id <- "chr"
  check_class(id, "character")

  id <- 2
  check_class(id, "numeric")

  # check variable type and length
  check_class(id, "numeric", n = 1)

  # pass check if variable NULL
  id <- NULL
  check_class(id, "character", allowNULL = TRUE)

  ## Not run: 
    # generates error
    id <- 2
    check_class(id, "character")
  
## End(Not run)

thfuchs/testr documentation built on Jan. 22, 2021, 2:35 p.m.