R/cdss_read_skill_assignment.R

Defines functions cdss_read_skill_assignment

Documented in cdss_read_skill_assignment

#' Read a skill assignment from file
#' 
#' @param filename Name of the file for ODS or XLSX files
#' @param taught Name of the sheet/file for taught skill assignments
#' @param required Name of the sheet/file for required skil assignments
#' @param sep Cell-separating character for CSV files
#' @param warnonly Are non-compliant SAs allowed? (default FALSE)
#' @param verbose Verbosity (default TRUE)
#' @returns Skill assignment ('cdss_sa' object)
#' 
#' Skill assignments are stored in a pair of tables, 'taught' and 
#' 'required'. For ODS and XLSX files, these must be sheets of **one** file;
#' CSV does not support multi-sheet file and, therefore, two files must 
#' be specified there.
#' 
#' For CSV files, the 'filename' parameter must be 'NULL', and the two 
#' filenames are specified in the 'taught' and 'required' parameters. For ODS
#' and XLSX, the 'filename' must be specified, and teh 'taught' and 'required'
#' parameters specify the names of the respective sheets within that file.
#' 
#' @importFrom tools file_ext
#' 
#' @export
cdss_read_skill_assignment <- function(filename = NULL,
                                       taught = "Taught",
                                       required = "Required",
                                       sep = ',',
                                       warnonly = FALSE,
                                       verbose = FALSE) {
  if (is.null(filename)) { # Probably CSV file
    if (tolower(file_ext(taught)) != "csv")
      stop("NULL filename is only allowed for CSV files.")
    if (tolower(file_ext(required)) != "csv")
      stop("NULL filename is only allowed for CSV files.")
    if (sep == ',') dec <- '.'
    else if (sep == ';') dec <- ','
    else stop("Unknown cell separator specified.")
    t <- read.csv(taught, sep=sep, dec=dec, fill=FALSE)
    r <- read.csv(required, sep=sep, dec=dec, fill=FALSE)
  }
  # Now we have either ODS or XLSX
  else if (tolower(file_ext(filename)) == "ods") {
    t <- as.data.frame(read_ods(path=filename, sheet=taught, progress=FALSE))
    r <- as.data.frame(read_ods(path=filename, sheet=required, progress=FALSE))
  } else if (tolower(file_ext(filename)) == "xlsx") {
    t <- read.xlsx(xlsxFile = filename, sheet = taught)
    r <- read.xlsx(xlsxFile = filename, sheet = required)
  } else stop("Unknown file extension.")
  sa <- cdss_tables2sa(t, r)
  if (verbose) {
    print(t)
    print(r)
    print(sa)
  }
  sa$taught <- sa$taught[,sort(colnames(sa$taught))]
  sa$required <- sa$required[,sort(colnames(sa$required))]
  if (verbose) {
    print("After sorting:")
    print(sa)
  }
  check <- cdss_sa_compliance(sa, verbose)
  if (!check) {
    if (warnonly) {
      print("The assignment tables are not skill assignment compliant!")
    } else {
      stop("The assignment tables are not skill assignment compliant!")
    }
  }
  return(sa)
}

Try the CDSS package in your browser

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

CDSS documentation built on Sept. 7, 2026, 9:08 a.m.