r-markdown - 用for循环显示多个kables?
问题描述
下面的 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?
解决方案
你的第三种方法几乎是正确的;-)
只需使用该选项results = "asis"
```{r pressure3b, echo=FALSE, results='asis'}
for (table in tables) {
print(table)
}
```
推荐阅读
- java - 如果一行在匹配字符串的末尾或中间不包含分号,则正则表达式匹配字符串
- django - 总价 Django
- vue.js - 上传文件数组formData vue
- python - Webscraping form webbrowser 缺少价格
- python - 用新数据更新数据框
- python - 在 Python 中检查变量的类型是否为 dict[str, Any]
- html - 将链接粘贴到背景图像上的特定区域
- python - Python:在 numpy_financial.irr() 上导入多进程可以很好地减少运行时间吗?
- angular - 如何让组件在 html 之前完成加载
- ios - Handling UserIntent in Flutter by calling eventSink from AppDelegate