Search code examples
cssrxaringan

How to make {stargazer} table smaller in {xaringan} presentation?


I'm making my first Ninja Presentation and included a stargazer table that runs off the slide (too long). Any suggestions?

Reproducible table to be put in {xaringan}.

library(tidyverse)
library(stargazer)
df<-tibble(y=sample(1:100,500,replace=TRUE),x=sample(1:5,500,replace=TRUE),x2=sample(1:3,500,replace=TRUE),x3=sample(1:15,500,replace=TRUE),x4=sample(LETTERS[1:5],500,replace=TRUE))
stargazer(lm(df$y~df$x+df$x2+df$x3+df$x4),type="html")

along with html output for those who just want to plop that in:

<table style="text-align:center"><tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"></td><td><em>Dependent variable:</em></td></tr>
<tr><td></td><td colspan="1" style="border-bottom: 1px solid black"></td></tr>
<tr><td style="text-align:left"></td><td>y</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">x</td><td>-1.175</td></tr>
<tr><td style="text-align:left"></td><td>(0.902)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">x2</td><td>-0.742</td></tr>
<tr><td style="text-align:left"></td><td>(1.580)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">x3</td><td>0.028</td></tr>
<tr><td style="text-align:left"></td><td>(0.309)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">x4B</td><td>0.660</td></tr>
<tr><td style="text-align:left"></td><td>(4.181)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">x4C</td><td>-2.830</td></tr>
<tr><td style="text-align:left"></td><td>(4.283)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">x4D</td><td>2.581</td></tr>
<tr><td style="text-align:left"></td><td>(4.221)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">x4E</td><td>1.637</td></tr>
<tr><td style="text-align:left"></td><td>(4.100)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td style="text-align:left">Constant</td><td>55.593<sup>***</sup></td></tr>
<tr><td style="text-align:left"></td><td>(5.977)</td></tr>
<tr><td style="text-align:left"></td><td></td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left">Observations</td><td>500</td></tr>
<tr><td style="text-align:left">R<sup>2</sup></td><td>0.008</td></tr>
<tr><td style="text-align:left">Adjusted R<sup>2</sup></td><td>-0.006</td></tr>
<tr><td style="text-align:left">Residual Std. Error</td><td>29.048 (df = 492)</td></tr>
<tr><td style="text-align:left">F Statistic</td><td>0.575 (df = 7; 492)</td></tr>
<tr><td colspan="2" style="border-bottom: 1px solid black"></td></tr><tr><td style="text-align:left"><em>Note:</em></td><td style="text-align:right"><sup>*</sup>p<0.1; <sup>**</sup>p<0.05; <sup>***</sup>p<0.01</td></tr>
</table>

Solution

  • There are many ways to make an HTML table smaller. I suggest two things here:

    1. set single.row = T in stargazer::stargazer(). This gives you coefficient estimates and corresponding standard errors on the same line and hence is more space-efficient.

    2. Reduce the font size. For this you need some CSS, e.g. table { font-size: 12px; }. You may embed this in a code chunk:

      ```{css, echo = F}
      table {
        font-size: 12px;     
      }
      ```
      

      Note that this will change the font size for all tables in your presentation. A more general approach would be to define a class for a specific table style and than assigning that class to the slide:

      ```{css, echo = F}
      .regression table {
        font-size: 12px;     
      }
      ```
      
      ---
      class: regression
      # Hello World 
      
      < code producing the table >
      

    Using the default Xaringan template, the result looks like this:

    enter image description here