Learn R Programming

xpectr (version 0.4.4)

stop_if: Simple side effect functions

Description

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)

Value

Returns NULL invisibly.

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.

Author

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

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.

Examples

Run this code
# Attach packages
library(xpectr)

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

# \donttest{
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.")
# }

Run the code above in your browser using DataLab