Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a GitHub flavoured markdown file using knitr?

I love using knitr to generate dynamic reports and share them with my co-workers using GitHub. What I usually do is to knit my Rmarkdown script --knit ('myfile.Rmd')-- and generate a markdown (myfile.md) version that can be directly seen on GitHub. The markdown file on GitHub works much better for me than a HTML file that knitr generates with pandoc.

This workflow usually works flawless except when I want to display a table. At the moment I'm using kable inside the R-chunk which works pretty nicely if the end product is an HTML file.

My R-chunk looks like:

```{r}
library (knitr)
data (cars)
kable (head (cars))
```

When kable is called from the console, I get the piped table I want:

| speed| dist|
|-----:|----:|
|     4|    2|
|     4|   10|
|     7|    4|
|     7|   22|

which is nicely displayed by GitHub.

However, what knit('myfile.Rmd') generates in myfile.md (when kable is called from the R-chunk) is a simple table

speed   dist
------  -----
    4      2
    4     10
    7      4
    7     22

which is not nicely displayed by GitHub.

Is there any way to make the tables in my markdown file compatible with the GitHub flavoured markdown?. Maybe there is a knitr or kable() option I'm not aware of? Or maybe there is an alternative to kable that achieves the desired results?

like image 427
Fernando Cagua Avatar asked Feb 11 '23 20:02

Fernando Cagua


1 Answers

You can specify the table format via the format argument of kable(), e.g.

kable(head(mtcars), format = 'markdown')

Or if you want to set this option globally, you can

options(knitr.table.format = 'markdown')
# then just kable(head(mtcars))
like image 153
Yihui Xie Avatar answered Feb 13 '23 12:02

Yihui Xie