R/utils-canada-load-sample-data.R

Defines functions canada_load_sample_data

Documented in canada_load_sample_data

#' Load the Canadian commercial sample data
#'
#' @description
#' Load the Canadian commercial sample data from an RDS file generated by
#' [canada_extract_samples_from_db()]. The filename is stored in the package
#' data variable [can_sample_data_rds_fn]
#'
#' @details
#' The following filtering is performed in this function for all fleets:
#' 1) Areas - BC offshore areas + Strait of Juan de Fuca
#' 2) Fishing gear - Midwater trawl only by default
#'
#' @param fn The file name for the sample RDS file
#' @param gear_type A vector of fishing gear type (description). Must be
#' one or more of: `midwater trawl`, `bottom trawl`, `shrimp trawl`,
#' or `unknown trawl`. If `NULL`, all type will be included
#'
#' @return A data frame containing filtered Canadian samples
#' @export
canada_load_sample_data <- function(
    fn = file.path("/srv/hake/other/samples/canada", can_sample_data_rds_fn),
    gear_type = "midwater trawl"){

  if(!file.exists(fn)){
    stop("The file:\n`", fn, "`\ndoes not exist. Run ",
         "`canada_extract_sample_data()` to create it")
  }

  out <- readRDS(fn) |>
    filter(major_stat_area_code %in% can_major_hake_areas |
             (major_stat_area_code == "01" &
                minor_stat_area_code == "20"))

  if(!is.null(gear_type)){
    gear_type <- toupper(gear_type)
    gear_pat <- paste(gear_type, collapse = "|")

    out <- out %>%
      filter(grepl(gear_pat, .$gear_desc))
  }

  out
}
pacific-hake/hake-assessment documentation built on July 21, 2024, 8:19 a.m.