Learn R Programming

spsUtil (version 0.1.0)

emptyIsFalse: Empty objects and FALSE will return FALSE

Description

judge if an object is empty or FALSE, and return FALSE if it is

Usage

emptyIsFalse(x)

Arguments

x

any R object

Value

NA, "", NULL, length(0), nchar == 0 and FALSE will return FALSE, otherwise TRUE.

Details

not working on S4 class objects.

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

Examples

Run this code
# NOT RUN {
emptyIsFalse(NULL)
emptyIsFalse(NA)
emptyIsFalse("")
try(`if(NULL) "not empty" else "empty"`) # will generate error
if(emptyIsFalse(NULL)) "not empty" else "empty" # this will work
# similar for `NA`, `""`, `character(0)` and more
# }

Run the code above in your browser using DataLab