R/linevis.R

#' Loads commit stats (as generated by cloc) into a data frame, along with the commit ref and timestamp.
#' @export
#' @param path the path to the commit line count statistics. See linecount.sh
#' @return data.frame of all commit line count stats
#' @export
#' @examples
#' commit_stats <- load_commit_stats()
#' summary(commit_stats)
load_commit_stats <- function(path) {

  commits <- read.csv(file.path(path, 'commits.csv'), header=FALSE, col.names=c('ref','timestamp'))

  dat <- adply(
    .data=commits,
    .margins=1,
    .fun=function(c) {
      f <- sprintf(file.path(path, '%s.csv'), c$ref)
      if(file.exists(f)) {
        d <- read.csv(f)
        d <- d[grep("http", names(d), value=TRUE, invert=TRUE)] # Exclude "trash" columns
        d$ref <- c$ref
        d$timestamp <- as.POSIXct(c$timestamp)
        d
      }
    }
  )

  dat$lines_of_code_per_file <- dat$code/dat$files
  dat

}


#' Plots the variable for each language at each commit time.
#' @export
#' @param commit_stats
#' @param variable the variable to plot
#' @param title the plot title
#' @return ggplot object
plot_commit_stats<- function(commit_stats, variable, title) {
  ggplot(commit_stats, aes_string(x = 'timestamp', y = variable, colour = 'language')) +
    labs(title = title) +
    theme(
      legend.key = element_blank(),
      legend.text = element_text(hjust=0, size=20),
      legend.title = element_text(hjust=0, size=20)
    )
}

#' Plots the variable as an area chart vs. Time + languages.
#' @export
#' @param commit_stats
#' @param variable the variable to plot
#' @param title the plot title
#' @return ggplot object
plot_commit_stats_area <- function(commit_stats, variable, title)
    plot_commit_stats(commit_stats, variable, title) +
      geom_area(aes(fill = language), position = 'stack')

#' Plots the variable as a line chart vs. Time + languages.
#' @export
#' @param commit_stats
#' @param variable the variable to plot
#' @param title the plot title
#' @return ggplot object
plot_commit_stats_line <- function(commit_stats, variable, title)
    plot_commit_stats(commit_stats, variable, title) +
      geom_line(aes(colour = language), size=2)

#' Plot all to PNG.
#' @export
#' @param commit_stats
plot_all_to_PNG <- function(commit_stats, path) {
  plot_to_png <- function(variable, title, FUN) {
    png(file.path(path, sprintf('commit_%s.png', variable)), height=900, width=1600)
    print(FUN(commit_stats, variable, title))
    dev.off()
  }
  plot_to_png('code', 'Lines of code over time, by language', FUN=plot_commit_stats_area)
  plot_to_png('files', 'Number of files over time, by language', FUN=plot_commit_stats_line)
  plot_to_png('lines_of_code_per_file', 'Lines of code per file over time, by language', FUN=plot_commit_stats_line)
}
yoni/git_history_visualizer documentation built on May 4, 2019, 5:31 p.m.