# Packages
library(tidyverse)
library(knitr)
library(Hmisc)
library(knitr)
library(RColorBrewer)
library(extrafont)
library(kableExtra)
library(sfi)
library(ggplot2)
library(grid)
loadfonts()
## Global options
options(max.print="75")
opts_chunk$set(echo=FALSE,
            cache=FALSE,
              prompt=FALSE,
              tidy=TRUE,
              comment=NA,
              message=FALSE,
              warning=FALSE,
              dpi = 300,
              # dev = "cairo_pdf",
            fig.width = 7.25,
            fig.height = 4,
              dev = c("png", "cairo_pdf"),
              fig.pos="!h",
              fig.path = 'figures/')
opts_knit$set(width=75)
options(xtable.comment = FALSE)
plots_dict_plots <- plots_dict %>% filter(!table)
plots_dict_plots <- plots_dict_plots %>%
  group_by(author) %>%
  mutate(figure_first = dplyr::first(figure) == figure) %>%
  ungroup
chunks <- c()

make_plot_chunk <- function(author,
                            figure, 
                            plot_list){

  out <- c()
  for(i in 1:length(plot_list)){
    version <- i
    out[[i]] <- 
            paste0('\\newpage\n\n(',
             Hmisc::capitalize(author),
             ' ',
             figure, ')\n\n```{r, ', 
      tolower(author), '_figure_',
      figure, '_version_',
      version,
      '}\n',
      'sfi_plot(author = "',
      author,
      '", figure = "', 
      figure,
      '")[[',
      version,
      ']]\n```\n\n'
      )
  }  
  out <- paste0(unlist(out),collapse = '\n')
  return(out)
}
counter <- 0
for(i in 1:nrow(plots_dict_plots)){
  message(i)
  this_row <- plots_dict_plots[i,]
  this_author <- this_row$author
  this_figure <- this_row$figure
  this_function <- paste0('sfi_plot_',
                               this_author, 
                               '_',
                               this_figure)
  if(exists(this_function)){
    counter <- counter +1
    this_plot_list <- get(this_function)
  is_first <- this_row$figure_first
  result <- 
    paste0(
      # Author name
      ifelse(is_first,
           paste0('\\newpage\n\n\\begin{center}\n\\begin{Huge}\n',
                  Hmisc::capitalize(this_author),
                  '\n\\end{Huge}\n\\end{center}\n\n## ',
      Hmisc::capitalize(this_author), 
      '\n\n',
      collapse = NULL),
      ''),
      # Figure number
      '\n\n### ', Hmisc::capitalize(this_author), ' Figure ', gsub('_', '.', this_figure),
      '\n\n')
      # Actual plots
    out <- make_plot_chunk(author = this_author,
                      figure = this_figure,
                      plot_list = this_plot_list())
    out <- paste0(result, out, collapse = '\n')
  }
  chunks[counter] <- out
}
chunks <- paste0(chunks, collapse = '\n')



chunks <- paste0('# Plots\n\n', chunks, collapse = '')

file_connection <- file('plots.Rmd')
writeLines(paste0('---\noutput: pdf_document\n---\n\n', 
                  chunks), 
           file_connection)
close(file_connection)
plots_dict_tables <- plots_dict %>% filter(table)
plots_dict_tables <- plots_dict_tables %>%
  group_by(author) %>%
  mutate(figure_first = dplyr::first(figure) == figure) %>%
  ungroup
chunks <- c()

make_plot_chunk <- function(author,
                            figure, 
                            plot_list){

  out <- c()
  for(i in 1:length(plot_list)){
    version <- i
    out[[i]] <- 
      paste0('\\newpage\n\n(',
             Hmisc::capitalize(author),
             ' ',
             figure, ')\n\n```{r, ', 
      tolower(author), '_table_',
      figure, '_version_',
      version,
      ', results = "asis"}\n',
      'sfi_plot(author = "',
      author,
      '", figure = "', 
      figure,
      '")[[',
      version,
      ']]\n```\n\n'
      )
  }  
  out <- paste0(unlist(out),collapse = '\n')
  return(out)
}
counter <- 0
for(i in 1:nrow(plots_dict_tables)){
  this_row <- plots_dict_tables[i,]
  this_author <- this_row$author
  this_figure <- this_row$figure
  this_function <- paste0('sfi_plot_',
                               this_author, 
                               '_',
                               this_figure)
  if(exists(this_function)){
    counter <- counter +1
    this_plot_list <- get(this_function)
  is_first <- this_row$figure_first
  result <- 
    paste0(
      # Author name
      ifelse(is_first,
           paste0('\\newpage\n\n\\begin{center}\n\\begin{Huge}\n',
                  Hmisc::capitalize(this_author),
                  '\n\\end{Huge}\n\\end{center}\n\n## ',
      Hmisc::capitalize(this_author), 
      '\n\n',
      collapse = NULL),
      ''),
      # Figure number
      '\n\n### ', Hmisc::capitalize(this_author), ' Table ', gsub('_', '.', this_figure),
      '\n\n')
      # Actual plots
      out <- make_plot_chunk(author = this_author,
                      figure = this_figure,
                      plot_list = this_plot_list())
      out <- paste0(result, out, collapse = '\n')
  }
  chunks[counter] <- out
}
chunks <- paste0(chunks, collapse = '\n')


chunks <- paste0('\\newpage# Tables\n\n', chunks, collapse = '')

file_connection <- file('tables.Rmd')
writeLines(paste0('---\noutput: pdf_document\n---\n\n', 
                  chunks), 
           file_connection)
close(file_connection)

\newpage \tableofcontents \newpage



file.remove('plots.Rmd')
file.remove('tables.Rmd')


databrew/sfi documentation built on May 29, 2019, 1:52 a.m.