knitr::opts_chunk$set( comment = "#>", collapse = TRUE )
control how many times conditions are thrown
docs: https://ropensci.github.io/conditionz/
Package API:
cat(paste(" -", paste(sprintf("`%s`", getNamespaceExports("conditionz")), collapse = "\n - ")))
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:
remotes::install_github("ropensci/conditionz")
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.
x <- ConditionKeeper$new(times = 4) x x$get_id() x$add("one") x$add("two") x x$thrown_already("one") x$thrown_already("bears") x$not_thrown_yet("bears") x$add("two") x$add("two") x$add("two") x$thrown_times("two") x$thrown_enough("two") x$thrown_enough("one")
A simple function that throws messages
squared <- function(x) { stopifnot(is.numeric(x)) y <- x^2 if (y > 20) message("woops, > than 20! check your numbers") return(y) } foo <- function(x) { vapply(x, function(z) squared(z), numeric(1)) } bar <- function(x, times = 1) { y <- ConditionKeeper$new(times = times) 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)
Using in ConditionKeeper
allows you to control how many messages
are thrown
bar(x = 1:10)
bar(1:10, times = 3)
definitely need to work on performance
library(microbenchmark) microbenchmark::microbenchmark( normal = suppressMessages(foo(1:10)), with_conditionz = suppressMessages(bar(1:10)), times = 100 )
conditionz
in R doing citation(package = 'conditionz')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.