【问题标题】:R knitr print in a loopR knitr 循环打印
【发布时间】:2015-04-03 12:22:48
【问题描述】:

我一直在使用 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"')

【问题讨论】:

  • 您没有将 kable 或 print 的输出分配给任何东西。

标签: r loops knitr


【解决方案1】:

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

通过将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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    相关资源
    最近更新 更多