try_pmap: Apply a function over a list of vectors, capturing any errors...

Description Usage Arguments Details Value Examples

View source: R/try-catch.R

Description

This function is similar to purrr::pmap() except that instead of stopping at the first error it captures them and continues. If there are any errors it collects them together and displays them at the end. You have the option to specify a prefix to the error message using the msg_prefix argument.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
try_pmap(
  l,
  f,
  ...,
  msg_prefix,
  warn_level = 2,
  error_level = 1,
  on_error = "error",
  simplify = FALSE,
  use_names = TRUE
)

Arguments

l

(list) A list of vectors the same length to apply the function to.

f

(function) The function to map to the elements of the vectors in l.

...

(optional) Extra arguments to supply to f.

msg_prefix

(string, optional) A message to prefix any resulting error message.

warn_level

(integer, optional) The level of any warnings about errors encountered. If 0 then no warnings are shown. Default: 2.

error_level

(integer, optional) The level of any resulting errors. Default: 1.

on_error

(string) The kind of message to produce if there is an error. Either "info", "warn", or "error". Default: "error".

simplify

(boolean, optional) Whether to try to simplify the result of the mapping into an atomic vector. Default: FALSE.

use_names

(boolean, optional) Whether to use 'x' as names in the resulting list. 'x' must be a character vector for this to work. Default: TRUE.

Details

If the mapped function is a long running process try_pmap can output a warning at the time an error occurs, but specifying the warn_level argument to be greater than 0 (see warn() for more details about message levels. Similarly error_level argument specifies the level of any reported error, as detailed in error().

If you do not want the function to stop with an error, you can instead return a warning or info message using the on_error argument.

Finally, simplify and use_names allow the user to specify whether to simplify the output to an atomic vector, if possible, and whether to use the vector input x as names to the resulting list.

Value

If simplify = FALSE a list is returned. Otherwise, the function attempts to simplify the result to an atomic vector.

Examples

1
2
3
4
5
6
## Not run: 
  test_try_pmap <- function(x, y) if (x > y) stop("x > y") else x
  try_pmap(list(1:3, 3:1), test_try_pmap)
  try_pmap(list(1:3, 2:4), test_try_pmap)

## End(Not run)

msgr documentation built on Dec. 16, 2019, 5:41 p.m.