is_type: Check the variable type & class

Description Usage Arguments Details Value See Also Examples

View source: R/predicates.R

Description

This function returns TRUE if the variable type is correct and FALSE otherwise. Depending on the type, a number of other attributes can be checked simultaneously, such as length, using n, or size of rows and columns, using n_row and n_col respectively.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
is_type(x, type, n = NULL)

is_class(x, class, n = NULL)

is_natural(x, n = NULL)

is_number(x, n = NULL)

is_factor(x, levels = NULL, n = NULL)

is_array(x, dims = NULL)

is_matrix(x, n_col = NULL, n_row = NULL)

is_data_frame(x, n_col = NULL, n_row = NULL)

Arguments

x

(any) The object to test.

type

(character) The allowed type.

n

(integer, optional) The allowed length. If NULL the length is not checked. Default: NULL.

class

(character) The allowed class.

levels

(character, optional) The allowed levels. If NULL the levels are not checked. Default: NULL.

dims

(integer, optional) The allowed dimension sizes. If NULL the dimensions are not checked. Default: NULL.

n_col

(integer, optional) The allowed number of columns. If NULL the columns are not checked. Default: NULL.

n_row

(integer, optional) The allowed number of rows. If NULL the rows are not checked. Default: NULL.

Details

The following types can be checked:

Value

TRUE if x is a valid URL, FALSE otherwise

See Also

rlang::is_null(), rlang::is_atomic(), rlang::is_vector(), rlang::is_logical(), rlang::is_integer(), rlang::is_double(), rlang::is_character(), rlang::is_raw(), rlang::is_bytes(), rlang::is_list()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# TRUE
is_type(1:3, "integer")
is_type(data.frame(a = 1:26, b = letters), "list")
is_type(1:3, "integer", n = 3)
# FALSE
is_type(data.frame(a = 1:26, b = letters), "data.frame")
is_type(1:3, "integer", n = 1)

# TRUE
is_class(1:3, "integer")
is_class(data.frame(a = 1:26, b = letters), "data.frame")
is_class(1:3, "integer", n = 3)
# FALSE
is_class(data.frame(a = 1:26, b = letters), "list")
is_class(1:3, "integer", n = 1)

# TRUE
is_natural(1:3)
is_natural(c(1.0, 2.0))
is_natural(1:3, n = 3)
# FALSE
is_natural(-1:-3)
is_natural(3.142)
is_natural(1:3, n = 1)

# TRUE
is_number(1:3)
is_number(c(1.2, 2.4))
is_number(1:3, n = 3)
# FALSE
is_number("text")
is_number(1:3, n = 1)

# TRUE
is_factor(factor(c("a", "b", "a")))
is_factor(factor(c("a", "b", "a")), n = 3)
is_factor(factor(c("a", "b", "a")), levels = c("a", "b"))
# FALSE
is_factor(1:3)
is_factor(factor(c("a", "b", "a")), n = 1)
is_factor(factor(c("a", "b", "a")), levels = c("A", "B"))

# TRUE
is_array(array(1:3))
is_array(array(1:12, dim = c(3, 4)))
is_array(array(1:12, dim = c(3, 4)), dims = c(3, 4))
# FALSE
is_array(1:3)
is_array(array(1:12, dim = c(3, 4)), dims = c(4, 3))

# TRUE
is_matrix(matrix(1:3))
is_matrix(matrix(1:12, nrow = 3, ncol = 4), n_col = 4)
is_matrix(matrix(1:12, nrow = 3, ncol = 4), n_row = 3)
# FALSE
is_matrix(1:3)
is_matrix(matrix(1:12, nrow = 3, ncol = 4), n_col = 3)
is_matrix(matrix(1:12, nrow = 3, ncol = 4), n_row = 4)

# TRUE
is_data_frame(data.frame(a = 1:26, b = letters))
is_data_frame(data.frame(a = 1:26, b = letters), n_col = 2)
is_data_frame(data.frame(a = 1:26, b = letters), n_row = 26)
# FALSE
is_data_frame(list(a = 1:26, b = letters))
is_data_frame(data.frame(a = 1:26, b = letters), n_col = 5)
is_data_frame(data.frame(a = 1:26, b = letters), n_row = 5)

ChadGoymer/msgr documentation built on April 10, 2021, 10:31 a.m.