# No more ascii-art

four
five 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=""

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))
to get this instead

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" )="" 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,="" append="TRUE)" cat(footer,="" newfile="" paste0(file_path_sans_ext(fname),="" ".pdf")="" cropfile="" "-crop.pdf")="" origdir="" getwd()="" trycatch({="" setwd(tempdir())="" ##="" next="" to="" the="" tex="" texi2pdf(fname,="" clean="TRUE)" system2("pdfcrop",="" args="c(" --margins","="" "10",="" newfile,="" cropfile),="" stdout="FALSE)" },="" finally="setwd(origdir))" system2("open",="" 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.

注意：本文来自Will Lowe。本站无法对本文内容的真实性、完整性、及时性、原创性提供任何保证，请您自行验证核实并承担相关的风险与后果！CoLaBug.com遵循[CC BY-SA 4.0]分享并保持客观立场，本站不承担此类作品侵权行为的直接责任及连带责任。您有版权、意见、投诉等问题，请通过[eMail]联系我们处理，如需商业授权请联系原作者/原网站。