Checking for a proper "stop" statement...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Checking for a proper "stop" statement...

Jonathan Greenberg-5
Folks:

Consider the following two use cases:

goodfunction <- function()
{
stop("Something went wrong..."
}

# vs.

badfunction <- function()
{
notgood()
}

Is there a way for me to test if the functions make use of a stop()
statement WITHOUT modifying the stop() output (assume I can't mod the
function containing the stop() statement itself)?  For "goodfunction" the
answer is TRUE, for "badfunction" the answer is FALSE.  Both return an
error, but only one does it "safely".

I thought the answer might lie in a tryCatch statement but I'm having a
hard time figuring out how to do this test.

--j
--
--
Jonathan A. Greenberg, PhD
Randall Endowed Professor and Associate Professor of Remote Sensing
Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
Natural Resources & Environmental Science
University of Nevada, Reno
1664 N Virginia St MS/0186
Reno, NV 89557
Phone: 415-763-5476
http://www.unr.edu/nres
Gchat: [hidden email], Skype: jgrn3007

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Checking for a proper "stop" statement...

Duncan Murdoch-2
On 21/02/2018 4:28 PM, Jonathan Greenberg wrote:

> Folks:
>
> Consider the following two use cases:
>
> goodfunction <- function()
> {
> stop("Something went wrong..."
> }
>
> # vs.
>
> badfunction <- function()
> {
> notgood()
> }
>
> Is there a way for me to test if the functions make use of a stop()
> statement WITHOUT modifying the stop() output (assume I can't mod the
> function containing the stop() statement itself)?  For "goodfunction" the
> answer is TRUE, for "badfunction" the answer is FALSE.  Both return an
> error, but only one does it "safely".
>
> I thought the answer might lie in a tryCatch statement but I'm having a
> hard time figuring out how to do this test.
>

I think tryCatch() is what you want.  To see the difference between
those two errors, run

tryCatch(goodfunction(), error = function(e) browser())

and similarly with badfunction().  When you land in the browser, you'll see

Browse[1]> str(e)
List of 2
  $ message: chr "Something went wrong..."
  $ call   : language goodfunction()
  - attr(*, "class")= chr [1:3] "simpleError" "error" "condition"

and

Browse[1]> str(e)
List of 2
  $ message: chr "could not find function \"notgood\""
  $ call   : language notgood()
  - attr(*, "class")= chr [1:3] "simpleError" "error" "condition"


so you could write a test based on the message, or based on the call to
see where the stop() happened.

Duncan Murdoch

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.