kable_styling 循环内
kable_styling inside a loop
我一直在尝试使用 kable 在循环中创建 table。我发现我需要将 kable 封装在打印语句中并使用 cat("\n")。这样我就能够在循环内打印 table 。但是,格式看起来很糟糕。如何使循环内的 table 格式正确?
我如何在循环中使用 kable_styling 和 print 和 kable?
这是我的代码
# function definition
test.kable <- function(filename){
rmarkdown::render(filename)
}
#test.rmd
```{r , echo=FALSE, results="asis"}
for (i in 1:2){
print(kable(head(iris)))
cat("\n")
}
```
```{r , echo=FALSE, results="asis"}
kable_styling(kable(head(iris)),c("striped","bordered","responsive"))
```
#main r markdown in which I call the function
```{r,echo=FALSE,results='asis'}
test.kable("test.rmd")
```
这是 html 输出中的样子。前两个 iris tables 没有漂亮的格式。如何让它们看起来像最后的虹膜 table?
编辑有关答案
接受的答案很有用,因为它告诉我 css 样式包含我从未注意过的 table 样式。但是如果有人仍然想使用 kable_styling,我发现你只需要执行以下操作(归功于此 answer):
```{r , echo=FALSE, results="asis"}
for (i in 1:2){
kable(head(iris)) %>%
kable_styling("striped") %>%
htmltools::HTML() %>%
print
cat("\n")
}
```
下面是两个 table 和 kable_styling
的样子:
如果您能够添加 .CSS 文件,我们可以设置 table 的样式,因为在您的示例中它的输出是 HTML table。
---
title: "test"
author: "John Doe"
date: "18/11/2020"
output:
html_document:
css: styles.css
---
#test.rmd
```{r , echo=FALSE, results="asis"}
library(kableExtra)
for (i in 1:2){
print(kable(head(iris)))
cat("\n")
}
```
```{r , echo=FALSE, results="asis"}
kable_styling(kable(head(iris)),c("striped","bordered","responsive"))
```
这是标题为 styles.css
的 .css 文件,位于与 .Rmd
相同的目录中
table {
margin: auto;
border-top: 1px solid #666;
border-bottom: 1px solid #666;
}
table thead th { border-bottom: 1px solid #ddd; }
th, td { padding: 5px; }
thead, tfoot, tr:nth-child(even) { background: #eee; }
这给了我下面的输出。
Here 是我寻求帮助的地方,它甚至有一个关于 HTML tables 和使用 kable 循环的部分。
我一直在尝试使用 kable 在循环中创建 table。我发现我需要将 kable 封装在打印语句中并使用 cat("\n")。这样我就能够在循环内打印 table 。但是,格式看起来很糟糕。如何使循环内的 table 格式正确? 我如何在循环中使用 kable_styling 和 print 和 kable?
这是我的代码
# function definition
test.kable <- function(filename){
rmarkdown::render(filename)
}
#test.rmd
```{r , echo=FALSE, results="asis"}
for (i in 1:2){
print(kable(head(iris)))
cat("\n")
}
```
```{r , echo=FALSE, results="asis"}
kable_styling(kable(head(iris)),c("striped","bordered","responsive"))
```
#main r markdown in which I call the function
```{r,echo=FALSE,results='asis'}
test.kable("test.rmd")
```
这是 html 输出中的样子。前两个 iris tables 没有漂亮的格式。如何让它们看起来像最后的虹膜 table?
编辑有关答案
接受的答案很有用,因为它告诉我 css 样式包含我从未注意过的 table 样式。但是如果有人仍然想使用 kable_styling,我发现你只需要执行以下操作(归功于此 answer):
```{r , echo=FALSE, results="asis"}
for (i in 1:2){
kable(head(iris)) %>%
kable_styling("striped") %>%
htmltools::HTML() %>%
print
cat("\n")
}
```
下面是两个 table 和 kable_styling
的样子:
如果您能够添加 .CSS 文件,我们可以设置 table 的样式,因为在您的示例中它的输出是 HTML table。
---
title: "test"
author: "John Doe"
date: "18/11/2020"
output:
html_document:
css: styles.css
---
#test.rmd
```{r , echo=FALSE, results="asis"}
library(kableExtra)
for (i in 1:2){
print(kable(head(iris)))
cat("\n")
}
```
```{r , echo=FALSE, results="asis"}
kable_styling(kable(head(iris)),c("striped","bordered","responsive"))
```
这是标题为 styles.css
的 .css 文件,位于与 .Rmd
table {
margin: auto;
border-top: 1px solid #666;
border-bottom: 1px solid #666;
}
table thead th { border-bottom: 1px solid #ddd; }
th, td { padding: 5px; }
thead, tfoot, tr:nth-child(even) { background: #eee; }
这给了我下面的输出。
Here 是我寻求帮助的地方,它甚至有一个关于 HTML tables 和使用 kable 循环的部分。