maybe: Maybe apply function to value

Description Usage Arguments Details Value Examples

Description

Maybe apply function to value

Usage

1
maybe(f, x, y = x)

Arguments

f

Function to be applied to ‘x’

x

Object to which ‘f’ should be applied

y

Alternative value to return if applying ‘f’ to ‘x’ leads to an error or a warning

Details

Errors or warnings caused by evaluating ‘x’ will not be caught. The purpose of maybe is only to catch errors and warning caused by evaluating ‘f(x)’.

Value

‘f(x)’ unless that results in an error or a warning, otherwise ‘y’.

Examples

1
2
3
4
5
6
7
x <- "12"
y <- "foo"
maybe(as.integer, x)
maybe(as.integer, y)
maybe(as.integer, y, 0L)
tryCatch(maybe(as.integer, stop(), 0L),
         error = function(e) pr("Evaluating `x' caused an error."))

cbaumbach/miscFun documentation built on May 13, 2019, 1:48 p.m.