R/chk-pars.R

Defines functions vld_pars chk_pars

Documented in chk_pars vld_pars

#' Check Parameter Names
#'
#' Checks if valid parameter names.
#'
#' The character vector must consist of values that start with an alpha
#' and only include alphanumeric characters and '_' or '.'.
#'
#' Missing values and duplicates are permitted.
#'
#' @inheritParams params
#' @inheritParams chk::chk_flag
#' @return
#' The `chk_` function throws an informative error if the test fails.
#'
#' The `vld_` function returns a flag indicating whether the test was met.
#' @export
#' @examples
#' x <- c("x", "a1._", "X")
#' chk_pars(x)
#' y <- c("x[1]", "a1", "a1", "._0")
#' try(chk_pars(y))
chk_pars <- function(x, x_name = NULL) {
  if (vld_pars(x)) {
    return(invisible())
  }
  if (is.null(x_name)) x_name <- deparse_backtick_chk(substitute(x))

  chk_s3_class(x, "character", x_name = x_name)
  chk_match(x, p0("^", par_pattern(), "$"), x_name = x_name)
}

#' @describeIn chk_pars Validate Parameter Names
#'
#' @export
#' @examples
#' vld_pars(c("x", "a1._", "X"))
#' vld_pars(c("x[1]", "a1", "a1", "._0"))
vld_pars <- function(x) {
  vld_s3_class(x, "character") &&
    vld_match(x, p0("^", par_pattern(), "$"))
}

Try the extras package in your browser

Any scripts or data that you put into this service are public.

extras documentation built on May 31, 2023, 6:22 p.m.