R/output.table-data-function.R

Output.table <- function(analysis_list){
  if(length(analysis_list) == 2){ #For lists of length 2
    w <- analysis_list[[1]] #Pull the first object, the Mantel test result
    x <- capture.output(cat(" \\\\ ")) #Store the escape characters separately to avoid an error
    y <- capture.output(cat(c(" Matrix.2 - Matrix.1", format(round(w$statistic,2), nsmall = 2), 
                              format(round(w$signif,2), nsmall = 2)), sep = " & ")) #Store the test results
    z <- capture.output(cat(y, x)) #Put the above together
    #The following assembles the LaTeX table code and inserts the relevant vector z
    cat(c("\\begin{tabular}{|p{3cm}||p{3cm}|p{3cm}|}"),
        c(" \\hline"),
        c(" \\multicolumn{3}{|c|}{Mantel Test} \\\\ "),
        c(" \\hline"),
        c(" Model & Test Statistic & P-Value \\\\ "),
        c(" \\hline"),
        z,
        c(" \\hline"),
        c("\\end{tabular}"), sep = "\n", fill = TRUE)
    #Put in some lines to separate this block of code from the next
    cat("\n \n \n")
    
    a <- analysis_list[[2]] #Pull the second object, the MRQAP test result
    b <- capture.output(cat(c(" Matrix.2 - Matrix.1", format(round(a$coefficients[1],2), nsmall = 2), 
                              format(round(a$coefficients[2],2), nsmall = 2),
                              format(round(a$P.values,2), nsmall = 2)), sep = " & ")) #Store the test results
    c <- capture.output(cat(b,x)) #Combine test results with x from before
    #Same format as before, but with different column headings and subsequent entries
    cat(c("\\begin{tabular}{|p{2cm}||p{2cm}|p{2cm}|p{2cm}|p{2cm}|}"),
        c(" \\hline"),
        c(" \\multicolumn{5}{|c|}{Multiple Regression Quadratic Assignment Procedure with Double-Semi-Partialing} \\\\ "),
        c(" \\hline"),
        c(" Model & Intercept & Coefficient & P-Value of Intercept & P-Value of Coefficient \\\\ "),
        c(" \\hline"),
        c,
        c(" \\hline"),
        c("\\end{tabular}"), sep = "\n", fill = TRUE)

  }
  if(length(analysis_list) == 7){ #For the other option in this function
    mtest <- 1 #Iterating for the mantel tests
    mrtest <- 4 #Iterating for the MRQAP
    count <- c(1,2,3) #Count for the loop
    for(i in count){ #This gets run 3 times, to print all 6 pairwise tests
      w <- analysis_list[[mtest]] #Pulls objects 1, 2, and 3
      x <- capture.output(cat(" \\\\ ")) #Store the escape characters
      y <- capture.output(cat(c(" Matrix.2 - Matrix.1", format(round(w$statistic,2), nsmall = 2), 
                              format(round(w$signif,2), nsmall = 2)), sep = " & ")) #Store test results
      z <- capture.output(cat(y, x)) #Combine the above
      #Print the LaTeX code for the table, inserting z when relevant
      cat(c("\\begin{tabular}{|p{3cm}||p{3cm}|p{3cm}|}"),
          c(" \\hline"),
          c(" \\multicolumn{3}{|c|}{Mantel Test} \\\\ "),
          c(" \\hline"),
          c(" Model & Test Statistic & P-Value \\\\ "),
          c(" \\hline"),
          z,
          c(" \\hline"),
          c("\\end{tabular}"), sep = "\n", fill = TRUE)
      #Add lines to separate the next chunk of code
      cat("\n \n \n")
      mtest <- mtest+1 #Iterate the counter for the object
    
      a <- analysis_list[[mrtest]] #Store objects 4, 5, and 6 from the list
      b <- capture.output(cat(c(" Matrix.2 - Matrix.1", format(round(a$coefficients[1],2), nsmall = 2), 
                              format(round(a$coefficients[2],2), nsmall = 2),
                              format(round(a$P.values,2), nsmall = 2)), sep = " & ")) #Store the test results
      c <- capture.output(cat(b,x)) #Assemble the line
      #Print the LaTeX table code
      cat(c("\\begin{tabular}{|p{2cm}||p{2cm}|p{2cm}|p{2cm}|p{2cm}|}"),
          c(" \\hline"),
          c(" \\multicolumn{5}{|c|}{Multiple Regression Quadratic Assignment Procedure with Double-Semi-Partialing} \\\\ "),
          c(" \\hline"),
          c(" Model & Intercept & Coefficient & P-Value of Intercept & P-Value of Coefficient \\\\ "),
          c(" \\hline"),
          c,
          c(" \\hline"),
          c("\\end{tabular}"), sep = "\n", fill = TRUE)
      #Create lines of separation for the next table
      cat("\n \n \n")
      mrtest <- mrtest+1 #Iterate to the next object
    }
    #Print the partial mantel test results outside of the loop, since the format changes a little
    j <- analysis_list[[7]]
    k <- capture.output(cat(" \\\\ ")) #Store escape characters
    l <- capture.output(cat(c(" Matrix.2,Matrix.3 - Matrix.1", format(round(j$statistic,2), nsmall = 2), 
                              format(round(j$signif,2), nsmall = 2)), sep = " & ")) #Store test results
    m <- capture.output(cat(l, k)) #Assemble the line
    #Print LaTeX table
    cat(c("\\begin{tabular}{|p{3cm}||p{3cm}|p{3cm}|}"),
        c(" \\hline"),
        c(" \\multicolumn{3}{|c|}{Partial Mantel Test} \\\\ "),
        c(" \\hline"),
        c(" Model & Test Statistic & P-Value \\\\ "),
        c(" \\hline"),
        m,
        c(" \\hline"),
        c("\\end{tabular}"), sep = "\n", fill = TRUE)
  }
}
kyarlagadda/499-project documentation built on May 13, 2019, 6:15 p.m.