This is a convenience function for testing if an object is NULL
, and
providing a custom error message if it is.
stopIfNull(target, msg = NULL)
Object to test if NULL
.
Optional custom message to display when target
is
NULL
.
If target
is not NULL
, target
is returned
invisibly.
library(MazamaCoreUtils)
# Return input invisibly if not NULL
x <- stopIfNull(5, msg = "Custom message")
print(x)
#> [1] 5
# This can be useful when building pipelines
y <- 1:10
y_mean <-
y %>%
stopIfNull() %>%
mean()
if (FALSE) {
testVar <- NULL
stopIfNull(testVar)
stopIfNull(testVar, msg = "This is NULL")
# Make a failing pipeline
z <- NULL
z_mean <-
z %>%
stopIfNull("This has failed.") %>%
mean()
}