control how many times conditions are thrown
Package API:
handle_messages
handle_conditions
ConditionKeeper
handle_warnings
capture_message
capture_warning
Use cases for conditionz
functions:
ConditionKeeper
is what you want to use if you want to
keep track of conditions inside a function being applied many times,
either in a for loop or lapply style fashion.handle_conditions
/handle_messages
/handle_warnings
is what you want to use if the multiple conditions are happening within
a single function or code blockcapture_message
/capture_warning
are meant
for capturing messages/warnings into a useable listThe CRAN version:
install.packages("conditionz")
Or the development version:
install.packages("devtools")
::install_github("ropenscilabs/conditionz") devtools
library("conditionz")
ConditionKeeper
is the internal R6 class that handles
keeping track of conditions and lets us determine if conditions have
been encountered, how many times, etc.
<- ConditionKeeper$new(times = 4)
x
x#> ConditionKeeper
#> id: 6824b49c-be8f-4ba1-9acc-47115b6bccbb
#> times: 4
#> messages: 0
$get_id()
x#> [1] "6824b49c-be8f-4ba1-9acc-47115b6bccbb"
$add("one")
x$add("two")
x
x#> ConditionKeeper
#> id: 6824b49c-be8f-4ba1-9acc-47115b6bccbb
#> times: 4
#> messages: 2
#> one two
$thrown_already("one")
x#> [1] TRUE
$thrown_already("bears")
x#> [1] FALSE
$not_thrown_yet("bears")
x#> [1] TRUE
$add("two")
x$add("two")
x$add("two")
x$thrown_times("two")
x#> [1] 4
$thrown_enough("two")
x#> [1] TRUE
$thrown_enough("one")
x#> [1] FALSE
A simple function that throws messages
<- function(x) {
squared stopifnot(is.numeric(x))
<- x^2
y if (y > 20) message("woops, > than 20! check your numbers")
return(y)
}<- function(x) {
foo vapply(x, function(z) squared(z), numeric(1))
}<- function(x, times = 1) {
bar <- ConditionKeeper$new(times = times)
y on.exit(y$purge())
vapply(x, function(z) y$handle_conditions(squared(z)), numeric(1))
}
Running the function normally throws many messages
foo(1:10)
#> woops, > than 20! check your numbers
#> woops, > than 20! check your numbers
#> woops, > than 20! check your numbers
#> woops, > than 20! check your numbers
#> woops, > than 20! check your numbers
#> woops, > than 20! check your numbers
#> [1] 1 4 9 16 25 36 49 64 81 100
Using in ConditionKeeper
allows you to control how many
messages are thrown
bar(x = 1:10)
#> woops, > than 20! check your numbers
#> [1] 1 4 9 16 25 36 49 64 81 100
bar(1:10, times = 3)
#> woops, > than 20! check your numbers
#>
#> woops, > than 20! check your numbers
#>
#> woops, > than 20! check your numbers
#> [1] 1 4 9 16 25 36 49 64 81 100
definitely need to work on performance
library(microbenchmark)
::microbenchmark(
microbenchmarknormal = suppressMessages(foo(1:10)),
with_conditionz = suppressMessages(bar(1:10)),
times = 100
)#> Unit: microseconds
#> expr min lq mean median uq max
#> normal 857.006 874.4165 989.1222 900.992 943.1775 2796.801
#> with_conditionz 1906.543 1947.7495 2078.4096 1998.475 2132.5750 2729.156
#> neval
#> 100
#> 100
conditionz
in R doing
citation(package = 'conditionz')