is.scalar provides a generic method for checking input is a scalar. is.string, is.flag, is.number and is.count provide tests for specific types.

is.scalar(x)

is.string(x)

is.number(x)

is.flag(x)

is.count(x)

Arguments

x

object to test

See also

Other assertions: are_equal, is.error, noNA, not_empty

Examples

# Generic check for scalars
see_if(is.scalar("a"))
#> [1] TRUE
see_if(is.scalar(1:10))
#> [1] FALSE
#> attr(,"msg")
#> [1] "1:10 is not a scalar."

# string = scalar character vector
see_if(is.string(1:3))
#> [1] FALSE
#> attr(,"msg")
#> [1] "1:3 is not a string (a length one character vector)."
see_if(is.string(c("a", "b")))
#> [1] FALSE
#> attr(,"msg")
#> [1] "c(\"a\", \"b\") is not a string (a length one character vector)."
see_if(is.string("x"))
#> [1] TRUE

# number = scalar numeric/integer vector
see_if(is.number(1:3))
#> [1] FALSE
#> attr(,"msg")
#> [1] "1:3 is not a number (a length one numeric vector)."
see_if(is.number(1.5))
#> [1] TRUE

# flag = scalar logical vector
see_if(is.flag(1:3))
#> [1] FALSE
#> attr(,"msg")
#> [1] "1:3 is not a flag (a length one logical vector)."
see_if(is.flag("a"))
#> [1] FALSE
#> attr(,"msg")
#> [1] "\"a\" is not a flag (a length one logical vector)."
see_if(is.flag(c(FALSE, FALSE, TRUE)))
#> [1] FALSE
#> attr(,"msg")
#> [1] "c(FALSE, FALSE, TRUE) is not a flag (a length one logical vector)."
see_if(is.flag(FALSE))
#> [1] TRUE

# count = scalar positive integer
see_if(is.count("a"))
#> [1] FALSE
#> attr(,"msg")
#> [1] "\"a\" is not a count (a single positive integer)"
see_if(is.count(-1))
#> [1] FALSE
#> attr(,"msg")
#> [1] "-1 is not a count (a single positive integer)"
see_if(is.count(1:5))
#> [1] FALSE
#> attr(,"msg")
#> [1] "1:5 is not a count (a single positive integer)"
see_if(is.count(1.5))
#> [1] FALSE
#> attr(,"msg")
#> [1] "1.5 is not a count (a single positive integer)"
see_if(is.count(1))
#> [1] TRUE