vn: Validate input as numeric and replace with default on empty...

vnR Documentation

Validate input as numeric and replace with default on empty input if desired

Description

This function is intended to be a more flexible replacement for calling is.numeric(). In particular, it makes it possible to replace empty values with a default value (e.g., 0) and to define whether NULL and/or NA are treated as empty values. It also allows checking whether the input (or a resulting default) value falls in a given range. If no range is specified, then any numeric value is allowed. It also allows specification of whether the bounds represent an open, closed, or semi-closed interval through the bounds_type argument. That argument is a 2 character string composed of any combination of 'i' and 'e' to designate whether the bounds are inclusive or exclusive. The first character of the string is for the lower bound and the second is for the upper bound, e.g., if the string is "ei", then the function will check whether the value is strictly > the lower bound and <= to the upper bound.

Usage

vn(
  var_value,
  range_lo = -Inf,
  range_hi = Inf,
  bounds_types = "ii",
  def_on_empty = FALSE,
  def = 0,
  treat_NULL_as_empty = TRUE,
  treat_NA_as_empty = TRUE
)

Arguments

var_value

the value to be checked to see if it's a numeric in range

range_lo

the lower bound of the range to see if input value falls in

range_hi

the upper bound of the range to see if input value falls in

bounds_types

a 2 character string indicating whether the lower and upper bounds are inclusive or exclusive bounds, with 'i' meaning inclusive and 'e' meaning exclusive; legal strings are "ii", "ie", "ei", and "ee"

def_on_empty

boolean flag indicating whether to return a default value instead of the input value when the input value is empty (where empty is defined by other flags below)

def

a TRUE or FALSE default value to return instead of the input value when a default is requested

treat_NULL_as_empty

a boolean flag set to TRUE if a NULL input is to be treated as an empty input; FALSE otherwise

treat_NA_as_empty

a boolean flag set to TRUE if an NA input is to be treated as an empty input; FALSE otherwise

Details

Note that even if they are set to TRUE, the flags treat_NULL_as_empty and treat_NA_as_empty are ignored if def_on_empty is FALSE.

Value

Returns the input value if it is numeric and in range or, returns a specified numeric value if other arguments force a valid default value to return; otherwise, throws an error

Examples

x <- 0.7
vn (x)
vn (x, range_lo = 0, range_hi = 1)
vn (x, range_lo = 0.7, range_hi = 1, bounds_types = "ie")
vn (100, range_hi = 100)
vn (NULL, def_on_empty = TRUE, def = 0)
vn (NULL, def_on_empty = TRUE)
vn (NA, def_on_empty = TRUE, def = -999)
vn (NA, range_hi = 100, bounds_types = "ee",
    def_on_empty = TRUE, def=15, treat_NA_as_empty = TRUE)
## Not run: 
vn (1000, range_hi = 100, bounds_types = "ii")                       #  error
vn (0.7, range_lo = 0.7, bounds_types = "ei")                        #  error
vn (NULL)                                                            #  error
vn (NA)                                                              #  error
vn (NA, range_lo = -10, range_hi = 10, bounds_types = "ee",
    def_on_empty = TRUE, def = 15)                                   #  error
vn (NULL, range_lo = -10, range_hi = 10, bounds_types = "ee",
    def_on_empty = TRUE, def = 15, treat_NULL_as_empty = TRUE)       #  error
         
## End(Not run)

langfob/bdpg documentation built on Dec. 8, 2022, 5:33 a.m.