Rmd chunk 中循环输出需要使用cat函数
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(lubridate)
library(knitr)
library(kableExtra)
library(htmltools)
for (i in 1:3) {
cat("\n")
cat("### ", sprintf('第%d次', i), "\n")
tab11 = iris %>%
head(10) %>%
kable(row.names = F) %>%
kable_styling() %>%
row_spec(0, background = 'grey') %>%
row_spec(10, background = '#E6C1C1')
cat(tab11)
cat('\n')
cat('\n')
cat('\n')
}
第1次
Sepal.Length
|
Sepal.Width
|
Petal.Length
|
Petal.Width
|
Species
|
5.1
|
3.5
|
1.4
|
0.2
|
setosa
|
4.9
|
3.0
|
1.4
|
0.2
|
setosa
|
4.7
|
3.2
|
1.3
|
0.2
|
setosa
|
4.6
|
3.1
|
1.5
|
0.2
|
setosa
|
5.0
|
3.6
|
1.4
|
0.2
|
setosa
|
5.4
|
3.9
|
1.7
|
0.4
|
setosa
|
4.6
|
3.4
|
1.4
|
0.3
|
setosa
|
5.0
|
3.4
|
1.5
|
0.2
|
setosa
|
4.4
|
2.9
|
1.4
|
0.2
|
setosa
|
4.9
|
3.1
|
1.5
|
0.1
|
setosa
|
第2次
Sepal.Length
|
Sepal.Width
|
Petal.Length
|
Petal.Width
|
Species
|
5.1
|
3.5
|
1.4
|
0.2
|
setosa
|
4.9
|
3.0
|
1.4
|
0.2
|
setosa
|
4.7
|
3.2
|
1.3
|
0.2
|
setosa
|
4.6
|
3.1
|
1.5
|
0.2
|
setosa
|
5.0
|
3.6
|
1.4
|
0.2
|
setosa
|
5.4
|
3.9
|
1.7
|
0.4
|
setosa
|
4.6
|
3.4
|
1.4
|
0.3
|
setosa
|
5.0
|
3.4
|
1.5
|
0.2
|
setosa
|
4.4
|
2.9
|
1.4
|
0.2
|
setosa
|
4.9
|
3.1
|
1.5
|
0.1
|
setosa
|
第3次
Sepal.Length
|
Sepal.Width
|
Petal.Length
|
Petal.Width
|
Species
|
5.1
|
3.5
|
1.4
|
0.2
|
setosa
|
4.9
|
3.0
|
1.4
|
0.2
|
setosa
|
4.7
|
3.2
|
1.3
|
0.2
|
setosa
|
4.6
|
3.1
|
1.5
|
0.2
|
setosa
|
5.0
|
3.6
|
1.4
|
0.2
|
setosa
|
5.4
|
3.9
|
1.7
|
0.4
|
setosa
|
4.6
|
3.4
|
1.4
|
0.3
|
setosa
|
5.0
|
3.4
|
1.5
|
0.2
|
setosa
|
4.4
|
2.9
|
1.4
|
0.2
|
setosa
|
4.9
|
3.1
|
1.5
|
0.1
|
setosa
|
表格的字符宽度不够可以使用空格填充
使用固定宽度生成表格在copy到word格式不易产生表格变形
library(stringr)
str_pad('你好', 12)
## [1] " 你好"