camr_assert: Pipe-friendly assertions

camr_assertR Documentation

Pipe-friendly assertions

Description

[Experimental]

Usage

camr_assert(any_x, lgl_expr, chr_message)

Arguments

any_x

An object, preferably a dataframe, to pass through.

lgl_expr

A logical expression to evaluate within any_x.

chr_message

A message to be displayed by stop() if lgl_expr is false.

Details

The purpose of this function is to enable pipeable assertions on dataframe columns without the need to break the pipe-chain or to use inline functions.

For example... ⁠df_data |> (function (df) {if (!all(str_detect(df$x, '^a'))) stop('Elements of col x must start with "a".'); df})() |> ...⁠

Can be written more readably... ⁠df_data |> camr_assert(all(str_detect(x, '^a')), 'Elements of col x must start with "a".') |> ...⁠

If the assertion passes, the object being tested will be passed through unchanged.

Value

any_x

Examples

iris |>
  camr_assert(
    any(Species =='versicolor'),
    'Dataset contains no irises of species versicolor'
  ) |>
  summarize(
   m_length = mean(Petal.Length)
  )

rettopnivek/camrprojects documentation built on Dec. 20, 2024, 10:17 p.m.