I have just come across Will Lowe’s excellent post on using pretty Latex tables when you’re still working on the analysis.
I had to make a tiny modification to get this run on GNU/Linux: changing the “open” command to the more specific “evince” (i.e. the PDF viewer). All credit should go to Will Lowe.
as_pdf <- function(x){
require(tools)
fname <- tempfile(pattern = "texview-", tmpdir = tempdir(),
fileext = ".tex")
header <- "\\documentclass{article}
\\usepackage[margin=10pt,font=small,labelformat=empty,
labelsep=none]{caption}
\\usepackage{dcolumn}
\\thispagestyle{empty}
\\begin{document}"
footer <- "\\end{document}"
cat(header, file=fname, sep='\n')
cat(x, file=fname, append=TRUE)
cat(footer, file=fname, append=TRUE, sep='\n')
newfile <- paste0(file_path_sans_ext(fname), ".pdf")
cropfile <- paste0(file_path_sans_ext(fname), "-crop.pdf")
origdir <- getwd()
tryCatch({
setwd(tempdir()) ## next to the tex file
texi2pdf(fname, clean=TRUE)
system2("pdfcrop", args=c("--margins", "10", newfile, cropfile),
stdout=FALSE)
}, finally=setwd(origdir))
system2("evince", args=c(cropfile))
## shell.exec(file_path_as_absolute(cropfile)) ## for Windows, probably
}
One observation: The leading comment line in the stargazer package breaks the function in its current version (the Latex code is not broken down into lines). All works fine with the apsrtable package.
For some nice ASCII art in the console, Martin Elff’s mtable command (memisc package) is worth a look (and it does Latex, too): http://www.martin-elff.net/memisc/mtable-creating-tables-model-estimates/