Warnings instead of errors from assert_that ()?

I am using the R package assertthat and want to (temporarily) display a warning instead of an error on an assertion failure. What's the easiest way to do this with the assertthat package?

I understand that not wanting warnings, not errors, goes against what assumptions should be used. In the long run, we really want to output errors on assertion failure. In the short term, we still want the code to function even with bad input, because output with bad inputs is still "good enough" for now.

Simple example: suppose I have a function that takes x as input and outputs x + 5. I want to print a warning if x! = 3. Since we will be using assert_that ultimately, it would be nice if we could use assertthat for warning.

In the long run, we will use this:

> x <- 3
> fn <- function(x) {assert_that(x==3); return(x+5)}
> fn(3)
[1] 8
> fn(4)
 Error: x not equal to 3 

      

In the short term, here's the best I've got so far:

> fn <- function(x) {if(!see_if(x==3)) warning(validate_that(x==3)); return(x+5)}
> fn(3)
[1] 8
> fn(4)
[1] 9
Warning message:
In fn(4) : x not equal to 3

      

I'm looking for a more concise solution if possible (the best example is to pass the output_warning parameter to assert_that, but I don't think it exists).

+4


source to share


3 answers


I created a custom function that accepts a string that matches the expression you would like to run against validate_that()

(ultimately assert_that()

). The function displays a warning if the assertion fails and remains silent at the same time. See below for use. You can easily extend this custom function to accept more than one expression as needed. Note that I'm also using sys.calls()

to get the name of the function that called this helper function. This is important information, so you can relate your warnings to the code that generated them.

assert_that_soft <- function(exp) {
                        if (!exp) {
                            print (paste("Error in function:",
                                   parse(sys.calls()[[sys.nframe()-1]])) ) # name of caller
                        }
                    }

      



Application:

> fn <- function(x) { assert_that_soft(x==3); return(x+5) }
> fn(3)
[1] 8
> fn(8)
[1] "Error in function: fn(8)"
[1] 13

      

+3


source


I think the easiest way to rewrite this function is to copy most of the function assert_that

as it is, and call a new function with the same name so that you don't have to change all the code when you enter error mode.



assert_that <- function(..., env=parent.frame()) {
    res <- see_if(..., env=env)
    if (res)
        return(TRUE)
    warning(attr(res, "msg"))
    TRUE
}

fn <- function(x) { assert_that(x==3); return(x+5) }
fn(3)
# [1] 8
fn(8)
# [1] 13
# Warning message:
# In assert_that(x == 3) : x not equal to 3

      

+1


source


Another option is to wrap assert_that in tryCatch.

fn <- function(x) tryCatch(assert_that(x == 3), error = function(e) warning(e), finally = return(x+5))
fn(3)
# [1] 8
fn(8)
# [1] 13
# Warning message:
# x not equal to 3

      

0


source







All Articles