Pander tables inside of knitr

September 18, 2014
By

(This article was first published on rapporter, and kindly contributed to R-bloggers)

Hadley Wickham opened my eyes that calling pander to generate nifty markdown tables inside of knitr requires a special chunk option, which bothersome extra step might be saved by updating pander a bit. So it’s done.

In a nutshell, whenever you call pander inside of a knitr document, instead of returning the markdown text to the standard output (as it used to happen), pander returns a knit_asis class object, which renders fine in the resulting document — without the double comment chars, so rendering the tables in HTML, pdf or other document formats just fine.

All those who might not like the new behaviour can of course disable it via panderOptions.

Quick demo:

No need to specify `results='asis'` anymore:

```{r}
## not to split tables
panderOptions('table.split.table', Inf)
## iris still rocks
pander(head(iris))
```

But you can if you wish:

```{r}
panderOptions('knitr.auto.asis', FALSE)
pander(head(iris))
```

```{r results='asis'}
pander(head(iris))
```

Results:

No need to specify `results='asis'` anymore:


```r
## not to split tables
panderOptions('table.split.table', Inf)
## iris still rocks
pander(head(iris))
```

-------------------------------------------------------------------
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
-------------- ------------- -------------- ------------- ---------
5.1 3.5 1.4 0.2 setosa

4.9 3 1.4 0.2 setosa

4.7 3.2 1.3 0.2 setosa

4.6 3.1 1.5 0.2 setosa

5 3.6 1.4 0.2 setosa

5.4 3.9 1.7 0.4 setosa
-------------------------------------------------------------------

But you can if you wish:


```r
panderOptions('knitr.auto.asis', FALSE)
pander(head(iris))
```

```
##
## -------------------------------------------------------------------
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## -------------- ------------- -------------- ------------- ---------
## 5.1 3.5 1.4 0.2 setosa
##
## 4.9 3 1.4 0.2 setosa
##
## 4.7 3.2 1.3 0.2 setosa
##
## 4.6 3.1 1.5 0.2 setosa
##
## 5 3.6 1.4 0.2 setosa
##
## 5.4 3.9 1.7 0.4 setosa
## -------------------------------------------------------------------
```

```r
pander(head(iris))
```

-------------------------------------------------------------------
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
-------------- ------------- -------------- ------------- ---------
5.1 3.5 1.4 0.2 setosa

4.9 3 1.4 0.2 setosa

4.7 3.2 1.3 0.2 setosa

4.6 3.1 1.5 0.2 setosa

5 3.6 1.4 0.2 setosa

5.4 3.9 1.7 0.4 setosa
-------------------------------------------------------------------

To leave a comment for the author, please follow the link and comment on their blog: rapporter.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)