# No more ascii-art

January 24, 2013
By

(This article was first published on Will Lowe » R, and kindly contributed to R-bloggers)

At least fourfive R packages will turn your regression models into pretty latex tables: texreg, xtable, apsrtable, memisc, and stargazer.  This is very nice if you happen to be a latex document or its final reader, but it’s not so great if you’re making those models to start with.

What if you wanted to see these as you were working on them? In particular what if you wanted to see all your models lined up as if they were already Table 4 of the Masterwork Yet To Be Named that is only now slowly taking shape in your mind?

For example, you have a cunning idea about the well-known Chatterjee–Price Attitude Data that’s built into R so you run a stack of regression models to test it. (No I’ve never heard of the Chatterjee-Price Attitude Data either, but it’ll do as an example and it’s good enough for Marek Hlavac). Here we go:

m1 <- lm(rating ~ complaints + privileges + learning + raises + critical,
data=attitude)
m2 <- lm(rating ~ complaints + privileges + learning, data=attitude)
m3 <- lm(rating ~ learning + critical + advance, data=attitude)


Now, rather than running summary on each of them and then scrolling up and down to compare them, you’d like to type this

library(apsrtable) ## for fancy latex regression table output
as_pdf(apsrtable(m1,m2,m3))

Much nicer.

Here’s how to do it.

First, the prerequisites: You’ve already got latex and R installed, but for convenience I’m going to assume that you have a texlive-based latex installation, e.g. the one that comes with MacTeX and the default one for Linux. That’s because I want and it bundles a sweet little utility called pdfcrop. As the link suggests, you can get it separately, although you shouldn’t have to.

Also, if you’re a Mac user I’m also going to assume that Apple did not screw up your command line tools. If you can type

which pdflatex

in the Terminal and get a path starting /usr/texbin back, then you’re in business. If that doesn’t happen, see the answer to this question.

Now here’s a little R function to take all the nice apsrtable-generated latex and make it do something practical for you right now

as_pdf <- function(x){
require(tools)

fname <- tempfile(pattern = "texview-", tmpdir = tempdir(),
fileext = ".tex")

\\usepackage[margin=10pt,font=small,labelformat=empty,
labelsep=none]{caption}
\\usepackage{dcolumn}
\\thispagestyle{empty}
\\begin{document}"
footer <- "\\end{document}"

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("open", args=c(cropfile))
## shell.exec(file_path_as_absolute(cropfile)) ## for Windows, probably
}


This function creates a little latex file from a template, drops it into temporary directory, wedges the function’s input – assumed to be some latex representation of something interesting – into the middle, and attempts to compile the result. If all goes well pdfcrop is used to shrink the results to a reasonable size.

The last lines of the function try to open the resulting file. On a Mac this will launch the system pdf viewer. The commented-out section should do a similar thing on Windows, though I haven’t checked because I’m not a masochist. Feel free to tell me all about it.

So there it is. No more ascii art for your models.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...