R/skeleton.R

#' The Main Skeleton Build Function
#'
#' Generate R analysis scripts prefilled with database queries, data processing,
#'   plotting functions, and plot saving.  When finished either source the .R file,
#'   set the function to source it automatically, or edit the plotting functions
#'   for the specific details of the analysis.
#'
#' A total list of available plot functions arein the See Also section under
#'   other skeletonPlots.
#'
#' @param filename The resulting name of the .R script
#' @param csid The CSID(s) to process in the script
#' @param plots List of plots to include
#' @param run Whether to source the resulting analysis script, defaults to FALSE
#' @param username Database username, password defaults to a .pgpass password (highly recommended)
#' @param tex TRUE to compile a Beamer presentation of the generated figures, defaults to FALSE
#' @export
#' @return Writes the analysis script of csid to make plots to the file filename
#' @details Plot functions passed to skeleton need to take no input and output a
#' list of named character vectors for data, plot, etc.
#' @family skeletonPlots
#' @examples
#' ## Make a few basic plots for a scout of Edimburgh and run resulting script
#' skeleton("edinburgh", 3138,
#'          list(downloadTimeSeries, uploadTimeSeries,
#'               downloadBoxPlot, uploadBoxPlot,
#'               twilioFailureMap, m2mFailureMap), run = TRUE)
#'
#' ## Make a full report of Edinburgh and compile it
#' skeleton("edinburgh", 3138,
#'          plotUnitTest(), run = TRUE, tex = TRUE)
skeleton <- function(filename = "analysis.R", csid, plots,
                     run = FALSE, username = NULL, tex = FALSE) {

    ## Set some environmental variables for TeX integration (these should match templateHeader())
    saveTag <- paste(csid, sep = "", collapse = "_")
    rootName <- strsplit(filename, '[.]')[[1]][1]
    cacheDir <- sprintf("cache/%s/", rootName)
    figureDir <- sprintf("figures/%s/", rootName)
    texName <- paste('reports/auto_', rootName, saveTag, '.tex', sep = "")

    ## Open File Connection

    fileCon <- file(filename, "w")

    ## Extract out text

    dataText <- unlist(lapply(plots, function(x){x()[['data']]}))
    dataText <- unique(dataText)

    processText <- unlist(lapply(plots, function(x){x()[['process']]}))
    processText <- unique(processText)

    plotText <- unlist(lapply(plots, function(x){x()[['plot']]}))

    saveText <- unlist(lapply(plots, function(x){x()[['save']]}))
    saveText <- vapply(saveText, function(x){sprintf("%s%s_%s.png", figureDir, x, saveTag)}, character(1), USE.NAMES = FALSE)

    ## Change any NULL strings to empty characters
    if (is.null(dataText))    dataText <- ""
    if (is.null(processText)) processText <- ""
    if (is.null(plotText))    plotText <- ""
    if (is.null(saveText))    saveText <- ""

    ## Write header

    header <- templateHeader(csid, username, cacheDir, figureDir)

    writeLines(header, fileCon)

    ## Write data

    writeLines(dataHeader(), fileCon)
    writeLines(sort(dataText), fileCon)

    ## Write processing

    writeLines(processHeader(), fileCon)
    writeLines(processText, fileCon)

    ## Write plots

    writeLines(plotHeader(), fileCon)
    writeLines(plotText, fileCon)

    ## Finish Writing

    close(fileCon)

    ## Run file

    if (run) source(filename)

    ## Compile TeX

    if (tex) {
        dir.create('reports', showWarnings = FALSE)
        makeTeX(texName, saveText)
        compile(texName)
    }

}
mlhutchins/fast-analytics documentation built on May 23, 2019, 2:10 a.m.