DefaultIfMising.RdInfix function will replace missing values with another value.
x %||% yreplacement value if x is NULL
## replace a NULL value with a replacement value
x <- NULL
x %||% "replacementValue"
#> [1] "replacementValue"
print(x)
#> NULL
## replace a NULL value within a function with a replacement value
testFunction <- function(x=NULL, y = "bar"){
x <-x %||% "foo"
print(c(x, y))
}
testFunction()
#> [1] "foo" "bar"
## replace x if length of x is 0
x <- list()
replacementValue <- "replacement"
output <- x %||% replacementValue
print(output)
#> [1] "replacement"