R针织衫在一个循环中打印 [英] R knitr print in a loop
问题描述
我一直在使用xtable
包从R矩阵中创建HTML表.当我在循环中使用函数kable
时,它没有输出任何内容.所以我盯着使用print
函数,它起作用了.问题是,当我使用打印功能时,我在表格HTML上打印了很多"##".有没有一种方法可以打印我的电缆,但是在循环时避免每行显示"##"?
I've been using the xtable
package to create HTML tables out of R matrices. When I used the function kable
in a loop, it didn't output anything. So I stared using the print
function, which worked. The problem is that when I use the print function I get lots of "##" printed along the table HTML. Is there a way to print my kable but avoiding the "##" per line while in a loop?
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"')
推荐答案
您应该告诉数据块按原样使用结果.
You should tell the chunk to use results as-is.
通过在块标题中添加results='asis'
来完成此操作.
Do this by adding results='asis'
to your chunk header.
尝试一下:
```{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"'))
}
```
你应该得到
You should get
speed dist
4 2
4 10
7 4
7 22
8 16
9 10
这篇关于R针织衫在一个循环中打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!