stopIfNull: Stop if an object is NULL

Description Usage Arguments Value Examples

View source: R/stopIfNull.R

Description

This is a convenience function for testing if an object is NULL, and providing a custom error message if it is.

Usage

1
stopIfNull(target, msg = NULL)

Arguments

target

Object to test if NULL.

msg

Optional custom message to display when target is NULL.

Value

If target is not NULL, target is returned invisibly.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
library(MazamaCoreUtils)

# Return input invisibly if not NULL
x <- stopIfNull(5, msg = "Custom message")
print(x)

# This can be useful when building pipelines
y <- 1:10
y_mean <-
  y %>%
  stopIfNull() %>%
  mean()

## Not run: 
testVar <- NULL
stopIfNull(testVar)
stopIfNull(testVar, msg = "This is NULL")

# Make a failing pipeline
z <- NULL
z_mean <-
  z %>%
  stopIfNull("This has failed.") %>%
  mean()

## End(Not run)

MazamaCoreUtils documentation built on Nov. 12, 2021, 1:07 a.m.