stop_if: Simple side effect functions

View source: R/do_if.R

stop_ifR Documentation

Simple side effect functions

Description

\Sexpr[results=rd, stage=render]{lifecycle::badge("experimental")}

If the `condition` is TRUE, generate error/warning/message with the supplied message.

Usage

stop_if(condition, message = NULL, sys.parent.n = 0L)

warn_if(condition, message = NULL, sys.parent.n = 0L)

message_if(condition, message = NULL, sys.parent.n = 0L)

Arguments

condition

The condition to check. (Logical)

message

Message. (Character)

Note: If NULL, the `condition` will be used as message.

sys.parent.n

The number of generations to go back when calling the message function.

Details

When `condition` is FALSE, they return NULL invisibly.

When `condition` is TRUE:

stop_if()

Throws error with the supplied message.

warn_if()

Throws warning with the supplied message.

message_if()

Generates message with the supplied message.

Value

Returns NULL invisibly.

Author(s)

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

Examples

# Attach packages
library(xpectr)

# Note: The use of `try()` is just for package-testing purposes


a <- 0
try(stop_if(a == 0, "'a' cannot be 0."))
try(warn_if(a == 0, "'a' was 0."))
message_if(a == 0, "'a' was so kind to be 0.")


xpectr documentation built on April 12, 2025, 2:12 a.m.