user_displays: Display conditions in output terminal

Description Usage Arguments See Also Examples

Description

These functions make a catchr plan immediately print the condition to the output terminal, similar to how display works. But unlike display and most catchr functions or special reserved terms, these functions are meant to be used in user-defined functions of a plan.

user_display() immediately displays a condition n the output terminal, and if crayon is installed, will style the text with whatever crayon styling is supplied (either as a crayon function or a character vector for crayon::combine_styles()). This function should be used within a custom function, i.e., function(x) {user_display(x, "pink")}, and not called by itself, since when it is called, it doesn't evaluate to a function, string or unquoted term, which are required input types to a catchr plan.

display_with can be used at the "top" level of a plan, since it returns a function that calls user_display(). Thus user_display("pink") is equivalent to the example above.

Usage

1
2
3
user_display(cond, crayon_style, ...)

display_with(crayon_style, ...)

Arguments

cond

A condition one wishes to display

crayon_style

If crayon is installed, this can be either a crayon style (e.g., crayon::green(), blue$bold, etc.) or a character vector for crayon::combine_styles(). These styles will be applied to the output.

...

Parameters to pass into extract_display_string(); namely cond_name (which controls how the condition is introduced) and include_call, which determines whether the call is included.

See Also

the display special term, which essentially uses a version of user_display; user_exit() and exit_with() for parallel functions for the exit special term, and beep_with() for a parallel function for the beep special term.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
make_warnings <- function() {
  warning("This warning has a call")
  warning("This warning does not", call. = FALSE)
  invisible("done")
}

# The crayon stylings won't work if `crayon` isn't installed.
catch_expr(make_warnings(), warning = c(display_with("pink"), muffle))
catch_expr(make_warnings(),
           warning = c(display_with(c("pink","bold"), include_call = FALSE), muffle))
catch_expr(make_warnings(), warning = c(display_with("inverse", cond_name=NULL), muffle))
# If you don't want to use crayon styles, just use `NULL`
catch_expr(make_warnings(), warning = c(display_with(NULL, cond_name="Warning"), muffle))

# You can get a lot of weird crayon styles
if (requireNamespace("crayon", quietly = TRUE) == TRUE) {
  freaky_colors <- crayon::strikethrough$yellow$bgBlue$bold$blurred
  catch_expr(make_warnings(),
             warning = c(function(x) user_display(x, freaky_colors), muffle))
}

burchill/catchr documentation built on Jan. 9, 2020, 2:34 p.m.