【发布时间】:2020-11-19 01:33:52
【问题描述】:
我一直在尝试使用 kable 在循环中创建一个表。我发现我需要将 kable 包含在 print 语句中并使用 cat("\n")。这样我就可以在循环内打印表格。但是,格式看起来很糟糕。如何使循环内的表格正确格式化? 如何在循环内使用带有 print 和 kable 的 kable_styling?
这是我的代码
# 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 表没有漂亮的格式。如何让它们看起来像最后一个 iris 表?
编辑答案
接受的答案很有用,因为它告诉我包含我从不关注的表格样式的 css 样式。但是如果有人仍然想使用 kable_styling,我发现你只需要执行以下操作(感谢 answer):
```{r , echo=FALSE, results="asis"}
for (i in 1:2){
kable(head(iris)) %>%
kable_styling("striped") %>%
htmltools::HTML() %>%
print
cat("\n")
}
```
【问题讨论】:
标签: r-markdown kable