Pander tables inside of knitr

[This article was first published on rapporter, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)