Skip to contents

This function makes sure you know what to expect when evaluating uncertain results in an if-clause. In most cases, you should not use this function, because it can lump a lot of very different cases together, but it may have some use for fool-proofing certain if-clauses on formr.org, where a field in a survey may either not exist, be missing or have a value to check.

Usage

if_na_null(test, na = FALSE, null = FALSE)

Arguments

test

condition. can only have length 0 or length 1

na

returned if the condition has a missing value

null

passed to ifelse

Examples

testdf = data.frame(test1 = 1, test2 = NA)
if ( if_na_null(testdf$test1 == 1) ) { print("go on") }
#> [1] "go on"
if ( if_na_null(testdf$test2 == 1) ) { print("not shown") }
if ( if_na_null(testdf$test3 == 1) ) { print("not shown") }
tryCatch({ if ( if_na_null(testdf2$test1 == 1) ) { print("causes error") } }, 
   error = function(e) { warning(e) }) 
#> Error: object 'testdf2' not found