R/separateLaTeXtableR.R

Defines functions separateLaTeXtableR

Documented in separateLaTeXtableR

#' Separated LaTeX Tables
#'
#' @param caption Caption produced by internal function titleR
#' @param tableNote Table note produced by internal function tableNoteR
#' @param mat Name of result summary matrix to look for in environment...Format: <outcome>_<analysis>_<model>
#' @param multiOutcome Multiple outcomes in same table? TRUE/FALSE
#'
#' @return
#' @export
separateLaTeXtableR <- function(caption, tableNote, mat,
                   multiOutcome = FALSE) {

  label <- paste0(outcomeVar, '_', analysis, '_', model) # label for LaTeX table referencing

  colspecs <- as.character(paste0(rep('l', dim(mat)[2] + 1)))

  syntax <- paste0('\\begin{table}
                   \\begin{threeparttable}
                   \\centering
                   \\caption{', caption, '}
                   \\label{tab:', label, '}', '
                   \\begin{tabular}{')
  syntax <- c(syntax, colspecs, '} \\toprule
              ')

  #columnHead <- paste0(dimnames(mat)[[2]], collapse = ' & ')

  syntax <- c(syntax,
              '
              & ',
              #columnHead,
              paste0(dimnames(mat)[[2]], collapse = ' & '), ' \\\\', ' \\midrule
              ', '')

  ## Body
  body <- rep(NA, dim(mat)[1])
  for (row in 1:dim(mat)[1]) {
    body[row] <- paste0(mat[row, 1:dim(mat)[2]], collapse = ' & ')
    body[row] <- paste0(dimnames(mat)[[1]][row], ' & ', body[row])
  }
  body <- paste0('
                 ', body[1:dim(mat)[1]], sep = ' \\\\ ')
  body <- c(body, ' \\midrule
            ')

  footer <- paste0('
                   \\end{tabular}
                   \\begin{tablenotes}
                   \\small
                   \\item \\textit{Note}. ', tableNote,
                   '
                   \\end{tablenotes}
                   \\end{threeparttable}
                   \\end{table}

                   \\newpage


                   ')

  cat(c(syntax, body, footer))

}
enaY15/TabulationAutomation documentation built on March 18, 2020, 8:35 p.m.