# Core functionality

The package is quite small and contains the core function sanityTracker::add_sanity_check and a few convenience functions (use the prefix sc_) that basically call sanityTracker::add_sanity_check. Some of the convenience functions like sanityTracker::sc_left_join perform more than one check, other can check multiple columns at the same time like sanityTracker::sc_cols_non_NA.

The most helpful feature is that no matter how deep your sanity check is buried in your source code, sanityTracker will centralize the results AND if the defined check fails a few examples of the failed rows are stored for investigation.

The functions are more or less self explanatory, therefore we focus on the stored results and examples.

library(sanityTracker)
fail_vec = mtcars$mpg > 30, description = "mpg should be below 30" ) get_sanity_checks() #> description additional_desc data_name n n_fail n_na counter_meas #> 1: mpg should be below 30 - 32 4 0 - #> fail_vec_str param_name call #> 1: mtcars$mpg > 30          - eval(expr, envir, enclos)

We see that from the 32 observations contained in mtcars 4 observations have a mpg above 30. It also tracked how we actually performed the check in the column fail_vec_str. Usually, if failures happen, the next step is to actually investigate those cases. For this purpose that package offers the parameter data:

fail_vec = mtcars$mpg > 30, description = "mpg should be below 30. extract example ", data = mtcars, param_name = "mpg" ) get_sanity_checks() #> description additional_desc data_name n n_fail #> 1: mpg should be below 30 - 32 4 #> 2: mpg should be below 30. extract example - mtcars 32 4 #> n_na counter_meas fail_vec_str param_name call #> 1: 0 - mtcars$mpg > 30          - eval(expr, envir, enclos)
#> 2:    0            - mtcars$mpg > 30 mpg eval(expr, envir, enclos) #> example #> 1: #> 2: <data.frame> First note that we now see two lines where the first one is from our initial sanity check. Furthermore, the second line shows now that the column example is not empty: get_sanity_checks()[["example"]][[2]] #> mpg cyl disp hp drat wt qsec vs am gear carb #> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 #> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 #> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 If you call the sanity check from within a function, the results also appear in the global list of all sanity checks and the table shows the function call where the check happened. g <- function(x) { sanityTracker::add_sanity_check( fail_vec = x$mpg > 30,
description = "mpg should be below 30. check in function",
data = x,
param_name = "mpg"
)
}
f <- function(x) {g(x = x)}
dummy <- f(x = mtcars)
get_sanity_checks()
#> 1:                    mpg should be below 30               -           32
#> 2:  mpg should be below 30. extract example                -    mtcars 32
#> 3: mpg should be below 30. check in function               -         x 32
#>    n_fail n_na counter_meas    fail_vec_str param_name
#> 1:      4    0            - mtcars$mpg > 30 - #> 2: 4 0 - mtcars$mpg > 30        mpg
#> 3:      4    0            -      x\$mpg > 30        mpg
#>                         call      example
#> 1: eval(expr, envir, enclos)
#> 2: eval(expr, envir, enclos) <data.frame>
#> 3:                  g(x = x) <data.frame>

The function sanityTracker::clear_sanity_checks discards all sanity checks that are currently stored.

sanityTracker::clear_sanity_checks()
sanityTracker::get_sanity_checks()
#> NULL