From 02b3a4482908a2968e11c547e8b88a637669047e Mon Sep 17 00:00:00 2001 From: Ernest Benedito Date: Sun, 16 Dec 2018 18:13:59 +0100 Subject: [PATCH] Update tryExcept --- R/except.R | 21 ++++++++++++--------- README.md | 4 +++- man/except.Rd | 17 ++++++++++------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/R/except.R b/R/except.R index 1acbf4c..a5e3a29 100644 --- a/R/except.R +++ b/R/except.R @@ -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()) #' @@ -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) diff --git a/README.md b/README.md index 30afb7d..a4ff9f2 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 (%||%) diff --git a/man/except.Rd b/man/except.Rd index 8f58e13..9229309 100644 --- a/man/except.Rd +++ b/man/except.Rd @@ -6,14 +6,19 @@ \alias{\%except\%} \title{Simple Error Handling} \usage{ -tryExcept(expr, except = { }) +tryExcept(expr, except = { }, error = function(e) { }) expr \%except\% except } \arguments{ -\item{expr}{expression to be evaluated.} +\item{expr}{Expression to be evaluated.} -\item{except}{expression to be evaluated if \code{expr} raises an error.} +\item{except}{Expression to be evaluated if \code{expr} raises an error. By default it is +an empty expression.} + +\item{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.} } \description{ Use this method to handle errors. The function evaluates an expression, @@ -21,12 +26,11 @@ Use this method to handle errors. The function evaluates an 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. } \examples{ -\dontrun{ # No errors are raised tryExcept(stop()) @@ -58,4 +62,3 @@ print(foo) # "foo" } print(foo) # "foo bar" } -}