## Only tested on 2x2 tables!
#confusionMatrix <- matrix(c(100, 50, 25, 75), nrow = 2, ncol = 2, byrow = T,
# dimnames = list(c("Pred.True", "Pred.False"), c("True", "False")))
#N <- 250
#filename <- "~/Desktop/Test.tex"
confusion2pgf <- function(confusionMatrix, N, filename){
fileConnection <- file(filename)
writeLines("% This file was generated by R2pgf.\n", fileConnection)
close(fileConnection)
nRow <- nrow(confusionMatrix) + 2
nCol <- ncol(confusionMatrix) + 2
tabularRange <- rep(paste0("c|"), each=nCol)
tabularRange <- c("|", tabularRange)
tabularRange <- paste(tabularRange, collapse = "")
rowLabels <- paste0("& \\thread{", colnames(confusionMatrix), "}")
rowLabels <- paste(rowLabels, collapse = " ")
colLabels <- vector(mode = "numeric", length = nRow-2)
for(i in seq_along(colLabels)){
colLabels[i] <- paste0(" & ", as.numeric(confusionMatrix[1,]), collapse = "")
colLabels[i] <- paste(colLabels[i], "\\\\%", collapse = "")
colLabels[i] <- paste("\t\t\t\t\\thread{", rownames(confusionMatrix)[i], "} ", colLabels[i], collapse = "")
}
write("\\begin{table}", filename, append = T)
write("\t\\begin{center}", filename, append = T)
write(paste0("\t\t\\begin{tabular}{", tabularRange, "}\\hline%"), filename, append = T)
write(paste0("\t\t\t\\", "multicolumn{2}{|c|}{\\multirowcell{2.2}{$N=", N, "$}} &"), filename, append = T)
write(paste0("\t\t\t\t", "\\multicolumn{", nCol-2, "}{c|}{\\bfseries Predicted} \\\\", "%"), filename, append = T)
write(paste0("\t\t\t\t\\cline{", nCol-2+1, "-", nCol, "}"), filename, append = T)
write(paste0("\t\t\t", "\\multicolumn{", nCol-2, "}{|c|}{}", rowLabels, "\\\\", "%"), filename, append = T)
write("\t\t\t\t\\hline", filename, append = T)
write(paste0("\t\t\t\t", "\\multirowcell{2.2}{\\bfseries Actual} &"), filename, append = T)
for(i in 1:length(colLabels)){
write(paste0(colLabels[i]), filename, append = T)
if(i != length(colLabels)){
write(paste0("\t\t\t\t\\cline{", nCol-2, "-", nCol, "} &"), filename, append = T)
}
}
write("\t\t\t\\hline", filename, append = T)
write("\t\t\\end{tabular}", filename, append = T)
write("\t\\end{center}", filename, append = T)
write("\\end{table}", filename, append = T)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.