R/importOABFiles.R

Defines functions importOABFiles

Documented in importOABFiles

#' Import the SIRENO on board reports from ICES project.
#'
#' This function import the on board report files of ICES project from SIRENO:
#' hauls, trips, catches, lengths, litter and accidental files.
#'
#' @param trips vector with the names of the haul files.
#' @param hauls vector with the names of the trip files.
#' @param catches vector with the names of the catch file names.
#' @param lengths vector with the names of the length file names.
#' @param litter vector with the names of the litter file names.
#' @param accidentals vector with the names of the accidental file names.
#' @param path path of the files. The working directory by default.
#' @param export to export reports data frame in csv file. False by default.
#' @note The SIRENO catches report must be generated with the option 'by
#' category'. The SIRENO hauls report must be generated with the
#' option 'only hauls'. The SIRENO Lengths report must be generated by categories.
#' @return Return a list with 6 data frames.
#' @export

importOABFiles <- function(trips, hauls, catches, lengths, litter,
                           accidentals, export = FALSE, path = getwd()){


  # check vector of mareas, lances, capturas and tallas has the same number of rows
  elements <- list(trips, hauls, catches, lengths, litter, accidentals)

  number_of_elements <- lapply(elements, nrow)
  if(length(unique(number_of_elements)) != 1){
    stop(paste0("the variables", trips, ", ", hauls, ", ", catches, ",",
                lengths, ",", litter, ",", accidentals,
                "does not have the same length."))
  }

  # create a list of functions
  df_functions <- list(   "trips" = importOABTrips,
                          "hauls" = importOABHauls,
                          "catches" = importOABCatches,
                          "lengths" = importOABLengths,
                          "litter" = importOABLitter,
                          "accidentals" = importOABAccidentals)

  # whit this apply, for every function of df_functions, execute it inside a
  # tryCatch returning the result of the function or the error thrown.
  samples_oab <- lapply(seq(1:length(df_functions)), function(x, y){

    name_of_df <- names(y)[x]

    df_data <- tryCatch(
      y[[x]](get(name_of_df), path),
      error = function(e){
        warning(e)
      }
    )

  }, df_functions)

  names(samples_oab) <- names(df_functions)

  samples_oab <- lapply(samples_oab, function(x){
    if(exists("message", where = x)){
      return(x[["message"]])
    } else {
      return(x)
    }
  })

  if (isTRUE(export)){
    exportListToCsv(samples_oab)
  }

  #return list
  return(samples_oab)
}
Eucrow/sapmuebase documentation built on April 17, 2024, 10:29 a.m.