Debian Codegraph @

vignettes/knitr.Rmd

raw
1 ---
2 title: "Using pander with knitr"
3 author: "Roman Tsegelskyi, Gergely Daróczi"
4 date: "`r Sys.Date()`"
5 output: rmarkdown::html_vignette
6 vignette: >
7 %\VignetteIndexEntry{Using pander with knitr}
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 library(knitr)
16 ```
17
18 While the report generation functionality of `pander` and `knitr` do overlap, we feel that the most powerful way to use `R/knitr/pander` for report generation is to utilize them together. This short vignette aims to explain how to embed `pander` output in reports generated by `knitr`. If you are not aware of `knitr`, be sure to check out the project's [homepage](https://yihui.org/knitr/) for extensive documentation and examples.
19
20 One of `knitr`'s most useful features is the ability to convert tables to output format on the fly. For example:
21
22 ```{r}
23 head(iris)
24 knitr::kable(head(iris))
25 ```
26
27 However, `kable` table generator is simple by design, and does not capture all the variety of classes that `R` has to offer. For example, `CrossTable` and `tabular` are not supported:
28
29 ```{r, error = TRUE}
30 library(descr, quietly = TRUE)
31 ct <- CrossTable(mtcars$gear, mtcars$cyl)
32 knitr::kable(ct)
33 library(tables, quietly = TRUE)
34 tab <- tabular( (Species + 1) ~ (n=1) + Format(digits=2)*
35 (Sepal.Length + Sepal.Width)*(mean + sd), data=iris )
36 knitr::kable(tab)
37 ```
38
39 This is where `pander` comes in handy, as `pander` supports rendering for many popular classes:
40
41 ```{r}
42 methods(pander)
43 ```
44
45 Also, `pander` is integrated with `knitr` by default. `pander` simply identifies if `knitr` is running in the background, and if so, it uses `capture.output` to return the resulting string as an `knit_asis` object, meaning that you do not need to specify the `results='asis'` option in your knitr chunk:
46
47 ```{r, error = TRUE}
48 library(descr, quietly = TRUE)
49 pander(CrossTable(mtcars$gear, mtcars$cyl))
50 library(tables, quietly = TRUE)
51 tab <- tabular( (Species + 1) ~ (n=1) + Format(digits=2)*
52 (Sepal.Length + Sepal.Width)*(mean + sd), data=iris )
53 pander(tab)
54 ```
55
56 In a nutshell, this is achieved by modification that whenever you call `pander` inside of a `knitr` document, instead of returning the markdown text to the standard output (the default behavior), `pander` returns a `knit_asis` class object, which renders correctly in the resulting document — without the double comment chars, thus properly rendering the tables in HTML, PDF, or other document formats.
57
58 If you don't want the results of `pander` to be converted automatically, just set `knitr.auto.asis` to `FALSE` using `panderOptions`:
59
60 ```{r}
61 panderOptions('knitr.auto.asis', FALSE)
62 pander(head(iris))
63 ```
64 ```{r}
65 panderOptions('knitr.auto.asis', TRUE)
66 ```
67
68 #### Rendering markdown inside loop/vectorized function
69
70 One frequenly asked question is how to use `pander` with `knitr` in a loop or vectorized function. For example, we have 3 tables that we want to render using `lapply`:
71
72 ```{r}
73 dfs <- list(mtcars[1:3, 1:4], mtcars[4:6, 1:4], mtcars[7:9, 1:4])
74 lapply(dfs, pander)
75 ```
76
77 As you can see, this doesn't work correctly because `pander` tries to return a `knit_asis` class object when run inside `knitr`, but for loops/vectorized functions this results in incorrect output. The recommended way to solve this is to disable this behavior by setting `knitr.auto.asis` to `FALSE` using `panderOptions`. However, we also need to tell `knitr` to convert the table on the fly by specifying `results='asis'` in the chunk options:
78
79 ```{r results='asis'}
80 panderOptions('knitr.auto.asis', FALSE)
81 dfs <- list(mtcars[1:3, 1:4], mtcars[4:6, 1:4], mtcars[7:9, 1:4])
82 invisible(lapply(dfs, pander))
83 ```