get_val2: Get value or return default if invalid

View source: R/validate.R

get_val2R Documentation

Get value or return default if invalid

Description

Get value or return default if invalid

Usage

get_val2(x, key = NA, default = NULL, na = FALSE, min_len = 1L, ...)

Arguments

x

a list, or environment, or just any R object

key

the name to obtain from x. If NA, then return x. Default is NA

default

default value if

na, min_len, ...

passed to is_valid_ish

Value

values of the keys or default is invalid

Examples


x <- list(a=1, b = NA, c = character(0))

# ------------------------ Basic usage ------------------------

# no key, returns x if x is valid
get_val2(x)

get_val2(x, 'a', default = 'invalid')



# get 'b', NA is not filtered out
get_val2(x, 'b', default = 'invalid')

# get 'b', NA is considered invalid
get_val2(x, 'b', default = 'invalid', na = TRUE)



# get 'c', length 0 is allowed
get_val2(x, 'c', default = 'invalid', min_len = 0)

# length 0 is forbidden
get_val2(x, 'c', default = 'invalid', min_len = 1)



raveio documentation built on July 26, 2023, 5:29 p.m.