Fernando Cagua
Fernando Cagua

Reputation: 125

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?

Upvotes: 4

Views: 2159

Answers (2)

Yihui Xie
Yihui Xie

Reputation: 30174

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))

Upvotes: 5

mb21
mb21

Reputation: 39413

Pandoc can output to github flavoured markdown with -t markdown_github. Using knitr that would be:

library(knitr)
pandoc('input.md', format='markdown_github')

Upvotes: 4

Related Questions