R/params-prelude-generated.R

Defines functions apply_choice_rules apply_qtest_rules check_list_shape

Documented in apply_choice_rules apply_qtest_rules check_list_shape

# Generated by devforge. Do not edit.
# Edit the specs under inst/params/ and run devforge::forge_params().

# internal helpers ------------------------------------------------------------

#' Check that a value is a list with the required names
#'
#' @description Boilerplate guard at the top of every generated parameter
#' checker: verifies `x` is a list and that `required_names` are present in
#' `names(x)`.
#'
#' @param x The object to check.
#' @param required_names Character vector of names that must be present in
#' `names(x)`.
#' @param strict Boolean. `TRUE` additionally rejects names that are not in
#' `required_names`. Defaults to `FALSE`.
#'
#' @returns `TRUE` if the check was successful, otherwise a checkmate-style
#' error string.
#'
#' @keywords internal
check_list_shape <- function(x, required_names, strict = FALSE) {
  res <- checkmate::checkList(x)
  if (!isTRUE(res)) {
    return(res)
  }
  res <- checkmate::checkNames(names(x), must.include = required_names)
  if (!isTRUE(res) || !strict) {
    return(res)
  }
  extra <- setdiff(names(x), required_names)
  if (length(extra) > 0L) {
    return(sprintf(
      "Unexpected element(s): %s.",
      paste(sprintf("`%s`", extra), collapse = ", ")
    ))
  }
  TRUE
}

#' Apply qtest rules by name
#'
#' @description Validates the elements of a named list `x` against per-field
#' [checkmate::qtest()] patterns. Fields whose names are not in `rules` are
#' skipped. On failure, returns an error string naming the first offending
#' element and appending an optional `hint`.
#'
#' @param x Named list of parameters to validate.
#' @param rules Named list mapping field name to a qtest pattern (or vector of
#' patterns passed to `qtest`).
#' @param label Short human-readable label used in the error message
#' (e.g. `"GSEA params"`).
#' @param hint Optional string appended to the error message to describe the
#' expected types/ranges. Defaults to `NULL` (no hint).
#'
#' @returns `TRUE` if all checked fields pass, otherwise a string of the form
#' `` "The element `<field>` in <label> is invalid. <hint>" ``.
#'
#' @keywords internal
apply_qtest_rules <- function(x, rules, label, hint = NULL) {
  res <- purrr::imap_lgl(x, \(val, name) {
    if (name %in% names(rules)) {
      checkmate::qtest(val, rules[[name]])
    } else {
      TRUE
    }
  })
  if (all(res)) {
    return(TRUE)
  }
  broken <- names(res)[!res][1]
  msg <- sprintf("The element `%s` in %s is invalid.", broken, label)
  if (!is.null(hint)) {
    msg <- paste(msg, hint)
  }
  msg
}

#' Apply testChoice rules by name
#'
#' @description Validates the elements of a named list `x` against per-field
#' [checkmate::testChoice()] choice sets. Fields whose names are not in `rules`
#' are skipped. On failure, returns an error string naming the first offending
#' element and appending an optional `hint`.
#'
#' @param x Named list of parameters to validate.
#' @param rules Named list mapping field name to the character vector of
#' allowed choices.
#' @param label Short human-readable label used in the error message
#' (e.g. `"MELD params"`).
#' @param hint Optional string appended to the error message. Defaults to
#' `NULL` (no hint).
#'
#' @returns `TRUE` if all checked fields pass, otherwise a string naming the
#' first offending element.
#'
#' @keywords internal
apply_choice_rules <- function(x, rules, label, hint = NULL) {
  res <- purrr::imap_lgl(x, \(val, name) {
    if (name %in% names(rules)) {
      checkmate::testChoice(val, rules[[name]])
    } else {
      TRUE
    }
  })
  if (all(res)) {
    return(TRUE)
  }
  broken <- names(res)[!res][1]
  msg <- sprintf(
    "The element `%s` in %s is not one of the expected choices.",
    broken,
    label
  )
  if (!is.null(hint)) {
    msg <- paste(msg, hint)
  }
  msg
}

Try the manifoldsR package in your browser

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

manifoldsR documentation built on Nov. 17, 2026, 5:09 p.m.