并排对齐多个表格
Align multiple tables side by side
以下代码生成 2 个表格。我将如何设置它让它们并排对齐,例如连续 3 个?
---
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))
```
输出如下所示:
我用这个 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 将 2 个 Latex table 彼此对齐。似乎您不能像使用 xtable
那样自由调整 table 的行(有人对此了解更多吗?)。使用 format = Latex
你会在每一行之后得到一条水平线。但是文档显示了其他格式的两个示例。一个使用 longtable
包(附加参数:longtable = TRUE
),另一个使用 booktabs
包(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}"
))
```
只需将两个数据框放在一个列表中,例如
t1 <- head(mtcars)[1:3]
t2 <- head(mtcars)[4:6]
knitr::kable(list(t1, t2))
注意这需要 knitr >= 1.13.
这里是 html 文档的解决方案
(因为这个问题被问得非常广泛,而不是具体 指的是 LaTeX)。
需要 knitr
和 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")
```
table t2 得到 position = "left"
是有意的。如果您允许它浮动,这将不会阻塞该段落的其余部分并弄乱文档中的以下行。
结果:
以下代码生成 2 个表格。我将如何设置它让它们并排对齐,例如连续 3 个?
---
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))
```
输出如下所示:
我用这个 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 将 2 个 Latex table 彼此对齐。似乎您不能像使用 xtable
那样自由调整 table 的行(有人对此了解更多吗?)。使用 format = Latex
你会在每一行之后得到一条水平线。但是文档显示了其他格式的两个示例。一个使用 longtable
包(附加参数:longtable = TRUE
),另一个使用 booktabs
包(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}"
))
```
只需将两个数据框放在一个列表中,例如
t1 <- head(mtcars)[1:3]
t2 <- head(mtcars)[4:6]
knitr::kable(list(t1, t2))
注意这需要 knitr >= 1.13.
这里是 html 文档的解决方案
(因为这个问题被问得非常广泛,而不是具体 指的是 LaTeX)。
需要 knitr
和 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")
```
table t2 得到 position = "left"
是有意的。如果您允许它浮动,这将不会阻塞该段落的其余部分并弄乱文档中的以下行。
结果: