在 RMarkdown 中使用 knitr 和 kableExtra 包创建多个表的 For 循环
For Loop for Creating Multiple Tables using knitr and kableExtra packages in RMarkdown
我需要在 RMarkdown 中创建多个 table 并使用 kableExtra 包对其进行样式设置。例如,我有鸢尾花数据集。我的第一个 table 显示前 20 行,第二个 table 显示接下来的 20 行,第三个 table 显示接下来的 20 行...下面是代码:
---
title: ""
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
library(knitr)
library(kableExtra)
landscape(kable_styling(kable(iris[1:20, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T))
landscape(kable_styling(kable(iris[21:40, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T))
landscape(kable_styling(kable(iris[41:60, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T))
```
效果很好,它 returns 三个 table,每个都在不同的 sheet 中。实际上,我有不止三个 table,所以我认为使用 for 循环会更明智,我利用了 link 中给出的答案。很简单,我按照建议在每次打印调用后换行。
---
title: "untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
library(knitr)
library(kableExtra)
for (i in 1:3) {
print(landscape(kable_styling(
kable(iris[20*(i-1)+1:20*i, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T)))
cat("\n")
}
```
但是不行。我想那是因为我用 kableExtra 包中的命令封装了 kable 命令。
有谁能做到吗?我的意思是有什么方法可以让我不用打字吗?
您现有的代码就快完成了。我认为唯一需要的更改是将 results='asis'
添加到块选项(我认为不需要额外的换行符)。这是对我有用的完整 RMarkdown 内容
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r results='asis'}
library(knitr)
library(kableExtra)
for (i in 1:3) {
print(landscape(kable_styling(
kable(iris[20*(i-1)+1:20*i, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T)))
}
```
我需要在 RMarkdown 中创建多个 table 并使用 kableExtra 包对其进行样式设置。例如,我有鸢尾花数据集。我的第一个 table 显示前 20 行,第二个 table 显示接下来的 20 行,第三个 table 显示接下来的 20 行...下面是代码:
---
title: ""
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
library(knitr)
library(kableExtra)
landscape(kable_styling(kable(iris[1:20, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T))
landscape(kable_styling(kable(iris[21:40, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T))
landscape(kable_styling(kable(iris[41:60, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T))
```
效果很好,它 returns 三个 table,每个都在不同的 sheet 中。实际上,我有不止三个 table,所以我认为使用 for 循环会更明智,我利用了 link
---
title: "untitled"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
library(knitr)
library(kableExtra)
for (i in 1:3) {
print(landscape(kable_styling(
kable(iris[20*(i-1)+1:20*i, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T)))
cat("\n")
}
```
但是不行。我想那是因为我用 kableExtra 包中的命令封装了 kable 命令。
有谁能做到吗?我的意思是有什么方法可以让我不用打字吗?
您现有的代码就快完成了。我认为唯一需要的更改是将 results='asis'
添加到块选项(我认为不需要额外的换行符)。这是对我有用的完整 RMarkdown 内容
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r results='asis'}
library(knitr)
library(kableExtra)
for (i in 1:3) {
print(landscape(kable_styling(
kable(iris[20*(i-1)+1:20*i, ], format = "latex", align = "c",
row.names = FALSE), latex_options = c("striped"), full_width = T)))
}
```