Skip to content

Instantly share code, notes, and snippets.

@lauratboyer
Created September 7, 2015 20:04
Show Gist options
  • Save lauratboyer/6eb2930dd07fe5e57d10 to your computer and use it in GitHub Desktop.
Save lauratboyer/6eb2930dd07fe5e57d10 to your computer and use it in GitHub Desktop.
Creates pdf table from matrix or dataframe --- table is cropped in mac, Ghostscript needs to be called directly for PC (not done here)
# table2pdf()
# Creates pdf table from matrix or dataframe
# Author: Laura Tremblay-Boyer, UBC Fisheries Centre
# Contact: l.boyer@fisheries.ubc.ca
# Note 1: Current version works on Mac, but can be tweaked for a PC.
# You need a working version of latex in your computer. Make sure
# latex in your R search path. Sys.getenv("PATH") will show you the value
# of your current path, see ?Sys.getenv and Details section in ?latex for more.
if(!grepl("tex",Sys.getenv("PATH"))) {
print("slow down cowboy! latex is either
not installed or not on your path")}
# Note 2: "capt" is by default set at NULL, which results in no heading to the table.
# To have a simple header with the table number but no caption, set capt=" "
# The "num" argument controls the table number in the table caption, with
# num=0 producing a simple "Table: " header with no numbers
# Note 3: crops to remove white margins, could be set up
# otherwise if pdfcrop function is not available (Hello there, PCs!)
# could use latex package anysize to set paper size and margins
# as a function of number of rows/columns and number of characters in each
# Apparently Ghostscript/Ghostview will do the cropping for PCs
# see: http://www.ats.ucla.edu/stat/latex/icu/install_win.htm
# Check for packages and install if missing
if("Hmisc" %in% installed.packages()[,1]) {
require(Hmisc) # for latex() function
} else { print("installing package Hmisc"); install.packages("Hmisc") }
if("tools" %in% installed.packages()[,1]) {
require(tools) # for latex() function
} else { print("installing package tools"); install.packages("tools") }
table2pdf <- function(data, capt=NULL, num=1, file="temptable.pdf", round.val=2,
useRowNames=FALSE,rownames.col, keepTex=FALSE,...) {
# Sample table if no data given in argument:
if(missing(data))
{data <- matrix(1:6, nrow=2, dimnames=list(c('a','b'),c('c','d','this that')))
capt <- "A sample table just for fun"}
if("data.frame" %in% class(data)) {
numcols <- which(sapply(data, is.numeric))
data[,numcols] <- round(data[,numcols], round.val)
}
# Creating formatted .tex file
preamble <- list(
paste("\\","documentclass{report} \n",sep=""),
paste("\\","usepackage{booktabs} \n",sep=""),
paste("\\","usepackage{pdflscape} \n",sep=""),
paste("\\","usepackage{color,colortbl} \n",sep=""),
paste("\\","thispagestyle{empty} \n",sep=""),
paste("\\","begin{document} \n",sep=""),
paste("\\","setcounter{table}{",num-1,"} \n", sep=""),
paste("\\","definecolor{Gray}{gray}{0.9} \n", sep=""),
paste("\\","definecolor{LightCyan}{rgb}{0.88,1,1}", sep="")
)
if(num==0) preamble[[length(preamble)+1]] <- paste("\\","renewcommand","\\","thetable{} \n",sep="")
cat(paste(preamble),file="temptable.tex")
rl <- "data"
if(!missing(rownames.col)) rl <- rownames.col
if(!useRowNames) {
rn <- NULL
rl <- NULL
#rownames(data) <- data[,1]
# rl <- names(data)[1]
}else{
rn <- rownames(data)}
# Format column names so that latex likes them
names(data) <- gsub("_","\\\\_",names(data))
latex(data,file="temptable.tex",append=TRUE, caption=capt, booktabs=TRUE,
rowlabel=rl, rowname=rn, ...)
cat(paste("\\","end{document}",sep=""),file="temptable.tex", append=TRUE)
# Convert .tex to .pdf
texi2dvi("temptable.tex", pdf=TRUE)
system("pdfcrop temptable.pdf temptable.pdf --margins 5", intern=TRUE)
file.rename("temptable.pdf",file)
if(!keepTex) file.remove("temptable.tex")
fr <- file.remove(c("temptable.aux","temptable.log"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment