R/check_inputlist.R

Defines functions check_inputlist

Documented in check_inputlist

#' Check input argument `inputlist`
#'
#' Check the elements of the `inputlist` list used as an argument in
#' `SS_write()` function.
#'
#' @param inputlist List created by the `SS_read()` function with
#' elements "dat", "ctl", "start", "fore", and (optionally) "wtatage" and "par".
#' @author Kelli F. Johnson, Ian G. Taylor
#' @return Either TRUE if the input list is valid, or FALSE if not, with
#' a warning about which elements are missing.
#' @seealso [SS_write()]

check_inputlist <- function(inputlist) {
  # list of elements of inputlist
  elements <- c("dat", "ctl", "start", "fore")

  # check for non-empty list
  if (length(inputlist) == 0) {
    stop("inputlist is empty")
  }

  if (!any(elements %in% names(inputlist))) {
    stop("input does not look like a list created by SS_read()")
  }
  # check for whether wtatage is required and if so, add it to the
  # vector of elements
  if (inputlist[["ctl"]][["EmpiricalWAA"]]) {
    elements <- c(elements, "wtatage")
  }
  # check for whether par is required and if so, add it to the
  # vector of elements
  if (inputlist[["start"]][["init_values_src"]] == 1) {
    elements <- c(elements, "par")
  }

  missingnames <- elements[!elements %in% names(inputlist)]
  if (length(missingnames) > 0) {
    warning(
      "The following elements are missing from the input list: ",
      paste(missingnames, collapse = ", ")
    )
    return(FALSE)
  } else {
    return(TRUE)
  }
}
r4ss/r4ss documentation built on April 30, 2024, 4:42 a.m.