capture_side_effects: Capture side effects

View source: R/side_effects.R

capture_side_effectsR Documentation

Capture side effects

Description

Captures errors, warnings, and messages from an expression.

In case of an error, no other side effects are captured.

Simple wrapper for testthat's capture_error(), capture_warnings() and capture_messages().

Note: Evaluates expr up to three times.

Usage

capture_side_effects(
  expr,
  envir = NULL,
  copy_env = FALSE,
  reset_seed = FALSE,
  disable_crayon = TRUE
)

Arguments

expr

Expression.

envir

Environment to evaluate in. Defaults to parent.frame().

copy_env

Whether to use deep copies of the environment when capturing side effects. (Logical)

Disabled by default to save memory but is often preferable to enable, e.g. when the function alters non-local variables before throwing its error/warning/message.

reset_seed

Whether to reset the random state on exit. (Logical)

disable_crayon

Whether to disable crayon formatting. This can remove ANSI characters from the messages. (Logical)

Value

named list with the side effects.

Author(s)

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

See Also

Other capturers: capture_parse_eval_side_effects()

Examples

# Attach packages
library(xpectr)

fn <- function(raise = FALSE){
  message("Hi! I'm Kevin, your favorite message!")
  warning("G'Day Mam! I'm a warning to the world!")
  message("Kevin is ma name! Yesss!")
  warning("Hopefully the whole world will see me :o")
  if (isTRUE(raise)){
    stop("Lord Evil Error has arrived! Yeehaaa")
  }
  "the output"
}

capture_side_effects(fn())
capture_side_effects(fn(raise = TRUE))
capture_side_effects(fn(raise = TRUE), copy_env = TRUE)


xpectr documentation built on Nov. 18, 2022, 5:10 p.m.