R/try_default.R

Defines functions try_default

try_default = function(fun, default = NULL, silent = FALSE) {
  #' Create a 'safe' version of a function with a default return value
  #'
  #' @param fun function to add try logic to
  #' @param default default value to return if fun errors
  #' @param silent should error be printed to console?
  #'
  #' @return either output of fun or default value in case of error
  #' @keywords internal
  #' @noRd

  function(...) {
    out = default
    try({
      out = fun(...)
    }, silent = silent)

    return(out)
  }
}

Try the regexTestR package in your browser

Any scripts or data that you put into this service are public.

regexTestR documentation built on Jan. 3, 2022, 5:12 p.m.