conditionz

knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE
)

Project Status: Active – The project has reached a stable, usable state and is being actively developed. R-check codecov.io rstudio mirror downloads cran version

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:

Installation

The CRAN version:

install.packages("conditionz")

Or the development version:

remotes::install_github("ropensci/conditionz")
library("conditionz")

ConditionKeeper

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")

basic usage

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)

benchmark

definitely need to work on performance

library(microbenchmark)
microbenchmark::microbenchmark(
  normal = suppressMessages(foo(1:10)),
  with_conditionz = suppressMessages(bar(1:10)),
  times = 100
)

Meta

rofooter



ropenscilabs/conditionz documentation built on Sept. 9, 2022, 11:56 p.m.