R/check_param.R

Defines functions check_param_types check_param_names check_param

Documented in check_param check_param_names check_param_types

#' Check if the parameter is a valid parameter
#'
#' Calls \code{stop} if the parameter is invalid
#' @inheritParams default_params_doc
#' @return nothing
#' @seealso Use \link{create_param} to create a valid parameter
#' @examples
#' check_empty_beautier_folder()
#'
#' check_param(create_alpha_param())
#' check_param(create_beta_param())
#'
#' check_empty_beautier_folder()
#' @author Richèl J.C. Bilderbeek
#' @export
check_param <- function(param) {
  check_param_names(param)
  check_param_types(param)
}

#' Check if the \code{param} has the list elements
#' of a valid \code{param} object.
#'
#' Calls \code{stop} if an element is missing
#' @inheritParams default_params_doc
#' @return nothing
#' @seealso Use \link{create_param} to create a valid \code{param}
#' @author Richèl J.C. Bilderbeek
#' @export
check_param_names <- function(param) {
  list_element_names <- c(
    "name", "id", "value"
  )
  for (arg_name in list_element_names) {
    if (!arg_name %in% names(param)) {
      stop(
        "'", arg_name, "' must be an element of an 'param'. \n",
        "Tip: use 'create_param'"
      )
    }
  }
}

#' Check if the \code{param} has the list elements
#' of the right type for a valid \code{param} object.
#'
#' Calls \code{stop} if an element has the incorrect type
#' @inheritParams default_params_doc
#' @return nothing
#' @seealso Use \link{create_param} to create a valid \code{param}
#' @author Richèl J.C. Bilderbeek
#' @export
check_param_types <- function(param) {

  if (!param$name %in% get_param_names()) {
    stop("'param$name' must be a valid parameter name")
  }
  if (is_one_na(param$value)) {
    stop("'param$value' must not be NA")
  }
}
richelbilderbeek/beastscriptr documentation built on April 9, 2024, 2:28 p.m.