From ebenedito@ @ending from gm@il@com  Fri Nov 23 15:47:19 2018
From: ebenedito@ @ending from gm@il@com (Ernest Benedito)
Date: Fri, 23 Nov 2018 15:47:19 +0100
Subject: [Rd] [tryExcept] New try Function
In-Reply-To: <97C3B2B2-A8C8-4A6B-B609-1964F16A4A1C@dans.knaw.nl>
References: <ADFC5C3F-9849-4AF3-86B0-017615152298@gmail.com>
 <97C3B2B2-A8C8-4A6B-B609-1964F16A4A1C@dans.knaw.nl>
Message-ID: <CAEEE=psD_zkX8skKhJd_uTu_CXqBi7HjV_FhiKYfUyh2NwF0Hg@mail.gmail.com>

Hi Emil,

First, thanks for the response. As you mentioned, a lot of times tryCatch
does the work, as you can return a value. However, sometimes it is useful
to assign several variables when an error occurs. You could do it with <<-,
but I prefer to reduce it's usage unless completely necessary.

I guess that the attachment was missed in the moderation. Here it is the
function:

tryExcept <- function (expr,
                       except = {})
{
  doTryExcept <- function(expr, parentenv) {
    .Internal(.addCondHands("error", list(NULL), parentenv,
                            environment(), FALSE))
    expr
  }
  parentenv <- parent.frame()
  doTryExcept(return(expr), parentenv)
  invisible(except)
}

As you can see, the tryExcept function uses a simplified version of the
tryCatch architecture, but it allows you to pass by a second expression
that is evaluated in case an error occurs during the evaluation of the
first expression. It could even work as an infix operator:

`%except%` <- tryExcept

# dummy example
{foo <- "foo"} %except% {foo <- "foo bar"}
print(foo) # "foo"

{ foo <- "foo"
  stop()
} %except% {
  foo <- "foo bar"
}
print(foo) # "foo bar"

It's main downside is that you are not able to handle the error occured,
although there is the possibility to add a 'silent' parameter such as in
'try' in order to print the error if desired. All in all, this would be a
function for simple error handling, but I think it would be practical, and
you can always move to tryCatch if you need a more complex error handling.

I will be looking forward to hearing your insights.

Best,
Ernest Benedito

Missatge de Emil Bode <emil.bode at dans.knaw.nl> del dia dv., 23 de nov. 2018
a les 13:17:

> Hi Ernest,
>
> To start: I don't see an attachment, I think they're not (always) allowed
> on this mailing-list. If you want to send something, text is your safest
> bet.
> But regarding the issue of tryCatch: I think you're not fully using what
> it already can do. In almost all circumstances I've encountered the
> following works fine:
> res <- tryCatch(expr, error = function(cond) {
>   # a bunch of code
>   # Some value to be stored in res
> })
> The only difference is that now "#abunchofcode" is run from inside a
> function, which means you're working in a different environment, and if you
> want to assign values to other variables you need to use <<- or assign.
> For a modified function, I think it would be nice if there's a way to
> supply an expression instead of a function, so that evaluation (and
> assignment!) takes place in the same environment as the main code in the
> tryCatch (in expr). Is that what you made?
> And with the current tryCatch, you could use something like this:
> res <- tryCatch(expr, error=function(e) evalq({
>   # a bunch of code
>   # Some value for res
> }, envir=parent.frame(4))) # The 4 is because some internal functions are
> involved, parent.frame(4) is the same environment as used by expr
>
> Although this is cumbersome, and it gets even more cumbersome if you want
> to access the error-object in #abunchofcode, or use #abunchofcode to return
> to a higher level, so I get it you're looking for a more elegant solution.
>
> Best regards,
> Emil Bode
>
> ?On 23/11/2018, 08:49, "R-devel on behalf of Ernest Benedito" <
> r-devel-bounces at r-project.org on behalf of ebeneditos at gmail.com> wrote:
>
>     Hi everyone,
>
>     When dealing with errors, sometimes I want to run a bunch of code when
> an error occurs.
>     For now I usually use a structure such as:
>
>     res <- tryCatch(expr, error = function(cond) cond) # or try(expr)
>
>     if (inherits(res, ?error?)) # or inherits(res, ?try-error?)
>       # a bunch of code
>
>     I though it would be useful to have a function that does this
> naturally, so I came up with the attached function.
>
>     I would be glad to hear your insights and if you think it would make
> sense to add this function to R.
>
>     Best regards,
>     Ernest
>     ______________________________________________
>     R-devel at r-project.org mailing list
>     https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

	[[alternative HTML version deleted]]



[Rd] [tryExcept] New try Function
Ernest Benedito