Learn R Programming

assertions (version 0.3.0)

assert_non_null: Assert that the input is not NULL

Description

This function asserts that the input is not NULL and aborts with an error message if it is.

Usage

assert_non_null(x, msg = NULL, call = rlang::caller_env(), arg_name = NULL)

Value

invisible(TRUE) if x is not NULL, otherwise aborts with the error message specified by msg.

Arguments

x

A value to check.

msg

A character string containing the error message to display if x is NULL.

call

Only relevant when pooling assertions into multi-assertion helper functions. See cli_abort for details.

arg_name

Advanced use only. Name of the argument passed (default: NULL, will automatically extract arg_name).

Examples

Run this code

# Passes for non-NULL
assert_non_null(1)

try({
 # Throws default error for NULL
 assert_non_null(NULL)

 # Throws custom error message
 assert_non_null(NULL, msg = "Custom error message")
})

Run the code above in your browser using DataLab