Stargazing with Dexy

May 22, 2013   ·   By ana   ·   Comments Off   ·   Posted in Examples

Let’s check out the new Stargazer library for making beautiful PDF tables and see how to make it work with dexy. To use the R package we first load the library:

> library(stargazer)

Hlavac, Marek (2013). stargazer: LaTeX code for well-formatted regression and summary statistics tables.
R package version 3.0.1. http://CRAN.R-project.org/package=stargazer


Here’s the basic demo:

> stargazer(attitude)

% Table created by StarGazer v.3.0.1 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Wed, May 22, 2013 - 12:21:48 PM
\begin{table}[htb] \centering
\caption{}
\label{}
\footnotesize

\begin{tabular}{@{\extracolsep{5pt}}l c c c c c }
\\[-1.8ex]\hline
\hline \\[-1.8ex]
Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Max} \\
\hline \\[-1.8ex]
rating & 30 & 64.633 & 12.173 & 40 & 85 \\
complaints & 30 & 66.600 & 13.315 & 37 & 90 \\
privileges & 30 & 53.133 & 12.235 & 30 & 83 \\
learning & 30 & 56.367 & 11.737 & 34 & 75 \\
raises & 30 & 64.633 & 10.397 & 43 & 88 \\
critical & 30 & 74.767 & 9.895 & 49 & 92 \\
advance & 30 & 42.933 & 10.289 & 25 & 72 \\
\hline \\[-1.8ex]
\normalsize
\end{tabular}
\end{table}


This prints the LaTeX markup in our R session. The Stargazer docs say:

 ‘stargazer’ uses ‘cat()’ to output LaTeX code for the table. To
allow for further processing of this output, ‘stargazer’ also
returns the same output invisibly as a character string vector.
You can include the produced tables in your paper by inserting
‘stargazer’ output into your publication's TeX source.


So, according to this there’s no built-in way to get stargazer to write its output directly to a file. One option would be to use R’s sink() to divert the output from stargazer:

> sink("output-from-sink.tex")
> stargazer(attitude)
> sink()


This gives us a file named output-from-sink.tex which we can include in documents.

That works fine, we can create as many separate files as we need. However, it might be nice to create a single file with multiple LaTeX snippets in it, referenced by name.

To do this we create a list and add named elements to the list containing output from running stargazer:

> latex.snippets <- list()
> latex.snippets["attitude"] <- paste(stargazer(attitude), collapse="\n")

% Table created by StarGazer v.3.0.1 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Wed, May 22, 2013 - 12:21:49 PM
\begin{table}[htb] \centering
\caption{}
\label{}
\footnotesize

\begin{tabular}{@{\extracolsep{5pt}}l c c c c c }
\\[-1.8ex]\hline
\hline \\[-1.8ex]
Statistic & \multicolumn{1}{c}{N} & \multicolumn{1}{c}{Mean} & \multicolumn{1}{c}{St. Dev.} & \multicolumn{1}{c}{Min} & \multicolumn{1}{c}{Max} \\
\hline \\[-1.8ex]
rating & 30 & 64.633 & 12.173 & 40 & 85 \\
complaints & 30 & 66.600 & 13.315 & 37 & 90 \\
privileges & 30 & 53.133 & 12.235 & 30 & 83 \\
learning & 30 & 56.367 & 11.737 & 34 & 75 \\
raises & 30 & 64.633 & 10.397 & 43 & 88 \\
critical & 30 & 74.767 & 9.895 & 49 & 92 \\
advance & 30 & 42.933 & 10.289 & 25 & 72 \\
\hline \\[-1.8ex]
\normalsize
\end{tabular}
\end{table}


We’ll also generate the regression output example from Tal Galili’s blog post and save this to the list:

> latex.snippets["regression"] <- paste(regression.results, collapse="\n")


Then we export this list to a JSON file using the rjson library:

> library(rjson)
> latex.snippets.file <- file("stargazer-snippets.json", "w")
> writeLines(toJSON(latex.snippets), latex.snippets.file)
> close(latex.snippets.file)


Now let’s create a LaTeX document and make use of the snippets we have generated. Here’s the full source of the LaTeX document:

\documentclass{article}

\usepackage{graphicx}
\usepackage{color}
\usepackage{fancyvrb}
\usepackage{hyperref}

% Pygments syntax highlighting codes
<< pygments['pastie.tex'] >>

\begin{document}

Examples taken from \href{http://bit.ly/10TgaJp}{Tal Galili's blog post} about
stargazer.  This document is part of blog post \url{http://blog.dexy.it/523}.

Here's a pretty table:

<< d['output-from-sink.tex'] >>

<% set snippets = d['stargazer-snippets.json'].from_json() -%>

Here's the same table:

<< snippets.attitude >>

Here's a fancier table:

<< snippets.regression >>

Here is the source of this \LaTeX document:

<< d['example.tex|pyg|l'] >>

\end{document}


Here is how we configure the latex document to include the output from tables.R (you don’t need the ‘botoup’ filter unless you want to upload your PDF to Amazon S3):

    - example.tex|jinja|latex|botoup:
- example.tex|pyg|l
- tables.R|rout:
- rout: {
}


Here’s part of the LaTeX document showing how we include the contents of the standalone file we created using sink():

Here's a pretty table:

<< d['output-from-sink.tex'] >>


To load our named list, we call the from_json() method to convert the JSON to a (python) dictionary:

<% set snippets = d['stargazer-snippets.json'].from_json() -%>


Then we can access the elements in this list as follows:

Here's the same table:

<< snippets.attitude >>

Here's a fancier table:

<< snippets.regression >>


We can use a dot to access the elements because of jinja. You can download the generated PDF to see what it looks like here.

That was two different ways to get Stargazer’s output into a file (for use with dexy).