Debian Codegraph @

vignettes/pandoc_table.Rmd

raw
1 ---
2 title: "Rendering tables with pandoc.table"
3 author: "Roman Tsegelskyi, Gergely Daróczi"
4 date: "`r Sys.Date()`"
5 output: rmarkdown::html_vignette
6 vignette: >
7 %\VignetteIndexEntry{Rendering tables with pandoc.table}
8 %\VignetteEngine{knitr::rmarkdown}
9 %\VignetteEncoding{UTF-8}
10 ---
11
12 ```{r, echo = FALSE, message = FALSE}
13 knitr::opts_chunk$set(collapse = T, comment = "#>")
14 library(pander)
15 ```
16
17 Core functionality of `pander` is centered around `pandoc.table`, which is aimed at rendering tables in `markdown`. In case of 2D tables, `pander` calls `pandoc.table` internally, thus in such cases `pander` and `pandoc.table` support the same argument and in this vignette will be used iterchangingly. `pandoc.table` has a wide variety of options ([highlighting](#highlighting-cells), [styles](#table-styles), etc.) and this vignette aims to give a more detailed overview of the most common options. `pander` comes with a variety of globally adjustable options, which have an effect on the result of your reports. You can query and update these options with the `panderOptions` function.
18
19 ## Table styles
20
21 Since `pander` aims at rendering R objects into [Pandoc](https://pandoc.org/)'s markdown all four (`multiline`, `simple`, `grid`, `rmarkdown`) of [Pandoc](https://johnmacfarlane.net/pandoc/README.html#tables)'s formats are supported. Users are advised to stick with the default `multiline` style, but if there is a need to change it either specify `style` argument when calling `pander/pandoc.table` or change the default `style` using `panderOptions`.
22
23 ### multiline tables
24
25 `multiline` tables allow headers and table rows to span multiple lines of text (but cells that span multiple columns or rows of the table are not supported). Also note that, for simplicity, line breaks are removed from cells by default, so multiline cells are typically the result of [splitting large cells](#table-and-cell-width) or setting `keep.line.breaks` to `TRUE`:
26
27 ```{r}
28 m <- data.frame('Value\n1', 'Value\n2')
29 colnames(m) <- c('Multiline\nCol1', 'Multiline\nCol2')
30 pandoc.table(m, keep.line.breaks = TRUE)
31 m <- mtcars[1:3, 1:4]
32 pandoc.table(m)
33 ```
34
35 ### simple tables
36
37 `simple` tables are have more compact syntax that all other styles, but don't they support multiline cells:
38
39 ```{r, error=TRUE}
40 m <- mtcars[1:3, 1:4]
41 pandoc.table(m, style = 'simple')
42 m <- data.frame('Value\n1', 'Value\n2')
43 colnames(m) <- c('Multiline\nCol1', 'Multiline\nCol2')
44 pandoc.table(m, keep.line.breaks = TRUE, style='simple')
45 ```
46
47 ### grid tables
48
49 `grid` format is really handy for `emacs` users ([Emacs table mod](https://table.sourceforge.net/)) and it does support block elements (multiple paragraphs, code blocks, lists, etc.) inside cells, but cells can't span multiple columns or rows. Alignments are not supported for grid tables by most parsers, meaning that even though `pander` will produce a table with alignment, it will be lost during conversion from `markdown` to `HTML/PDF/DOCX`.
50
51 ```{r, error=TRUE}
52 m <- mtcars[1:3, 1:4]
53 pandoc.table(m, style = 'grid')
54 m <- data.frame('Value\n1', 'Value\n2')
55 colnames(m) <- c('Multiline\nCol1', 'Multiline\nCol2')
56 pandoc.table(m, keep.line.breaks = TRUE, style='grid')
57 ```
58
59 ### rmarkdown tables
60
61 `rmarkdown` or pipe table format, is often used directly with `knitr`, since it was supported by the first versions of the `markdown` package. It is similar to `simple` table in that multiline cells are also not supported. The beginning and ending pipe characters are optional, but pipes are required between all columns:
62
63 ```{r, error=TRUE}
64 m <- mtcars[1:3, 1:4]
65 pandoc.table(m, style = 'rmarkdown')
66 m <- data.frame('Value\n1', 'Value\n2')
67 colnames(m) <- c('Multiline\nCol1', 'Multiline\nCol2')
68 pandoc.table(m, keep.line.breaks = TRUE, style='rmarkdown')
69 ```
70
71 ## Cell alignment
72
73 `pander` allows users to control cell alignment (`left`, `right` or `center/centre`) in a table directly by setting the `justify` parameter when calling `pander/pandoc.table`. Note that it is possible to specify alignment for each column separately by supplying a vector to justify:
74
75 ```{r}
76 pandoc.table(head(iris[,1:3], 2), justify = 'right')
77 pandoc.table(head(iris[,1:3], 2), justify = c('right', 'center', 'left'))
78 ```
79
80 Another way to define alignment is by using a permanent option `table.alignment.default/table.alignment.rownames` in `panderOptions` (preferred way) or by using `set.alignment` function (legacy way of setting alignment for next table or permanently) which support setting alignment separately for cells and rownames:
81
82 ```{r}
83 set.alignment('left', row.names = 'right') # set only for next table since permanent parameter is falce
84 pandoc.table(mtcars[1:2, 1:5])
85 ```
86
87 Interesting application for this functionality is specifying a function that takes the R object as its argument to compute some unique alignment for your table based on e.g. column values or variable types:
88
89 ```{r}
90 panderOptions('table.alignment.default',
91 function(df)
92 ifelse(sapply(df, mean) > 2, 'left', 'right'))
93 pandoc.table(head(iris[,1:3], 2))
94 panderOptions('table.alignment.default', 'center')
95 ```
96 ## Highlighting cells
97
98 One of the great features of `pander` is the ease of highlighting rows, columns and cells in a table. This is a native `markdown` feature without custom `HTML` or `LaTeX`-only tweaks, so all `HTML/PDF/MS Word/OpenOffice` etc. formats are supported.
99
100 This can be achieved by specifying one of the arguments below when calling `pander`/`pandoc.table` or change default style using `panderOptions`:
101
102 * emphasize.italics.rows
103 * emphasize.italics.cols
104 * emphasize.italics.cells
105 * emphasize.strong.rows
106 * emphasize.strong.cols
107 * emphasize.strong.cells
108 * emphasize.verbatim.rows
109 * emphasize.verbatim.cols
110 * emphasize.verbatim.cells
111
112 The `emphasize.italics` helpers would turn the affected cells to *italic*, `emphasize.strong` would apply a **bold** style to the cell and `emphasize.verbatim` would apply a `verbatim` style to the cell. A cell can be also *italic*, **bold** and `verbatim` at the same time.
113
114 Those functions and arguments ending in rows or cols take a vector (like which columns or rows to emphasize in a table), while the cells argument take either a vector (for one dimensional "tables") or an array-like data structure with two columns holding row and column indexes of cells to be emphasized -- just like what `which(..., arr.ind = TRUE)` returns:
115
116 ```{r}
117 t <- mtcars[1:3, 1:5]
118 emphasize.italics.cols(1)
119 emphasize.italics.rows(1)
120 emphasize.strong.cells(which(t > 20, arr.ind = TRUE))
121 pandoc.table(t)
122 pandoc.table(t, emphasize.verbatim.rows = 1, emphasize.strong.cells = which(t > 20, arr.ind = TRUE))
123 ```
124
125 For more elaborative examples, please see our blog post - [Highlight cells in markdown tables](http://blog.rapporter.net/2013/04/hihglight-cells-in-markdown-tables.html).
126
127 ## Table and Cell width
128
129 `pander/pandoc.table` is able to deal with wide tables. Ever had an issue in `LaTeX` or `MS Word` when trying to print a correlation matrix of 40 variables? This problem is carefully addressed with `split.table` parameter:
130
131 ```{r}
132 pandoc.table(mtcars[1:2, ], style = "grid", caption = "Wide table to be split!")
133 ```
134
135 `split.table` defaults to 80 characters and to turn it off, set `split.table` to `Inf`:
136
137 ```{r}
138 pandoc.table(mtcars[1:2, ], style = "grid",
139 caption = "Wide table to be split!", split.table = Inf)
140 ```
141
142 Also, `pander` tries to split too wide cells into multiline cells. The maximum number of characters in a cell is specified by the `split.cells` parameter (defaults to 30), which can be a single value, vector (values for each column separately) and relative vector (percentages of `split.tables` parameter). Please not that this only works for `multiline` and `grid` tables:
143
144 ```{r, error=TRUE}
145 df <- data.frame(a = 'Lorem ipsum', b = 'dolor sit', c = 'amet')
146 pandoc.table(df, split.cells = 5)
147 pandoc.table(df, split.cells = c(5, 20, 5))
148 pandoc.table(df, split.cells = c("80%", "10%", "10%"))
149 pandoc.table(df, split.cells = 5, style = 'simple')
150 ```
151
152 In some cases it is also useful to split too long words with hyphens, and `pander` uses `sylly` functionality for that. Just specify `use.hyphening` argument and have `sylly` installed:
153
154 ```{r}
155 pandoc.table(data.frame(baz = 'foobar', foo='accoutrements'),
156 use.hyphening = TRUE, split.cells = 3)
157 ```
158
159 ## Rounding and number formatting
160
161 `pander/pandoc.table` deals with formatting numbers by having 4 parameters:
162
163 * `round` to the number of decimal places.
164 * `digits` to specify how many significant digits are to be used for numeric
165 * `decimal.mark/big.mark` to specify character for decimal point/orders of magnitude
166
167 `round` and `digits` parameter can be a vector specifying values for each column (has to be the same length as number of columns). Values for non-numeric columns will be disregarded.
168
169 Now let's get to some examples:
170 ```{r}
171 r <- matrix(c(283764.97430, 29.12345678901, -7.1234, -100.1), ncol = 2)
172 pandoc.table(r, round = 2)
173 pandoc.table(r, round = c(4,2)) # vector for each column
174 pandoc.table(r, digits = 2)
175 pandoc.table(r, digits = c(1, 5)) # vector for each column
176 pandoc.table(r, big.mark = ',')
177 pandoc.table(r, decimal.mark = ',')
178 ```
179 ## Other options
180
181 Functionality described in other sections is most notable, but `pander/pandoc.table` also has smaller nifty features that are worth mentioning:
182
183 * `plain.ascii` - allows to have the output without `markdown` markup:
184 ```{r}
185 pandoc.table(mtcars[1:3, 1:4])
186 pandoc.table(mtcars[1:3, 1:4], plain.ascii = TRUE)
187 ```
188
189 * `caption` - set caption (string) to be shown under the table:
190 ```{r}
191 pandoc.table(mtcars[1:3, 1:5], style = "grid", caption = "My caption!")
192 ```
193
194 * `missing` - set a string to replace missing values:
195 ```{r}
196 m <- mtcars[1:3, 1:5]
197 m$mpg <- NA
198 pandoc.table(m, missing = '?')
199 ```