util_expect_scalar: check, if a scalar/vector function argument matches...

View source: R/util_expect_scalar.R

util_expect_scalarR Documentation

check, if a scalar/vector function argument matches expectations

Description

check, if a scalar/vector function argument matches expectations

Usage

util_expect_scalar(
  arg_name,
  allow_more_than_one = FALSE,
  allow_null = FALSE,
  allow_na = FALSE,
  min_length = -Inf,
  max_length = Inf,
  check_type,
  convert_if_possible,
  dont_assign = FALSE
)

Arguments

arg_name

the argument

allow_more_than_one

allow vectors

allow_null

allow NULL

allow_na

allow NAs

min_length

minimum length of the argument's value

max_length

maximum length of the argument's value

check_type

a predicate function, that must return TRUE on the argument's value.

convert_if_possible

if given, a lambda can be given similar to check_type This lambda would be used to try a conversion. If a conversion fails (returns NA, where the input was not ‘util_empty’), an error is still thrown, the data is converted, otherwise

dont_assign

set TRUE to keep x in the caller environment untouched

Value

the value of arg_name – but this is updated in the calling frame anyway.

Examples

## Not run: 
f <- function(x) {
  util_expect_scalar(x, check_type = is.integer)
}
f(42L)
try(f(42))
g <- function(x) {
  util_expect_scalar(x, check_type = is.integer, convert_if_possible =
          as.integer)
}
g(42L)
g(42)

## End(Not run)

dataquieR documentation built on July 26, 2023, 6:10 p.m.