R/summarize_usage.R

Defines functions summarize_usage

Documented in summarize_usage

#' Summarize Usage
#'
#' Summarize usage from daily_work sheet.
#'
#' @param usage Data frame of usage.
#' @param proj Data frame of projects.
#' @export

summarize_usage <- function(usage, proj){
  # metacharacters: . \ | ( ) [ { ^ $ * + ?
  stopifnot(nrow(usage)>0, !is.na(usage$proj), !grepl(":|;|/", usage$proj),
            length( grep('^ ', usage[,1], value = TRUE) )==0)
  wh.na <- is.na(usage$hrs.charge)
  if (any(wh.na)) usage$hrs.charge[wh.na] <- 0
  stopifnot(usage[!(usage$proj %in% proj$project), "hrs.charge"] == 0)

  # use which b/c NAs
  usage <- usage[which(usage$hrs.charge > 0),]
  # get rid of rows for commented out time
  usage <- usage[usage$proj!="",]
  # summarize
  us.total <- tapply(X=usage$hrs.charge, INDEX=tolower(usage$proj), FUN=sum, na.rm=TRUE)

  matched.ind <- which(rownames(us.total) %in% tolower(proj$project))
  if (!all(matched.ind %in% 1:nrow(us.total))){
    stop("Projects", paste(rownames(us.total)[-matched.ind], collapse=", "), "in dw not matched in proj.", call. = FALSE)
  }
  us.ss <- us.total[matched.ind]
  # create table
  us.tab <- data.frame(proj[match(names(us.ss), tolower(proj$project)), c("Lab", "Cortex_user", "Lawson")],
                       Project=names(us.ss), hrs.charge=round(us.ss, digits=2), stringsAsFactors = FALSE)

  # us.tab <- us.tab[, setdiff(colnames(us.tab), c("hrs.est", "Charge"))]
  # sum(us.tab$hrs.charge * 100)

  us.tab <- us.tab[order(-us.tab$hrs.charge),]
  us.tab
}
jdreyf/jdcbill documentation built on Nov. 4, 2019, 2:35 p.m.