R/as_grade_list.R

##' constructor function for a grade object
##' @param x data frame that contains a vector of names (or other IDs) and a vector of grades
##' @param id.col column that contains unique identifiers for students (e.g. last name)
##' @param raw.grades.col column that contains raw grades
##' @param adj.grades.col column that contains adjusted grades (this will usually be left as the default NULL)
##' @export
as_grade_list <- function(x, id.col=lastname,
                          raw.grade.col=raw.grade,
                          adj.grade.col = NULL) {

  ###
  # NEED TO MAKE GRADE LIST INHERIT FROM DATA FRAME
  ###

  # No idea how to test the object for the presence of the specified columns
  # # x must be a data frame with columns including id and raw.grades
  # if(!is.data.frame(x)) {
  #   stop("x must be a data frame")
  # }
  # if(sum(c(id, raw.grade) %in% names(x)) != 2) {
  #   stop(paste0("x must contain columns named ", id, " and ", raw.grade))
  # }

  # if it passes these tests then I guess it'll work
  class(x) <- c("grade_list", "data.frame")

  # This is not the right way to set objects in functions, but such is life
  attr(x, "id.col") <- id.col
  attr(x, "raw.grades.col") <- raw.grade.col

  # I can't think of a reason to define an adjusted grades column when the object is read in,
  #    but I'm putting this as a reminder for when I set these attributes properly
  if(!is.null(adj.grade.col)) {
    attr(x, "adj.grades.col") <- adj.grade.col
  } else {
    attr(x, "adj.grades.col") <- NULL
  }


  x
}
adsteen/gradr documentation built on May 10, 2019, 7:26 a.m.