I'm writing a function with a case_when statement and I can't figure out why my is.null(x) check in the case when will not result in "I'm NULL." I've simplified my issue below, test_function1 gives the result I'd expect and test_function does not. Is this an issue with case_when or something I'm not understanding about how is.null() works? I'd like to just handle everything in the case_when statement. Any insights are appreciated, thanks!
Function calls and result:
test_function1(NULL) --> "other content"
test_function(NULL) --> character(0)
Functions:
test_function1 <- function(x) {
if (is.null(x)) {
return("other content")
}
dplyr::case_when(
grepl("word", x, ignore.case = TRUE) ~ "I'm a word",
TRUE ~ "other"
)
}
test_function <- function(x) {
dplyr::case_when(
grepl("word", x, ignore.case = TRUE) ~ "I'm a word",
is.null(x) ~ "I'm NULL",
TRUE ~ "other"
)
}