collect_labels: Collect and append validation messages

View source: R/utils-validation.R

collect_labelsR Documentation

Collect and append validation messages

Description

Given a data frame containing the results of validation tests, this will append a column of labels that describes each failure.

Usage

collect_labels(VAL, cli = FALSE, msg = heading_tests)

Arguments

VAL

a data frame containing the results of tests

cli

indicator to use the cli package to format warnings

msg

a named vector of template messages to provide for each test formatted for the glue package.

See Also

throw_link_warnings() for details on how this is implemented.

Examples

# As an example, consider a data frame where you have observations in rows
# and the results of individual tests in columns:
set.seed(2023-11-16)
dat <- data.frame(
  name = letters[1:10],
  rank = sample(1:3, 10, replace = TRUE),
  A = sample(c(TRUE, FALSE), 10, replace = TRUE, prob = c(7, 3)),
  B = sample(c(TRUE, FALSE), 10, replace = TRUE, prob = c(7, 3)),
  C = sample(c(TRUE, FALSE), 10, replace = TRUE, prob = c(7, 3))
)
dat
# you can see what the results of the tests were, but it would be a good
# idea to have a lookup table describing what these results mean
dat_tests <- c(
  A = "[missing widget]: {name}",
  B = "[incorrect rank]: {rank}",
  C = "[something else]"
)
# collect_labels will create the output you need:
pb <- asNamespace("pegboard")
res <- pb$collect_labels(dat, msg = dat_tests)
res
writeLines(res$labels)
if (requireNamespace("cli", quietly = TRUE)) {
  # you can also specify cli to TRUE to format with CLI
  res <- pb$collect_labels(dat, cli = TRUE, msg = dat_tests)
  writeLines(res$labels)
}

carpentries/pegboard documentation built on Nov. 13, 2024, 8:53 a.m.