dont_stop should run past multiple errors

Issue #1 resolved
Richard Cotton repo owner created an issue

Currently dont_stop actually stops on the first warning or error, so it isn't much more use than try. It should run through all the code passed to it. Possible implementation:

dont_stop <- function(expr)
{
  this_env <- sys.frame(sys.nframe())

  # Split the expression up into a list of calls
  subbed_expr <- substitute(expr, this_env)
  # Temporarily wrap expr in braces, if it isn't already
  brace <- quote(`{`)
  if(!identical(subbed_expr[[1L]], brace))
  {
    subbed_expr <- c(brace, subbed_expr)
  }
  call_list <- as.list(subbed_expr)[-1L] # -1 to ignore brace
  names(call_list) <- vapply(call_list, deparse, character(1))

  handler <- function(e)
  {
    e$call <- NULL # Override the condition's call
    e
  }

  # Evaluate each one in turn
  lapply(
    call_list,
    function(doti)
    {
      tryCatch(eval(doti, this_env), warning = handler, error = handler)
    }
  )
}

Usage is, e.g.,

dont_stop(
  {
    warning("warning")
    x <- 1
    stop("error 1")
    y <- sqrt(exp(x + 1))
    stop("error 2")
    y + 1
  }
)
## $`warning("warning")`
## <simpleWarning: warning>
## 
## $`x <- 1`
## [1] 1
## 
## $`stop("error 1")`
## <simpleError: error 1>
## 
## $`y <- sqrt(exp(x + 1))`
## [1] 2.718282
## 
## $`stop("error 2")`
## <simpleError: error 2>
## 
## $`y + 1`
## [1] 3.718282

This changes the output, so thorough testing needed.