MyNixOS website logo
Description

Control How Many Times Conditions are Thrown.

Provides ability to control how many times in function calls conditions are thrown (shown to the user). Includes control of warnings and messages.

conditionz

Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. Build Status codecov.io rstudio mirror downloads

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 block
  • capture_message/capture_warning are meant for capturing messages/warnings into a useable list

Installation

The CRAN version:

install.packages("conditionz")

Or the development version:

install.packages("devtools")
devtools::install_github("ropenscilabs/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
#> ConditionKeeper
#>  id: 6824b49c-be8f-4ba1-9acc-47115b6bccbb
#>  times: 4
#>  messages: 0
x$get_id()
#> [1] "6824b49c-be8f-4ba1-9acc-47115b6bccbb"
x$add("one")
x$add("two")
x
#> ConditionKeeper
#>  id: 6824b49c-be8f-4ba1-9acc-47115b6bccbb
#>  times: 4
#>  messages: 2
#>   one  two
x$thrown_already("one")
#> [1] TRUE
x$thrown_already("bears")
#> [1] FALSE
x$not_thrown_yet("bears")
#> [1] TRUE

x$add("two")
x$add("two")
x$add("two")
x$thrown_times("two")
#> [1] 4
x$thrown_enough("two")
#> [1] TRUE
x$thrown_enough("one")
#> [1] FALSE

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

benchmark

definitely need to work on performance

library(microbenchmark)
microbenchmark::microbenchmark(
  normal = 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

Meta

  • Please report any issues or bugs.
  • License: MIT
  • Get citation information for conditionz in R doing citation(package = 'conditionz')
  • Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

rofooter

Metadata

Version

0.1.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows