Skip to content

Commit

Permalink
Update tryExcept
Browse files Browse the repository at this point in the history
  • Loading branch information
ebeneditos committed Dec 16, 2018
1 parent 88ab07c commit 02b3a44
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
21 changes: 12 additions & 9 deletions R/except.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
#' and if it raises an error then evaluates a second expression.
#'
#' @details \code{tryExcept} is a wrapper around \code{\link[base]{tryCatch}},
#' but it allows you to run a whole expression (\code{except}) when an error occurs to
#' the first expression argument (\code{expr}). Note that, if \code{expr} raises
#' but it allows you to evaluate an expression \code{except} when an error occurs to
#' the first expression argument \code{expr}. Note that, if \code{expr} raises
#' an error, the code evaluated before the error will be in use.
#' @param expr expression to be evaluated.
#' @param except expression to be evaluated if \code{expr} raises an error.
#' @examples \dontrun{
#' @param expr Expression to be evaluated.
#' @param except Expression to be evaluated if \code{expr} raises an error. By default it is
#' an empty expression.
#' @param error Handler function for an \code{error} condition occurred during the evaluation
#' of \code{expr}. It's output is not used, as the output in case of an error is the
#' evaluation of \code{except}. By default it is an empty function.
#' @examples
#' # No errors are raised
#' tryExcept(stop())
#'
Expand Down Expand Up @@ -45,14 +49,13 @@
#' foo <- "foo bar"
#' }
#' print(foo) # "foo bar"
#' }
#' @export
tryExcept <- function (expr, except = {})
tryExcept <- function(expr, except = {}, error = function(e){})
{
tryCatch(return(expr), error = function(e){})
tryCatch(return(expr), error = error)
invisible(except)
}

#' @rdname except
#' @export
`%except%` <- tryExcept
`%except%` <- function(expr, except) tryExcept(expr, except)
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ devtools::install_github("ebeneditos/infix")

## Usage

You can find a full list of operators running `?infix`, but here there are a few examples:

```r
library(infix)

Expand All @@ -46,7 +48,7 @@ print(foo) # "foo bar"
# file.path (%//%)
"home" %//% "dir" # returns "home/dir"

# no match (%!in%)
# nomatch (%!in%)
4 %!in% 1:3 # returns TRUE

# nil (%||%)
Expand Down
17 changes: 10 additions & 7 deletions man/except.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 02b3a44

Please sign in to comment.