R knitr print in a loop

后端 未结 1 951
故里飘歌
故里飘歌 2021-01-04 19:10

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.

相关标签:
1条回答
  • 2021-01-04 19:48

    You should tell the chunk to use results as-is.

    Do this by adding results='asis' to your chunk header.

    Try this:

    ```{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
    
    0 讨论(0)
提交回复
热议问题