R/parse_daily_work.R

Defines functions parse_daily_work

Documented in parse_daily_work

#' Parse entries with projects and associated hours from daily work tab
#'
#' Parse entries with projects and associated hours from daily work tab.
#'
#' @param dw data frame from daily work tab with some columns holding projects and associated hours.
#' @param col.inds indices of columns holding projects and associated hours in `dw`.
#' @export

# usage summary
# i'm col.ind 2
# date is col 1
parse_daily_work <- function(dw, col.inds=2:ncol(dw)){
  usage <- NULL

  for (col.ind in col.inds){
    for (row.ind in 1:nrow(dw)){
      # remove spaces at beg or end of line
      x <- gsub("^ +| +$", "", dw[row.ind, col.ind])

      # remove comments between ' /' or '/' to ';' not greedily (so use ?)
      # 1. remove comments ending with ";"
      # 2. remove comments not ending with ";" but going to end of line
      x <- gsub("( *)\\/.*$", "",
                gsub("( *)\\/.*?;", ";", x))

      # split based on ';'
      proj.tmp <- unlist(strsplit(x=x, split=";( *)"))
      # get any ';' that may be left over, e.g. from comments in middle of line
      if (any(grepl(";", proj.tmp))) stop("Extra ';' found.")
      # proj.tmp <- gsub(";", "", proj.v)
      colon.ind <- grep(":", proj.tmp)
      if (length(colon.ind) >= 1){
        proj.tmp <- proj.tmp[colon.ind]
        # rm hours charged from proj name
        proj.nms <- gsub(":.+", "", proj.tmp)
        hrs.charge <- as.numeric(gsub(".+:", "", proj.tmp))
        if (!all(grepl(":", proj.tmp))) hrs.charge[!grepl(":", proj.tmp)] <- 0
        # usage.tmp <- data.frame(proj=proj.nms, hrs.est=7/length(proj.nms), hrs.charge=as.numeric(hrs.charge))
        usage.tmp <- data.frame(proj=proj.nms, hrs.charge=hrs.charge, stringsAsFactors = FALSE)
        usage <- rbind(usage, usage.tmp)
      }
    }
  }
  usage
}
jdreyf/jdcbill documentation built on Nov. 4, 2019, 2:35 p.m.