【发布时间】:2020-03-27 23:14:40
【问题描述】:
下面的 Rmarkdown 代码将两个表放在一个列表中,然后尝试使用 for 循环显示它们。
---
title: "Testing Section Numbers"
author: "Authors"
# Neither HTML nor PDF work. To try PDF, uncomment:
# output: pdf_document
---
```{r pressure2, echo=FALSE}
library(knitr)
tables <- list(
kable(pressure[1:5,], caption = "My first table"),
kable(pressure[1:5,], caption = "My second table"))
```
first way works:
```{r pressure3a, echo=FALSE}
tables[[1]]
tables[[2]]
```
second way blank:
```{r pressure3b, echo=FALSE}
for (table in tables) {
table
}
```
third way has raw text:
```{r pressure3c, echo=FALSE}
for (table in tables) {
print(table)
}
```
fourth way badly formatted:
```{r pressure3d, echo=FALSE, results='asis'}
for (table in tables) {
cat(table)
}
```
fifth way blank:
```{r pressure3e, echo=FALSE}
for (idx in 1:length(tables)) {
table[[idx]]
}
```
第一种方式正确显示表格,但不是 for 循环。其他方法都行不通。
如何使用 for 循环在一个块中显示多个 kable?
【问题讨论】:
标签: r-markdown knitr kable