Reputation: 21204
The following code produces 2 tables on top of each other. How would I set it to have them aligned side by side, e.g. 3 to a row?
---
title: "sample"
output: pdf_document
---
```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)}
```
```{r sample, echo=FALSE, results='asis'}
library(knitr)
t1 <- head(mtcars)[1:3]
t2 <- head(mtcars)[4:6]
print(kable(t1))
print(kable(t2))
```
Upvotes: 41
Views: 80754
Reputation: 93
Piggybacking off @Alex's answer since I don't have enough rep to leave a comment:
---
title: "sample"
output: pdf_document
header-includes:
- \usepackage{booktabs}
---
```{r}
library(knitr)
library(xtable)
t1 <- kable(head(mtcars)[1:3], format = "latex", booktabs = TRUE)
t2 <- kable(head(mtcars)[4:6], format = "latex", booktabs = TRUE)
```
\begin{table}[!htb]
\begin{minipage}{.5\linewidth}
\centering
```{r}
t1
```
\end{minipage}%
\begin{minipage}{.5\linewidth}
\centering
```{r}
t2
```
\end{minipage}%
\end{table}
This will work instead of the cat()
function call. In my experience, outputting LaTeX code from a code chunk will just lead to it getting printed as plain text in the PDF.
Upvotes: 1
Reputation: 51914
In Quarto, you can use layout-ncol
. This works both for HTML and PDF outputs.
---
title: "sidebyside"
format: pdf
---
```{r}
#| layout-ncol: 2
#| tbl-cap: "Tables"
#| tbl-subcap: ["A table", "Another table"]
library(knitr)
# table on the left
kable(head(mtcars))
# table on the right
kable(head(cars))
```
Upvotes: 10
Reputation: 23737
here a solution for html documents
(As this question was asked very broadly and not specifically referring to LaTeX).
Requires knitr
and kableExtra
---
title: "Side by side"
output: html_document
---
```{r sample, echo=FALSE}
library(knitr)
library(kableExtra)
t1 <- head(mtcars)[1:3]
t2 <- head(mtcars)[4:6]
```
## as list
```{r}
kable(list(t1, t2))
```
## with float
```{r, echo = FALSE}
kable(t1) %>%
kable_styling(full_width = FALSE, position = "float_left")
kable(t2) %>%
kable_styling(full_width = FALSE, position = "left")
```
It is intentional that table t2 gets position = "left"
. If you allow it to float, this will not block the rest of the paragraph and mess up the following lines in your document.
result:
Upvotes: 29
Reputation: 30114
Just put two data frames in a list, e.g.
t1 <- head(mtcars)[1:3]
t2 <- head(mtcars)[4:6]
knitr::kable(list(t1, t2))
Note this requires knitr >= 1.13.
Upvotes: 50
Reputation: 4995
I used this Align two data.frames next to each other with knitr? which shows how to do it in html
and this https://tex.stackexchange.com/questions/2832/how-can-i-have-two-tables-side-by-side to align 2 Latex tables next to each other. It seems that you cannot freely adjust the lines of the table as you can do it with xtable
(does anybody know more about this?). With format = Latex
you get a horizontal line after each row. But the documentation shows two examples for other formats. One using the longtable
package (additional argument: longtable = TRUE
) and the other using the booktabs
package (booktabs = TRUE
).
---
title: "sample"
output: pdf_document
header-includes:
- \usepackage{booktabs}
---
```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)}
```
```{r sample, echo=FALSE, results='asis'}
library(knitr)
library(xtable)
t1 <- kable(head(mtcars)[1:3], format = "latex", booktabs = TRUE)
t2 <- kable(head(mtcars)[4:6], format = "latex", booktabs = TRUE)
cat(c("\\begin{table}[!htb]
\\begin{minipage}{.5\\linewidth}
\\caption{}
\\centering",
t1,
"\\end{minipage}%
\\begin{minipage}{.5\\linewidth}
\\centering
\\caption{}",
t2,
"\\end{minipage}
\\end{table}"
))
```
Upvotes: 30