notFalsy: Judgement of falsy value

notFalsyR Documentation

Judgement of falsy value

Description

judge if an object is or not a falsy value. This includes: empty value, empty string '""', 'NULL', 'NA', length of 0 and 'FALSE' itself

Usage

notFalsy(x)

isFalsy(x)

emptyIsFalse(x)

Arguments

x

any R object

Details

R does not have good built-in methods to judge falsy values and these kind of values often cause errors in 'if' conditions, for example 'if(NULL) 1 else 2' will cause error. So this function will be useful to handle this kind of situations: 'if(notFalsy(NULL)) 1 else 2'.

1. not working on S4 class objects. 2. 'isFalsy' is the reverse of 'notFalsy': 'isFalsy(x)' = !'notFalsy(x)' 3. 'emptyIsFalse' is the old name for 'notFalsy'

Useful for if statement. Normal empty object in if will spawn error. Wrap the expression with 'emptyIsFalse' can avoid this. See examples

Value

'NA', '""', 'NULL', 'length(0)', 'nchar == 0' and 'FALSE' will return 'FALSE', otherwise 'TRUE' in 'notFalsy' and the opposite in 'isFalsy'

Examples

notFalsy(NULL)
notFalsy(NA)
notFalsy("")
try(`if(NULL) "not empty" else "empty"`) # this will generate error
if(notFalsy(NULL)) "not falsy" else "falsy" # but this will work
# Similar for `NA`, `""`, `character(0)` and more
isFalsy(NULL)
isFalsy(NA)
isFalsy("")

spsUtil documentation built on Nov. 5, 2025, 7:36 p.m.