gpt4 book ai didi

R knitr 循环打印

转载 作者:行者123 更新时间:2023-12-04 05:38:12 24 4
gpt4 key购买 nike

我一直在使用 xtable使用 R 矩阵创建 HTML 表格的包。当我使用函数时 kable在循环中,它没有输出任何内容。所以我开始使用 print功能,这工作。问题是,当我使用打印功能时,我会在表格 HTML 上打印出很多“##”。有没有办法打印我的 kable 但在循环中避免每行“##”?

library("xtable", lib.loc="~/R/win-library/3.1")

for(i in 1:3) {
#Must use print because of the loop, but get ## per line
print(kable(head(cars), "html", table.attr='class="flat-table"'))
}
#No neded to use print, no ## printed per line
kable(head(cars), "html", table.attr='class="flat-table"')

最佳答案

您应该告诉块按原样使用结果。

通过添加 results='asis' 来做到这一点到你的块头。

尝试这个:

```{r, results='asis', echo=FALSE}
library(knitr)
library(xtable)

for(i in 1:3) {
#Must use print because of the loop, but get ## per line
print(kable(head(cars), "html", table.attr='class="flat-table"'))
}
```

你应该得到
speed    dist
4 2
4 10
7 4
7 22
8 16
9 10

关于R knitr 循环打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28313600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com