D8Amonk D8Amonk - 15 days ago 10
LaTeX Question

How can I put multiple knitr::kable or stargazer tables next to each other?

Trying to simply produce separate tables of Anscombe's Quartet Sets, so that there is a nice space between them and formatting. I'd prefer a 1x4 set of tables, but a 2x2 would suffice.

For either dimension of results, both

knitr::kable
s and
stargazer
solutions do not seem to be able to handle multiple non-model or non-summary objects:

anscombe.1 <- data.frame(X1 = anscombe[["x1"]], Y1 = anscombe[["y1"]], Set = "1")
anscombe.2 <- data.frame(X2 = anscombe[["x2"]], Y2 = anscombe[["y2"]], Set = "2")
anscombe.3 <- data.frame(X3 = anscombe[["x3"]], Y3 = anscombe[["y3"]], Set = "3")
anscombe.4 <- data.frame(X4 = anscombe[["x4"]], Y5 = anscombe[["y4"]], Set = "4")
# I usually call stargazer() with multiple model objects, so...
stargazer(anscombe.1, anscombe.2, anscombe.3, anscombe.4, summary = FALSE)



Error in if (.global.summary[i] == TRUE) { :
missing value where TRUE/FALSE needed

Calls: ... withVisible -> eval -> eval -> stargazer -> .stargazer.wrap


If call
stargazer(anscombe.1, summary = FALSE)
, however, I get the desired output. Similarly, I can call
knitr::kable(anscombe)
and get the table for a single set, but not all four:

knitr::kable(anscombe.1, anscombe.2, anscombe.3, anscombe.4)



Quitting from lines 9-36 (anscombe.Rmd)
Error in round(x[, j], digits[j]) :

non-numeric argument to mathematical function
Calls: ... withCallingHandlers -> withVisible -> eval -> eval ->


How can I put these four sets together - preferably in a single row of four tables, with some space between them - so that I can line up their (similar) summary statistics below, likely using the same technique for a columnar summary?

Answer

Putting things side-by-side in Markdown is hard: it's not designed for that. So you need to include some LaTeX markup in the file.

For example,

---
title: "Side by Side"
date: "October 31, 2016"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## Side by Side

\begin{minipage}{0.3\textwidth}
```{r}
knitr::kable(diag(2), format = "latex")
```
\end{minipage}
\begin{minipage}{0.3\textwidth}
```{r}
knitr::kable(diag(3), format = "latex")
```
\end{minipage}
\begin{minipage}{0.3\textwidth}
```{r echo=FALSE}
knitr::kable(diag(4), format = "latex")
```
\end{minipage}

This produces output like

enter image description here

Comments