R/append.R

Defines functions appendize

Documented in appendize

#' Generates Latex Appendix
#'
#' Creates a LaTeX appendix of regression summaries and descriptive statistics.
#'   A directory containing all the relevant files
#'
#' @param ... Models generated by \code{build}, \code{describe}, ggplot objects,
#'  or ggplot objects wrapped in \code{\link{gg_tex}}.
#' @param dir Path to Latex file node where the appendix folder will be appended.
#'
#' @examples
#'
#' data('freeny')
#' mod <- lm(y ~ lag.quarterly.revenue + price.index + income.level +
#'             market.potential, data = freeny)
#' mod2 <- lm(y ~ lag.quarterly.revenue + price.index + income.level,
#'           data = freeny)
#'
#' tex1 <- build(mod, label='f1', silent=TRUE)
#' tex2 <- build(mod2, label='f2', silent=TRUE)
#' d <- paste(getwd(), 'rchitEx', sep='/')
#' append(list(tex1, tex2), d)
#' ##...Latex files outputted to dir... ##
#'
#' @export

appendize <- function(..., dir) {
  if (dir.exists(paste(dir, '/appendix', sep=''))) {
    stop('Directory already exists. Delete directory before creating new appendix.',
         call. = FALSE)
  }
  dir.create(paste(dir, '/appendix', sep=''))

  in_rchitex <- function(texOb) {
    iput <- paste0('\t\\input{appendix/', texOb$options$label, '.tex}\n', sep='')
    out <- table_wrap(iput, caption=texOb$options$caption,
                      label=texOb$options$label)
    if (texOb$options$landscape) out <- lan_wrap(out)
    paste0(out, '\\clearpage\n')
  }

  in_plot <- function(cap, label) {
    paste('\\begin{figure}[!htb]\n',
          '\t\\centering\n',
          '\t\\caption{', cap, '}\n',
          '\t\\label{', label, '}\n',
          '\t\\includegraphics[scale=1]{appendix/', label, '}\n',
          '\\end{figure}\n',
          '\\clearpage\n',
          sep = '')
  }

  appendix <- paste('\\clearpage\n\\section{Appendix}\n',
                    sep='\n')

  pth <- paste0(dir, '/appendix/')
  mods <- list(...)

  if ('ggplot' %in% unlist(lapply(mods,class))) {
    message('Figure found: `graphicx` package must be sourced in Latex preamble')
  }
  # saves the individual models
  for (m in mods) {
    if (class(m) == 'rcReg' || class(m) == 'rcTable') {
      writeLines(m$code, con = paste(dir, '/appendix/',
                                     m$options$label, '.tex', sep=''))
      out <- in_rchitex(m)
    } else if (class(m)[1] == 'gg' || class(m)[1] =='ggplot') {
      caption <- paste0('gg', sample(1000:2000,1))
      ggplot2::ggsave(m, filename=paste0(caption,'.png'), path=pth,
                      device='png')
      out <- in_plot(cap='Plot', label=caption)
    } else if (class(m)[1] == 'rchitex' && class(m)[2] == 'ggtex') {
      ggplot2::ggsave(m$code, filename=paste0(m$options$label, '.png'),
                      path=pth, device='png')
      out <- in_plot(cap=m$options$caption, label=m$options$label)
    }
    else out <- ''
    appendix <- paste0(appendix, out, collapse='', sep='\n')
    # removes final clear page
  }

  appendix <- substr(appendix, 1, nchar(appendix)-12)
  writeLines(appendix, con = paste(dir, '/appendix/appendix.tex', sep=''))
  message('Appendix successfully created. Make sure to input `appendix.tex`.')
}
bdempe18/rchitex documentation built on Nov. 9, 2020, 11:33 p.m.