科技动态

No more ascii-art

微信扫一扫,分享到朋友圈

No more ascii-art
0

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

MoonScript v0.2.3

上一篇

Reflections on StartupWeekend Missoula 2013

下一篇

您也可能喜欢

评论已经被关闭。

插入图片
No more ascii-art

长按储存图像,分享给朋友