R/compile-template-points.R

#' Compile writing template reports
#'
#' Allows users to take a writing template report from the admin space and generate a summary, which will include total template points for each user.
#'
#' `compile_template_points` is a function that will take a writing template report and create a template points summary for each user in a course. MJ often uses
#' these when analyzing course performance and student completion.
#'
#' @param filename A character string. This is the name of the file containing the raw writing template report downloaded from the admin space. This should be
#'  an .xlsx file. No change is required after downloading this file from the admin space, although it may be wise to rename it something shorter and more intuitive.
#' @import dplyr
#' @import readxl
#' @export

compile_template_points <- function(filename){
  requireNamespace("readxl")
  requireNamespace("dplyr")

  full_filename <- paste0(filename, ".xlsx")

  n_sheets <- 1:length(readxl::excel_sheets(full_filename))

  template_reports <- lapply(n_sheets, function(x){
    readxl::read_xlsx(full_filename, sheet = x, col_names = TRUE) %>%
      dplyr::select(one_of(c("Student ID", "Final Saved Date")))
  })

  template_reports_with_cols <- lapply(n_sheets, function(x){
    col_name <- paste0("template_", x)
    template_reports[[x]][[col_name]] <- with(template_reports[[x]], ifelse(`Final Saved Date` == "Did Not Post Response", 0, 1))
    template_reports[[x]]
  })

  template_reports_with_cols <- lapply(n_sheets, function(x) template_reports_with_cols[[x]] %>% select(-`Final Saved Date`))

  combined <- Reduce(function(...) merge(..., by = "Student ID", all.x = TRUE), template_reports_with_cols)

  combined <- combined %>%
    mutate(template_points = rowSums(.[2:ncol(.)]))

  return(combined)
}
plduffy/rsoomo documentation built on May 26, 2019, 12:32 a.m.