Formatting tables in markdown
by Karthik Ram. Average Reading Time: almost 2 minutes.
Since someone asked about tables in markdown in the comments section of an earlier post, I thought I’d elaborate a little more. Since the appeal of markdown is its minimalism, options for formatting tables are also fairly limited. LaTeX is a much better tool if one needs to work with complicated tables (like cells that span multiple columns).
Pandoc flavored tables
Since I’m still discussing Pandoc as the markdown parser, I’ll stick with the table formatting options that it can correctly parse. The pandoc user guide has several examples for formatting tables. One can create simple, multi-line (although not multi-cell), and gridded (with borders) tables. Although it is possible to manually format tables (using a fixed-width font editor), it’s much easier to do it programatically with R and one of several packages (ascii, pander). If the data were entered into a spreadsheet, those could easily be exported as csv files. Alternatively, if the data to be used in the tables were generated programmatically, perhaps as the output of an analysis in R, those could also be easily saved as csv files or directly formatted into markdown-flavored tables with R and knitr.
Load the data
If the data are already in a spreadsheet, simply read those in R. For the sake of this post, I’ll create a really simple table to use in a markdown document.
> foo <- data.frame(x = 1:3, y = rnorm(3))
> foo
x y
1 1 -1.3665947
2 2 -0.9967103
3 3 -0.6870180
Using the pandoc.table function in the pander, this data.frame could easily be formatted with:
pandoc.table(foo)
resulting in
-----------
x y
--- -------
1 -1.3666
2 -0.9967
3 -0.6870
-----------
with a caption
> pandoc.table(foo, caption = "This is the table caption")
-----------
x y
--- -------
1 -1.3666
2 -0.9967
3 -0.6870
-----------
Table: This is the table caption
as a gridded table
> pandoc.table(foo, caption = "This is the table caption",
style = "grid")
+-----+---------+
| x | y |
+=====+=========+
| 1 | -1.3666 |
+-----+---------+
| 2 | -0.9967 |
+-----+---------+
| 3 | -0.6870 |
+-----+---------+
Table: This is the table caption
One could also use multiline as a style option for tables containing lots of text. By default, the text is split at 30 characters but one could specify one with split.cells. Wide tables can also be split (default is 80 characters) using split.table.
Table headers can be justified (left, right, or center) with the justify option. Not so complicated, right?
[...] Formatting tables in markdown [...]
Just wanted to let you know that thanks to @onesandzeroes `pandoc.table` now also supports the PHP Markdown Extra syntax that can be useful with the “markdown” R package: https://github.com/Rapporter/pander/issues/19